forked from spiegela/minion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminion_spec.rb
More file actions
273 lines (201 loc) · 5.75 KB
/
Copy pathminion_spec.rb
File metadata and controls
273 lines (201 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# encoding: utf-8
require "spec_helper"
describe Minion do
let(:bunny) do
Bunny.new(Minion.config).tap do |bunny|
bunny.start
end
end
before(:all) do
Minion.logger {}
end
describe ".alert" do
context "when an error handler is provided" do
let(:error) do
RuntimeError.new("testing")
end
before do
Minion.error do |error|
error.message
end
end
after do
Minion.error
end
it "delegates to the handler" do
Minion.alert(error).should == "testing"
end
end
context "when an error handler is not provided" do
let(:error) do
RuntimeError.new("testing")
end
it "raises the error" do
expect { Minion.alert(error) }.to raise_error(RuntimeError)
end
end
end
describe ".enqueue" do
let(:queue) do
bunny.queue("minion.test", :durable => true, :auto_delete => false)
end
before do
queue.purge
end
context "when provided a string" do
context "when no data is provided" do
before do
Minion.enqueue("minion.test")
end
let(:message) do
JSON.parse(queue.pop[:payload])
end
it "adds empty json to the queue" do
message.should == {"content" => {}}
end
end
context "when nil data is provided" do
before do
Minion.enqueue("minion.test", nil)
end
let(:message) do
JSON.parse(queue.pop[:payload])
end
it "adds empty json to the queue" do
message.should == {"content" => nil}
end
end
context "when data is provided" do
context "when the data has no special characters" do
let(:data) do
{"content"=>{"field"=>"value"}}
end
before do
Minion.enqueue("minion.test", data)
end
let(:message) do
JSON.parse(queue.pop[:payload])
end
it "adds the json to the queue" do
message.should == data
end
end
context "without activesupport loaded" do
let(:data) do
{"content"=>{"field"=>"value"}}
end
it "ActiveSupport::JSON should not be defined" do
defined?(ActiveSupport::JSON).should be_false
end
it "should use the native JSON to serialize" do
JSON.expects(:dump).with(data).returns(data.to_json)
Minion.enqueue("minion.text", data)
end
end
context "with activesupport loaded" do
let(:data) do
{"content"=>{"field"=>"value"}}
end
before do
require 'active_support/json'
end
it "ActiveSupport::JSON should be defined" do
defined?(ActiveSupport::JSON).should be_true
end
it "should use ActiveSupport::JSON to serialize" do
ActiveSupport::JSON.expects(:encode).with(data, nil).returns(data.to_json)
end
end
context "when the data contains special characters" do
let(:data) do
{"content"=>{"field"=>"öüäßÖÜÄ"}}
end
before do
Minion.enqueue("minion.test", data)
end
let(:message) do
JSON.parse(queue.pop[:payload])
end
it "adds the json to the queue" do
message.should == data
end
end
end
end
context "when provided a nil queue" do
it "raises an error" do
expect { Minion.enqueue(nil, {}) }.to raise_error(RuntimeError)
end
end
context "when passed an array" do
let(:first) do
bunny.queue("minion.first", :durable => true, :auto_delete => false)
end
before do
first.purge
end
context "when the array is empty" do
it "raises an error" do
expect { Minion.enqueue([], {}) }.to raise_error(RuntimeError)
end
end
context "when the array has queue names" do
let(:data) do
{"field"=>"value"}
end
let(:serialized) do
{"content"=>{"field"=>"value"}, "callbacks"=>["minion.second", "minion.third"]}
end
before do
Minion.enqueue([ "minion.first", "minion.second", "minion.third" ], data)
end
let(:message) do
JSON.parse(first.pop[:payload])
end
it "adds the serialized data to the first queue" do
message.should == serialized
end
context "when the data has already been serialized" do
before do
Minion.enqueue([ "minion.first", "minion.second", "minion.third" ], serialized)
end
let(:message) do
JSON.parse(first.pop[:payload])
end
it "adds has the same serialized data in the first queue" do
message.should == serialized
end
end
end
end
end
describe ".error_handling" do
context "when nothing has been defined" do
it "returns nil" do
Minion.send(:error_handling).should be_nil
end
end
end
describe ".error" do
let(:block) do
lambda{ "testing" }
end
before do
Minion.error(&block)
end
it "sets the error handling to the provided block" do
Minion.send(:error_handling).should == block
end
end
describe ".info" do
let(:block) do
lambda{ |message| message }
end
before do
Minion.logger(&block)
end
it "delegates the logging to the provided block" do
Minion.info("testing").should == "testing"
end
end
end