forked from collectiveidea/delayed_job
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class SimpleJob | ||
cattr_accessor :runs; self.runs = 0 | ||
def perform; @@runs += 1; end | ||
end | ||
|
||
class ErrorJob | ||
cattr_accessor :runs; self.runs = 0 | ||
def perform; raise 'did not work'; end | ||
end | ||
|
||
class LongRunningJob | ||
def perform; sleep 250; end | ||
end | ||
|
||
module M | ||
class ModuleJob | ||
cattr_accessor :runs; self.runs = 0 | ||
def perform; @@runs += 1; end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
require File.dirname(__FILE__) + '/database' | ||
require File.dirname(__FILE__) + '/sample_jobs' | ||
|
||
describe Delayed::Worker do | ||
def job_create(opts = {}) | ||
Delayed::Job.create(opts.merge(:payload_object => SimpleJob.new)) | ||
end | ||
|
||
before(:each) do | ||
@worker = Delayed::Worker.new(:max_priority => nil, :min_priority => nil) | ||
|
||
Delayed::Job.delete_all | ||
|
||
SimpleJob.runs = 0 | ||
end | ||
|
||
context "worker prioritization" do | ||
before(:each) do | ||
@worker = Delayed::Worker.new(:max_priority => 5, :min_priority => -5) | ||
end | ||
|
||
it "should only work_off jobs that are >= min_priority" do | ||
SimpleJob.runs.should == 0 | ||
|
||
job_create(:priority => -10) | ||
job_create(:priority => 0) | ||
@worker.work_off | ||
|
||
SimpleJob.runs.should == 1 | ||
end | ||
|
||
it "should only work_off jobs that are <= max_priority" do | ||
SimpleJob.runs.should == 0 | ||
|
||
job_create(:priority => 10) | ||
job_create(:priority => 0) | ||
|
||
@worker.work_off | ||
|
||
SimpleJob.runs.should == 1 | ||
end | ||
end | ||
|
||
context "while running alongside other workers that locked jobs, it" do | ||
before(:each) do | ||
Delayed::Job.worker_name = 'worker1' | ||
job_create(:locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) | ||
job_create(:locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) | ||
job_create | ||
job_create(:locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) | ||
end | ||
|
||
it "should ingore locked jobs from other workers" do | ||
Delayed::Job.worker_name = 'worker3' | ||
SimpleJob.runs.should == 0 | ||
@worker.work_off | ||
SimpleJob.runs.should == 1 # runs the one open job | ||
end | ||
|
||
it "should find our own jobs regardless of locks" do | ||
Delayed::Job.worker_name = 'worker1' | ||
SimpleJob.runs.should == 0 | ||
@worker.work_off | ||
SimpleJob.runs.should == 3 # runs open job plus worker1 jobs that were already locked | ||
end | ||
end | ||
|
||
context "while running with locked and expired jobs, it" do | ||
before(:each) do | ||
Delayed::Job.worker_name = 'worker1' | ||
exp_time = Delayed::Job.db_time_now - (1.minutes + Delayed::Job::max_run_time) | ||
job_create(:locked_by => 'worker1', :locked_at => exp_time) | ||
job_create(:locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) | ||
job_create | ||
job_create(:locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) | ||
end | ||
|
||
it "should only find unlocked and expired jobs" do | ||
Delayed::Job.worker_name = 'worker3' | ||
SimpleJob.runs.should == 0 | ||
@worker.work_off | ||
SimpleJob.runs.should == 2 # runs the one open job and one expired job | ||
end | ||
|
||
it "should ignore locks when finding our own jobs" do | ||
Delayed::Job.worker_name = 'worker1' | ||
SimpleJob.runs.should == 0 | ||
@worker.work_off | ||
SimpleJob.runs.should == 3 # runs open job plus worker1 jobs | ||
# This is useful in the case of a crash/restart on worker1, but make sure multiple workers on the same host have unique names! | ||
end | ||
|
||
end | ||
|
||
end |