micro_q 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.ruby-version +1 -0
  3. data/.travis.yml +9 -1
  4. data/README.md +5 -2
  5. data/lib/micro_q/config.rb +1 -0
  6. data/lib/micro_q/dsl.rb +41 -16
  7. data/lib/micro_q/manager/default.rb +53 -7
  8. data/lib/micro_q/middleware/chain.rb +15 -2
  9. data/lib/micro_q/middleware/client/statistics.rb +22 -0
  10. data/lib/micro_q/middleware/server/retry.rb +20 -5
  11. data/lib/micro_q/middleware/server/statistics.rb +22 -0
  12. data/lib/micro_q/middleware/server/timeout.rb +19 -0
  13. data/lib/micro_q/middleware/util.rb +21 -0
  14. data/lib/micro_q/queue/default.rb +25 -18
  15. data/lib/micro_q/queue/redis.rb +2 -2
  16. data/lib/micro_q/statistics/base.rb +15 -0
  17. data/lib/micro_q/statistics/default.rb +18 -0
  18. data/lib/micro_q/statistics/redis.rb +23 -0
  19. data/lib/micro_q/version.rb +1 -1
  20. data/lib/micro_q/worker/standard.rb +20 -10
  21. data/lib/micro_q.rb +25 -3
  22. data/micro_q.gemspec +1 -1
  23. data/spec/helpers/methods_examples.rb +1 -1
  24. data/spec/helpers/queues_examples.rb +4 -4
  25. data/spec/lib/config_spec.rb +4 -0
  26. data/spec/lib/dsl_spec.rb +14 -2
  27. data/spec/lib/manager/default_spec.rb +90 -16
  28. data/spec/lib/methods/action_mailer_spec.rb +2 -2
  29. data/spec/lib/methods/active_record_spec.rb +2 -2
  30. data/spec/lib/methods/class_spec.rb +2 -2
  31. data/spec/lib/methods/instance_spec.rb +2 -2
  32. data/spec/lib/micro_q_spec.rb +13 -2
  33. data/spec/lib/middleware/chain_spec.rb +83 -6
  34. data/spec/lib/middleware/client/statistics_spec.rb +53 -0
  35. data/spec/lib/middleware/server/connection_spec.rb +1 -1
  36. data/spec/lib/middleware/server/retry_spec.rb +33 -4
  37. data/spec/lib/middleware/server/statistics_spec.rb +53 -0
  38. data/spec/lib/middleware/server/timeout_spec.rb +40 -0
  39. data/spec/lib/proxies/base_spec.rb +2 -2
  40. data/spec/lib/proxies/instance_spec.rb +1 -1
  41. data/spec/lib/queue/default_spec.rb +2 -4
  42. data/spec/lib/queue/redis_spec.rb +2 -4
  43. data/spec/lib/statistics/default_spec.rb +73 -0
  44. data/spec/lib/statistics/redis_spec.rb +73 -0
  45. data/spec/lib/util_spec.rb +1 -1
  46. data/spec/lib/worker/standard_spec.rb +26 -11
  47. data/spec/lib/wrappers/action_mailer_spec.rb +2 -2
  48. data/spec/spec_helper.rb +1 -1
  49. metadata +49 -58
  50. data/lib/micro_q/methods.rb +0 -7
  51. data/lib/micro_q/wrappers.rb +0 -1
data/spec/lib/dsl_spec.rb CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe MicroQ::DSL do
4
4
  describe '.worker' do
5
5
  class OtherWorker
6
- worker :update
6
+ worker :update, :option => 'value'
7
7
  end
8
8
 
9
9
  it 'should add the method' do
@@ -11,7 +11,7 @@ describe MicroQ::DSL do
11
11
  end
12
12
 
13
13
  it 'should include the dsl module' do
14
- Object.should be_is_a(MicroQ::DSL)
14
+ Object.should be_is_a(MicroQ::DSL::ClassMethods)
15
15
  end
16
16
 
17
17
  it 'should add the async_perform method' do
@@ -42,6 +42,12 @@ describe MicroQ::DSL do
42
42
  method.call
43
43
  end
44
44
 
45
+ it 'should have the \'worker\' options' do
46
+ MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:option => 'value')).and_return(@proxy)
47
+
48
+ method.call
49
+ end
50
+
45
51
  it 'should call the perform method' do
46
52
  @proxy.should_receive(:perform)
47
53
 
@@ -69,6 +75,12 @@ describe MicroQ::DSL do
69
75
  method.call
70
76
  end
71
77
 
78
+ it 'should have the \'worker\' options' do
79
+ MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:option => 'value')).and_return(@proxy)
80
+
81
+ method.call
82
+ end
83
+
72
84
  it 'should call the update method' do
73
85
  @proxy.should_receive(:update)
74
86
 
@@ -3,25 +3,38 @@ require 'spec_helper'
3
3
  describe MicroQ::Manager::Default do
4
4
  let(:create) { -> { subject } }
5
5
 
6
+ before do
7
+ MicroQ.config.workers = 2
8
+ end
9
+
6
10
  describe '.new' do
7
11
  before do
8
12
  MicroQ.config.workers = 4
9
13
  end
10
14
 
11
15
  it 'should create a default queue' do
12
- MicroQ::Queue::Default.should_receive(:new)
16
+ MicroQ::Queue::Default.should_receive(:new_link)
17
+
18
+ create.call
19
+ end
20
+
21
+ it 'should create a worker links' do
22
+ MicroQ::Worker::Standard.should_receive(:new_link).at_least(1)
13
23
 
14
24
  create.call
15
25
  end
16
26
 
17
- it 'should create a worker pool' do
18
- MicroQ::Worker::Standard.should_receive(:pool)
27
+ it 'should create config.workers number of links' do
28
+ MicroQ::Worker::Standard.should_receive(:new_link).exactly(4)
19
29
 
20
30
  create.call
21
31
  end
22
32
 
23
- it 'should be a config.workers size pool' do
24
- MicroQ::Worker::Standard.should_receive(:pool).with(hash_including(:size => 4))
33
+ it 'should pass itself to the worker' do
34
+ MicroQ::Worker::Standard.should_receive(:new_link).at_least(1) do |manager|
35
+ manager.class.should == MicroQ::Manager::Default
36
+ manager.wrapped_object.class.should == MicroQ::Manager::Default
37
+ end
25
38
 
26
39
  create.call
27
40
  end
@@ -35,41 +48,102 @@ describe MicroQ::Manager::Default do
35
48
 
36
49
  describe '#workers' do
37
50
  it 'should be the workers' do
38
- subject.workers.class.should == Celluloid::PoolManager
39
- subject.workers.inspect.should match(/MicroQ::Worker::Standard/)
51
+ subject.workers.collect {|w| w.wrapped_object.class}.uniq.should == [MicroQ::Worker::Standard]
40
52
  end
41
53
  end
42
54
 
43
55
  describe '#start' do
44
56
  it 'should not be performing' do
45
- subject.workers.should_not_receive(:perform!)
57
+ subject.workers.each{|w| w.should_not_receive(:perform!) }
46
58
 
47
59
  subject.start
48
60
  end
49
61
 
50
62
  describe 'when the queue has dequeue-able items' do
51
63
  before do
52
- @item, @other_item = mock(Hash), mock(Hash)
64
+ @item, @other_item = mock('Hash 1'), mock('Hash 2')
53
65
  @queue = mock(MicroQ::Queue::Default, :dequeue => [@item, @other_item])
54
- MicroQ::Queue::Default.stub(:new).and_return(@queue)
66
+ MicroQ::Queue::Default.stub(:new_link).and_return(@queue)
55
67
 
56
- @pool = mock(Celluloid::PoolManager, :idle_size => 1234, :perform! => nil)
57
- MicroQ::Worker::Standard.stub(:pool).and_return(@pool)
68
+ @worker1 = mock(MicroQ::Worker::Standard, :perform! => nil)
69
+ @worker2 = mock(MicroQ::Worker::Standard, :perform! => nil)
70
+ MicroQ::Worker::Standard.stub(:new_link).and_return(@worker1, @worker2)
58
71
  end
59
72
 
60
73
  it 'should dequeue the number of free workers' do
61
- @queue.should_receive(:dequeue).with(1234)
74
+ @queue.should_receive(:dequeue).with(2)
62
75
 
63
76
  subject.start
64
77
  end
65
78
 
66
79
  it 'should perform the items' do
67
- @pool.should_receive(:perform!).with(@item) do
68
- @pool.should_receive(:perform!).with(@other_item)
69
- end
80
+ @worker1.should_receive(:perform!).with(@other_item)
81
+ @worker2.should_receive(:perform!).with(@item)
70
82
 
71
83
  subject.start
72
84
  end
73
85
  end
74
86
  end
87
+
88
+ describe '#reinitialize' do
89
+ let(:death) { -> { subject.reinitialize } }
90
+
91
+ before do
92
+ @queue = mock(MicroQ::Queue::Default, :alive? => true, :dequeue => [])
93
+ MicroQ::Queue::Default.stub(:new_link).and_return(@queue)
94
+
95
+ @worker1 = mock(MicroQ::Worker::Standard, :alive? => true, :perform! => nil)
96
+ @worker2 = mock(MicroQ::Worker::Standard, :alive? => true, :perform! => nil)
97
+ MicroQ::Worker::Standard.stub(:new_link).and_return(@worker1, @worker2)
98
+
99
+ subject.start
100
+ end
101
+
102
+ it 'should have the items' do
103
+ subject.queue.should == @queue
104
+ subject.workers.should == [@worker1, @worker2]
105
+ end
106
+
107
+ describe 'when the queue died' do
108
+ before do
109
+ @queue.stub(:alive?).and_return(false)
110
+
111
+ @new_queue = mock(MicroQ::Queue::Default)
112
+ MicroQ::Queue::Default.stub(:new_link).and_return(@new_queue)
113
+ end
114
+
115
+ it 'should restart the queue' do
116
+ MicroQ::Queue::Default.should_receive(:new_link).and_return(@new_queue)
117
+
118
+ death.call
119
+ end
120
+
121
+ it 'should have the new queue' do
122
+ death.call
123
+
124
+ subject.queue.should == @new_queue
125
+ end
126
+ end
127
+
128
+ describe 'when a worker has died' do
129
+ before do
130
+ @worker2.stub(:alive?).and_return(false)
131
+
132
+ @new_worker2 = mock(MicroQ::Worker::Standard)
133
+ MicroQ::Worker::Standard.stub(:new_link).and_return(@new_worker2)
134
+ end
135
+
136
+ it 'should restart the dead worker' do
137
+ MicroQ::Worker::Standard.should_receive(:new_link).and_return(@new_worker2)
138
+
139
+ death.call
140
+ end
141
+
142
+ it 'should have the new worker' do
143
+ death.call
144
+
145
+ subject.workers.should == [@worker1, @new_worker2]
146
+ end
147
+ end
148
+ end
75
149
  end
@@ -26,10 +26,10 @@ describe MicroQ::Methods::ActionMailer do
26
26
  end
27
27
 
28
28
  describe 'when given when to run the job' do
29
- let(:method) { -> { MyMailer.async(:at => "sometime") } }
29
+ let(:method) { -> { MyMailer.async(:at => 'sometime') } }
30
30
 
31
31
  it 'should pass the option' do
32
- MicroQ::Proxy::ActionMailer.should_receive(:new).with(hash_including(:at => "sometime"))
32
+ MicroQ::Proxy::ActionMailer.should_receive(:new).with(hash_including(:at => 'sometime'))
33
33
 
34
34
  method.call
35
35
  end
@@ -59,10 +59,10 @@ describe MicroQ::Methods::ActiveRecord, :active_record => true do
59
59
  it_behaves_like 'an async AR instance'
60
60
 
61
61
  describe 'when given when to run the job' do
62
- let(:method) { -> { subject.async(:at => "sometime") } }
62
+ let(:method) { -> { subject.async(:at => 'sometime') } }
63
63
 
64
64
  it 'should pass the option' do
65
- MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:at => "sometime"))
65
+ MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:at => 'sometime'))
66
66
 
67
67
  method.call
68
68
  end
@@ -53,10 +53,10 @@ describe MicroQ::Methods::Class do
53
53
  it_behaves_like 'an async class'
54
54
 
55
55
  describe 'when given when to run the job' do
56
- let(:method) { -> { subject.async(:at => "sometime") } }
56
+ let(:method) { -> { subject.async(:at => 'sometime') } }
57
57
 
58
58
  it 'should pass the option' do
59
- MicroQ::Proxy::Class.should_receive(:new).with(hash_including(:at => "sometime"))
59
+ MicroQ::Proxy::Class.should_receive(:new).with(hash_including(:at => 'sometime'))
60
60
 
61
61
  method.call
62
62
  end
@@ -47,10 +47,10 @@ describe MicroQ::Methods::Instance do
47
47
  it_behaves_like 'an async instance'
48
48
 
49
49
  describe 'when given when to run the job' do
50
- let(:method) { -> { subject.async(:at => "sometime") } }
50
+ let(:method) { -> { subject.async(:at => 'sometime') } }
51
51
 
52
52
  it 'should pass the option' do
53
- MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:at => "sometime"))
53
+ MicroQ::Proxy::Instance.should_receive(:new).with(hash_including(:at => 'sometime'))
54
54
 
55
55
  method.call
56
56
  end
@@ -6,7 +6,7 @@ describe MicroQ do
6
6
  MicroQ.configure {|config| config.class.should == MicroQ::Config }
7
7
  end
8
8
 
9
- it "should cache the value" do
9
+ it 'should cache the value' do
10
10
  configs = []
11
11
 
12
12
  2.times { MicroQ.configure {|c| configs << c } }
@@ -27,6 +27,17 @@ describe MicroQ do
27
27
  end
28
28
  end
29
29
 
30
+ describe '.stats' do
31
+ it 'should have the statistics' do
32
+ klass = nil
33
+ MicroQ.stats do |stats|
34
+ klass = stats.class
35
+ end
36
+
37
+ klass.should == MicroQ::Statistics::Default
38
+ end
39
+ end
40
+
30
41
  describe '.start' do
31
42
  def start
32
43
  MicroQ.start
@@ -64,7 +75,7 @@ describe MicroQ do
64
75
  end
65
76
 
66
77
  before do
67
- @manager = mock(MicroQ::Manager::Default, :start! => nil, :queue => mock("Queue"))
78
+ @manager = mock(MicroQ::Manager::Default, :start! => nil, :queue => mock('Queue'))
68
79
  MicroQ::Manager::Default.stub(:new).and_return(@manager)
69
80
 
70
81
  MicroQ.start
@@ -1,10 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MicroQ::Middleware::Chain do
4
+ class MyMiddleware; end
5
+
4
6
  describe MicroQ::Middleware::Chain::Base do
5
7
  subject { MicroQ::Middleware::Chain::Base.new }
6
8
 
7
- class MyMiddleware; end
8
9
  class OtherMiddleware; end
9
10
 
10
11
  describe '#add' do
@@ -205,14 +206,17 @@ describe MicroQ::Middleware::Chain do
205
206
  end
206
207
 
207
208
  describe 'defaults' do
208
- [MicroQ::Middleware::Server::Retry, MicroQ::Middleware::Server::Connection].each do |klass|
209
+ [MicroQ::Middleware::Server::Statistics,
210
+ MicroQ::Middleware::Server::Timeout,
211
+ MicroQ::Middleware::Server::Retry,
212
+ MicroQ::Middleware::Server::Connection].each do |klass|
209
213
  it "should include #{klass}" do
210
214
  subject.server.entries.should include(klass)
211
215
  end
212
216
  end
213
217
 
214
- it 'should be 2 items long' do
215
- subject.server.entries.should have(2).items
218
+ it 'should be 4 items long' do
219
+ subject.server.entries.should have(4).items
216
220
  end
217
221
  end
218
222
  end
@@ -226,8 +230,16 @@ describe MicroQ::Middleware::Chain do
226
230
  subject.client.object_id.should == subject.client.object_id
227
231
  end
228
232
 
229
- it "should be empty" do
230
- subject.client.entries.should == []
233
+ describe 'defaults' do
234
+ [MicroQ::Middleware::Client::Statistics].each do |klass|
235
+ it "should include #{klass}" do
236
+ subject.client.entries.should include(klass)
237
+ end
238
+ end
239
+
240
+ it 'should be 1 item long' do
241
+ subject.client.entries.should have(1).item
242
+ end
231
243
  end
232
244
  end
233
245
 
@@ -243,6 +255,12 @@ describe MicroQ::Middleware::Chain do
243
255
  subject.server.call(worker, payload) { }
244
256
  end
245
257
 
258
+ it 'should make a new timeout instance' do
259
+ MicroQ::Middleware::Server::Timeout.should_receive(:new).and_call_original
260
+
261
+ call
262
+ end
263
+
246
264
  it 'should make a new retry instance' do
247
265
  MicroQ::Middleware::Server::Retry.should_receive(:new).and_call_original
248
266
 
@@ -255,6 +273,27 @@ describe MicroQ::Middleware::Chain do
255
273
  call
256
274
  end
257
275
 
276
+ it 'should make a new statistics instance' do
277
+ MicroQ::Middleware::Server::Statistics.should_receive(:new).and_call_original
278
+
279
+ call
280
+ end
281
+
282
+ it 'should not remove the Statistics' do
283
+ subject.server.remove(MicroQ::Middleware::Server::Statistics)
284
+
285
+ subject.server.entries.should include(MicroQ::Middleware::Server::Statistics)
286
+ end
287
+
288
+ it 'should call the timeout middleware' do
289
+ @timeout = mock(MicroQ::Middleware::Server::Timeout)
290
+ MicroQ::Middleware::Server::Timeout.stub(:new).and_return(@timeout)
291
+
292
+ @timeout.should_receive(:call).with(worker, payload)
293
+
294
+ call
295
+ end
296
+
258
297
  it 'should call the retry middleware' do
259
298
  @retry = mock(MicroQ::Middleware::Server::Retry)
260
299
  MicroQ::Middleware::Server::Retry.stub(:new).and_return(@retry)
@@ -272,6 +311,44 @@ describe MicroQ::Middleware::Chain do
272
311
 
273
312
  call
274
313
  end
314
+
315
+ it 'should call the statistics middleware' do
316
+ statistics = mock(MicroQ::Middleware::Server::Connection)
317
+ MicroQ::Middleware::Server::Connection.stub(:new).and_return(statistics)
318
+
319
+ statistics.should_receive(:call).with(worker, payload)
320
+
321
+ call
322
+ end
323
+ end
324
+
325
+ describe 'client' do
326
+ let(:opts) { mock('options') }
327
+
328
+ def call
329
+ subject.client.call(payload, opts) { }
330
+ end
331
+
332
+ it 'should make a new statistics instance' do
333
+ MicroQ::Middleware::Client::Statistics.should_receive(:new).and_call_original
334
+
335
+ call
336
+ end
337
+
338
+ it 'should not remove the Statistics' do
339
+ subject.client.remove(MicroQ::Middleware::Client::Statistics)
340
+
341
+ subject.client.entries.should include(MicroQ::Middleware::Client::Statistics)
342
+ end
343
+
344
+ it 'should call the statistics middleware' do
345
+ statistics = mock(MicroQ::Middleware::Client::Statistics)
346
+ MicroQ::Middleware::Client::Statistics.stub(:new).and_return(statistics)
347
+
348
+ statistics.should_receive(:call).with(payload, opts)
349
+
350
+ call
351
+ end
275
352
  end
276
353
  end
277
354
  end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe MicroQ::Middleware::Client::Statistics, :middleware => true do
4
+ describe '#call' do
5
+ let(:foo) { mock('Foo', :bar => nil) }
6
+ let(:block) { -> { foo.bar } }
7
+
8
+ def call
9
+ subject.call @payload, {}, &block
10
+ end
11
+
12
+ before do
13
+ @stats = mock(MicroQ::Statistics::Default, :incr => nil)
14
+ MicroQ::Statistics::Default.stub(:stats).and_yield(@stats)
15
+ end
16
+
17
+ it 'should execute the block' do
18
+ foo.should_receive(:bar)
19
+
20
+ call
21
+ end
22
+
23
+ it 'should call into the statistics backend' do
24
+ MicroQ::Statistics::Default.should_receive(:stats)
25
+
26
+ call
27
+ end
28
+
29
+ it 'should log a enqueued message' do
30
+ @stats.should_receive(:incr) do |*args|
31
+ args.should include('messages:enqueued')
32
+ end
33
+
34
+ call
35
+ end
36
+
37
+ it 'should log the enqueued class message' do
38
+ @stats.should_receive(:incr) do |*args|
39
+ args.should include("messages:#{@payload['class']}:enqueued")
40
+ end
41
+
42
+ call
43
+ end
44
+
45
+ it 'should log the queue' do
46
+ @stats.should_receive(:incr) do |*args|
47
+ args.should include('queues:a-queue:enqueued')
48
+ end
49
+
50
+ call
51
+ end
52
+ end
53
+ end
@@ -3,7 +3,7 @@ require 'active_record'
3
3
 
4
4
  describe MicroQ::Middleware::Server::Connection, :middleware => true do
5
5
  describe '#call' do
6
- let(:foo) { mock("Foo", :bar => nil) }
6
+ let(:foo) { mock('Foo', :bar => nil) }
7
7
  let(:block) { -> { foo.bar } }
8
8
 
9
9
  def call
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MicroQ::Middleware::Server::Retry, :middleware => true do
4
4
  describe '#call' do
5
- let(:foo) { mock("Foo", :bar => nil) }
5
+ let(:foo) { mock('Foo', :bar => nil) }
6
6
  let(:block) { -> { foo.bar } }
7
7
 
8
8
  def call
@@ -16,9 +16,14 @@ describe MicroQ::Middleware::Server::Retry, :middleware => true do
16
16
  end
17
17
 
18
18
  describe 'when the block raises an Exception' do
19
- let(:exception) { Exception.new }
19
+ let(:exception) { Exception.new('an exception') }
20
20
  let(:block) { -> { raise exception } }
21
21
 
22
+ before do
23
+ @stats = mock(MicroQ::Statistics::Default, :incr => nil)
24
+ MicroQ::Statistics::Default.stub(:stats).and_yield(@stats)
25
+ end
26
+
22
27
  describe 'when retry is disabled' do
23
28
  before do
24
29
  @payload['retry'] = false
@@ -27,7 +32,7 @@ describe MicroQ::Middleware::Server::Retry, :middleware => true do
27
32
  it 'should re-raise the error' do
28
33
  expect {
29
34
  call
30
- }.to raise_error(exception)
35
+ }.to raise_error(Exception, 'an exception')
31
36
  end
32
37
  end
33
38
 
@@ -39,7 +44,31 @@ describe MicroQ::Middleware::Server::Retry, :middleware => true do
39
44
  it 'should re-raise the error' do
40
45
  expect {
41
46
  call
42
- }.to raise_error(exception)
47
+ }.to raise_error(Exception, 'an exception')
48
+ end
49
+
50
+ it 'should log the retry' do
51
+ @stats.should_receive(:incr) do |*args|
52
+ args.should include('messages:retry')
53
+ end
54
+
55
+ safe(:call)
56
+ end
57
+
58
+ it 'should log the class\' retry' do
59
+ @stats.should_receive(:incr) do |*args|
60
+ args.should include("messages:#{@payload['class']}:retry")
61
+ end
62
+
63
+ safe(:call)
64
+ end
65
+
66
+ it 'should log the queues\' retry' do
67
+ @stats.should_receive(:incr) do |*args|
68
+ args.should include('queues:a-queue:retry')
69
+ end
70
+
71
+ safe(:call)
43
72
  end
44
73
 
45
74
  it 'should increment the number of retries' do
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe MicroQ::Middleware::Server::Statistics, :middleware => true do
4
+ describe '#call' do
5
+ let(:foo) { mock('Foo', :bar => nil) }
6
+ let(:block) { -> { foo.bar } }
7
+
8
+ def call
9
+ subject.call @worker, @payload, &block
10
+ end
11
+
12
+ before do
13
+ @stats = mock(MicroQ::Statistics::Default, :incr => nil)
14
+ MicroQ::Statistics::Default.stub(:stats).and_yield(@stats)
15
+ end
16
+
17
+ it 'should execute the block' do
18
+ foo.should_receive(:bar)
19
+
20
+ call
21
+ end
22
+
23
+ it 'should call into the statistics backend' do
24
+ MicroQ::Statistics::Default.should_receive(:stats)
25
+
26
+ call
27
+ end
28
+
29
+ it 'should log a completed message' do
30
+ @stats.should_receive(:incr) do |*args|
31
+ args.should include('messages:performed')
32
+ end
33
+
34
+ call
35
+ end
36
+
37
+ it 'should log the completed class message' do
38
+ @stats.should_receive(:incr) do |*args|
39
+ args.should include("messages:#{@payload['class']}:performed")
40
+ end
41
+
42
+ call
43
+ end
44
+
45
+ it 'should log the queue' do
46
+ @stats.should_receive(:incr) do |*args|
47
+ args.should include('queues:a-queue:performed')
48
+ end
49
+
50
+ call
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe MicroQ::Middleware::Server::Timeout, :middleware => true do
4
+ describe '#call' do
5
+ let(:foo) { mock('Foo', :bar => nil) }
6
+ let(:block) { -> { foo.bar } }
7
+
8
+ def call
9
+ subject.call @worker, @payload, &block
10
+ end
11
+
12
+ before do
13
+ Timecop.freeze(DateTime.now)
14
+ end
15
+
16
+ it 'should execute the block' do
17
+ foo.should_receive(:bar)
18
+
19
+ call
20
+ end
21
+
22
+ it 'should timeout after 10 minutes' do
23
+ subject.should_receive(:timeout).with(10 * 60)
24
+
25
+ call
26
+ end
27
+
28
+ describe 'when the timeout is set' do
29
+ before do
30
+ @payload.merge!('timeout' => 60)
31
+ end
32
+
33
+ it 'should timeout after 60 seconds' do
34
+ subject.should_receive(:timeout).with(60)
35
+
36
+ call
37
+ end
38
+ end
39
+ end
40
+ end
@@ -61,14 +61,14 @@ describe MicroQ::Proxy::Base do
61
61
  it "should have an error for #{type.inspect}" do
62
62
  options[:class] = type
63
63
 
64
- subject.errors.should include("Proxies require a valid class")
64
+ subject.errors.should include('Proxies require a valid class')
65
65
  end
66
66
  end
67
67
  end
68
68
  end
69
69
  end
70
70
 
71
- describe "#respond_to?" do
71
+ describe '#respond_to?' do
72
72
  it 'should be false' do
73
73
  subject.respond_to?(:not_a_method).should == false
74
74
  end
@@ -14,7 +14,7 @@ describe MicroQ::Proxy::Instance do
14
14
  subject.class.ancestors.should include(MicroQ::Proxy::Base)
15
15
  end
16
16
 
17
- describe "#respond_to?" do
17
+ describe '#respond_to?' do
18
18
  it 'should be false' do
19
19
  subject.respond_to?(:not_a_method).should == false
20
20
  end