mloughran-job_queue 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 7
3
+ :patch: 8
4
4
  :major: 0
@@ -1,20 +1,49 @@
1
+ # This adapter is designed for testing purposes.
2
+ #
3
+ # Features supported:
4
+ #
5
+ # named queues: yes
6
+ # priority: no
7
+ # ttr: no
8
+ #
9
+ # Additionally this queue can be inspeced with JobQueue.adapter.queue('name')
10
+ #
1
11
  class JobQueue::TestAdapter
2
12
  def initialize(options = {})
3
- @queue = []
13
+ @queues = {}
4
14
  end
5
-
6
- def put(string)
7
- @queue << string
15
+
16
+ def put(string, queue, priority, ttr)
17
+ @queues[queue] ||= []
18
+ @queues[queue] << string
8
19
  end
9
-
10
- def subscribe(error_report, &block)
20
+
21
+ def subscribe(error_report, cleanup_task, queue, &block)
11
22
  loop do
12
23
  begin
13
- sleep 0.1 if @queue.empty?
14
- yield @queue.shift
15
- rescue
16
- error_report.call(job.body, e)
24
+ if get_queue(queue).empty?
25
+ sleep 0.1
26
+ else
27
+ job = get_queue(queue).shift
28
+ yield job
29
+ end
30
+ rescue => e
31
+ error_report.call(job, e)
17
32
  end
18
33
  end
19
34
  end
35
+
36
+ # Additional method for TestAdapter to allow easy queue inspection with
37
+ #
38
+ # JobQueue.adapter.queue('foo')
39
+ #
40
+ def queue(queue = 'default')
41
+ get_queue(queue)
42
+ end
43
+
44
+ private
45
+
46
+ def get_queue(queue)
47
+ @queues[queue] || []
48
+ end
20
49
  end
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/common_adapter_spec'
2
3
 
3
4
  describe JobQueue::BeanstalkAdapter do
4
5
  before :each do
@@ -215,6 +216,10 @@ describe JobQueue::BeanstalkAdapter do
215
216
  JobQueue.adapter = JobQueue::BeanstalkAdapter.new
216
217
  end
217
218
 
219
+ describe "common" do
220
+ it_should_behave_like "JobQueue adapter named queues"
221
+ end
222
+
218
223
  it "should write onto queue and fetch stuff back off" do
219
224
  JobQueue.put("hello")
220
225
 
@@ -263,23 +268,6 @@ describe JobQueue::BeanstalkAdapter do
263
268
  end
264
269
  }
265
270
  end
266
-
267
- it "should put jobs onto a named queue and only read off that queue" do
268
- JobQueue.put("hello", :queue => "test")
269
- lambda {
270
- Timeout.timeout(0.1) do
271
- JobQueue.subscribe(:queue => "foo") do |job|
272
- throw :stop
273
- end
274
- end
275
- }.should raise_error(Timeout::Error)
276
- should_not_timeout {
277
- JobQueue.subscribe(:queue => "test") do |body|
278
- body.should == 'hello'
279
- throw :stop
280
- end
281
- }
282
- end
283
271
  end
284
272
 
285
273
  describe "when connecting to multiple instances" do
@@ -327,11 +315,3 @@ describe JobQueue::BeanstalkAdapter do
327
315
  end
328
316
  end
329
317
  end
330
-
331
- def should_not_timeout(timeout = 0.1)
332
- lambda {
333
- Timeout.timeout(timeout) do
334
- yield
335
- end
336
- }.should_not raise_error(Timeout::Error)
337
- end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ shared_examples_for 'JobQueue adapter basic' do
4
+ it "should write onto queue and fetch stuff back off" do
5
+ JobQueue.put("hello")
6
+
7
+ JobQueue.subscribe do |job|
8
+ @job = job
9
+ throw :stop
10
+ end
11
+
12
+ @job.should == "hello"
13
+ end
14
+
15
+ it "should pull items off in the order the were added" do
16
+ JobQueue.put("foo")
17
+ JobQueue.put("bar")
18
+
19
+ retrieved_jobs = []
20
+
21
+ begin
22
+ Timeout::timeout(0.5) do
23
+ JobQueue.subscribe do |job|
24
+ retrieved_jobs << job
25
+ end
26
+ end
27
+ rescue Timeout::Error
28
+
29
+ end
30
+
31
+ retrieved_jobs[0].should == "foo"
32
+ retrieved_jobs[1].should == "bar"
33
+ retrieved_jobs[2].should == nil
34
+ end
35
+ end
36
+
37
+ shared_examples_for "JobQueue adapter named queues" do
38
+ it "should put jobs onto a named queue and only read off that queue" do
39
+ JobQueue.put("hello", :queue => "test")
40
+ lambda {
41
+ Timeout.timeout(0.1) do
42
+ JobQueue.subscribe(:queue => "foo") do |job|
43
+ throw :stop
44
+ end
45
+ end
46
+ }.should raise_error(Timeout::Error)
47
+ should_not_timeout {
48
+ JobQueue.subscribe(:queue => "test") do |body|
49
+ body.should == 'hello'
50
+ throw :stop
51
+ end
52
+ }
53
+ end
54
+ end
@@ -3,3 +3,11 @@ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
3
 
4
4
  require 'rubygems'
5
5
  require 'job_queue'
6
+
7
+ def should_not_timeout(timeout = 0.1)
8
+ lambda {
9
+ Timeout.timeout(timeout) do
10
+ yield
11
+ end
12
+ }.should_not raise_error(Timeout::Error)
13
+ end
@@ -1,39 +1,18 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/common_adapter_spec'
2
3
 
3
4
  describe JobQueue::TestAdapter do
4
5
  before :all do
5
6
  JobQueue.adapter = JobQueue::TestAdapter.new
6
7
  end
7
8
 
8
- it "should write onto queue and fetch stuff back off" do
9
- JobQueue.put("hello")
10
-
11
- JobQueue.subscribe do |job|
12
- @job = job
13
- throw :stop
14
- end
15
-
16
- @job.should == "hello"
17
- end
9
+ it_should_behave_like 'JobQueue adapter basic'
10
+
11
+ it_should_behave_like "JobQueue adapter named queues"
18
12
 
19
- it "should pull items off in the order the were added" do
20
- JobQueue.put("foo")
21
- JobQueue.put("bar")
22
-
23
- retrieved_jobs = []
24
-
25
- begin
26
- Timeout::timeout(0.5) do
27
- JobQueue.subscribe do |job|
28
- retrieved_jobs << job
29
- end
30
- end
31
- rescue Timeout::Error
32
-
33
- end
34
-
35
- retrieved_jobs[0].should == "foo"
36
- retrieved_jobs[1].should == "bar"
37
- retrieved_jobs[2].should == nil
13
+ it "should allow queue inspection as a hash" do
14
+ JobQueue.adapter.queue.should == []
15
+ JobQueue.put('hello')
16
+ JobQueue.adapter.queue.should == ['hello']
38
17
  end
39
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mloughran-job_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martyn Loughran
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-22 00:00:00 -07:00
12
+ date: 2009-06-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -34,6 +34,7 @@ files:
34
34
  - lib/job_queue.rb
35
35
  - spec/amqp_adapter_spec.rb
36
36
  - spec/beanstalk_adapter_spec.rb
37
+ - spec/common_adapter_spec.rb
37
38
  - spec/job_queue_spec.rb
38
39
  - spec/spec_helper.rb
39
40
  - spec/test_adapter_spec.rb