Permalink
Browse files

Added new tests for 1 and 2 concurrent jobs

  • Loading branch information...
1 parent 028822a commit baca71a50978c666a31e3aa313be4b9211f93a10 @unlimit committed Apr 17, 2012
Showing with 111 additions and 22 deletions.
  1. +86 −22 spec/resque_worker_extensions_spec.rb
  2. +25 −0 spec/spec_helper.rb
View
108 spec/resque_worker_extensions_spec.rb
@@ -16,21 +16,21 @@
run_resque_queue('*')
Resque.redis.keys("restriction:*").should == []
end
-
+
it "should run normal job without restriction" do
run_resque_job(NoRestrictionJob, :queue => :normal, :inline => true)
Resque.size(:normal).should == 0
NoRestrictionJob.run_count.should == 1
Resque.redis.keys("restriction:*").should == []
end
-
+
it "should run a restricted job that is not currently restricted" do
run_resque_job(RestrictionJob, :queue => :normal)
Resque.size(:normal).should == 0
RestrictionJob.run_count.should == 1
RestrictionJob.running_count(RestrictionJob.tracking_key).should == 0
end
-
+
it "should stash a restricted job that is currently restricted" do
RestrictionJob.set_running_count(RestrictionJob.tracking_key, 99)
@@ -60,7 +60,7 @@
it "should prefer running a normal job over one on restriction queue" do
Resque::Plugins::ConcurrentRestriction.restricted_before_queued.should == false
-
+
RestrictionJob.set_running_count(RestrictionJob.tracking_key, 99)
run_resque_job(RestrictionJob, :queue => :normal)
@@ -90,7 +90,7 @@
RestrictionJob.run_count.should == 0
RestrictionJob.set_running_count(RestrictionJob.tracking_key, 0)
-
+
run_resque_job(NoRestrictionJob, :queue => :normal)
RestrictionJob.restriction_queue(RestrictionJob.tracking_key, :normal).should == []
NoRestrictionJob.run_count.should == 0
@@ -141,39 +141,103 @@
ConcurrentRestrictionJob.running_count(ConcurrentRestrictionJob.tracking_key).should == 0
end
+ it "should run only one concurrent job" do
+ 5.times {|i| Resque.enqueue(OneConcurrentRestrictionJob, i) }
+ 5.times do
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
+ end
+ sleep 0.25
+
+ OneConcurrentRestrictionJob.total_run_count.should == 1
+ OneConcurrentRestrictionJob.running_count(OneConcurrentRestrictionJob.tracking_key).should == 1
+ OneConcurrentRestrictionJob.restriction_queue(OneConcurrentRestrictionJob.tracking_key, :normal).size.should == 4
+
+ Process.waitall
+
+ 2.times do
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
+ end
+ sleep 0.25
+
+ OneConcurrentRestrictionJob.total_run_count.should == 2
+ OneConcurrentRestrictionJob.running_count(OneConcurrentRestrictionJob.tracking_key).should == 1
+ OneConcurrentRestrictionJob.restriction_queue(OneConcurrentRestrictionJob.tracking_key, :normal).size.should == 3
+
+ end
+
+ it "should run only two concurrent jobs" do
+ 5.times {|i| Resque.enqueue(TwoConcurrentRestrictionJob, i) }
+ 5.times do
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
+ end
+ sleep 0.25
+
+ TwoConcurrentRestrictionJob.total_run_count.should == 2
+ TwoConcurrentRestrictionJob.running_count(TwoConcurrentRestrictionJob.tracking_key).should == 2
+ TwoConcurrentRestrictionJob.restriction_queue(TwoConcurrentRestrictionJob.tracking_key, :normal).size.should == 3
+
+ Process.waitall
+
+ 2.times do
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
+ end
+ sleep 0.25
+
+ TwoConcurrentRestrictionJob.total_run_count.should == 4
+ TwoConcurrentRestrictionJob.running_count(TwoConcurrentRestrictionJob.tracking_key).should == 2
+ TwoConcurrentRestrictionJob.restriction_queue(TwoConcurrentRestrictionJob.tracking_key, :normal).size.should == 1
+
+ end
+
it "should run multiple jobs concurrently" do
7.times {|i| Resque.enqueue(MultipleConcurrentRestrictionJob, i) }
-
+
7.times do
- unless child = fork
- Resque.redis.client.connect
- run_resque_queue('*')
- exit!
- end
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
end
sleep 0.25
-
+
MultipleConcurrentRestrictionJob.total_run_count.should == 4
MultipleConcurrentRestrictionJob.running_count(MultipleConcurrentRestrictionJob.tracking_key).should == 4
MultipleConcurrentRestrictionJob.restriction_queue(MultipleConcurrentRestrictionJob.tracking_key, :normal).size.should == 3
-
+
Process.waitall
-
+
3.times do
- unless child = fork
- Resque.redis.client.connect
- run_resque_queue('*')
- exit!
- end
+ unless child = fork
+ Resque.redis.client.connect
+ run_resque_queue('*')
+ exit!
+ end
end
sleep 0.25
-
+
MultipleConcurrentRestrictionJob.total_run_count.should == 7
MultipleConcurrentRestrictionJob.running_count(MultipleConcurrentRestrictionJob.tracking_key).should == 3
MultipleConcurrentRestrictionJob.restriction_queue(MultipleConcurrentRestrictionJob.tracking_key, :normal).size.should == 0
-
+
Process.waitall
-
+
MultipleConcurrentRestrictionJob.running_count(MultipleConcurrentRestrictionJob.tracking_key).should == 0
MultipleConcurrentRestrictionJob.total_run_count.should == 7
end
View
25 spec/spec_helper.rb
@@ -167,3 +167,28 @@ def self.perform(*args)
sleep 0.5
end
end
+
+class OneConcurrentRestrictionJob
+ extend RunCountHelper
+ extend Resque::Plugins::ConcurrentRestriction
+ concurrent 1
+
+ @queue = 'normal'
+
+ def self.perform(*args)
+ sleep 0.5
+ end
+end
+
+class TwoConcurrentRestrictionJob
+ extend RunCountHelper
+ extend Resque::Plugins::ConcurrentRestriction
+ concurrent 2
+
+ @queue = 'normal'
+
+ def self.perform(*args)
+ sleep 0.5
+ end
+end
+

0 comments on commit baca71a

Please sign in to comment.