Skip to content

Commit

Permalink
Merge pull request #2 from wallace/master
Browse files Browse the repository at this point in the history
Sync w/wallace:master
  • Loading branch information
Lance Woodson committed Aug 13, 2014
2 parents bf6efd1 + 65912f7 commit dcbcfe2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/resque-lonely_job/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Resque
module Plugins
module LonelyJob
VERSION = "1.0.2"
VERSION = "1.0.3"
end
end
end
2 changes: 1 addition & 1 deletion resque-lonely_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'resque', '>= 1.2'
gem.add_development_dependency 'mock_redis'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'rspec', '>= 3.0'
gem.add_development_dependency 'timecop'

gem.description = <<desc
Expand Down
54 changes: 27 additions & 27 deletions spec/lib/lonely_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ def self.perform(account_id, *args); end

describe ".can_lock_queue?" do
it 'can lock a queue' do
SerialJob.can_lock_queue?(:serial_work).should be_true
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)
end

it 'cannot lock an already locked queue' do
SerialJob.can_lock_queue?(:serial_work).should be_true
SerialJob.can_lock_queue?(:serial_work).should be_false
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(false)
end

it 'cannot lock a queue with active lock' do
SerialJob.can_lock_queue?(:serial_work).should be_true
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)
Timecop.travel(Date.today + 1) do
SerialJob.can_lock_queue?(:serial_work).should be_false
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(false)
end
end

it 'can relock a queue with expired lock' do
SerialJob.can_lock_queue?(:serial_work).should be_true
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)

Timecop.travel(Date.today + 10) do
SerialJob.can_lock_queue?(:serial_work).should be_true
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)
end
end

it 'solves race condition with getset' do
SerialJob.can_lock_queue?(:serial_work).should be_true
expect(SerialJob.can_lock_queue?(:serial_work)).to eql(true)

Timecop.travel(Date.today + 10) do
threads = (1..10).to_a.map {
Expand All @@ -60,7 +60,7 @@ def self.perform(account_id, *args); end

# Only one worker should acquire lock
locks = threads.map {|t| t.join; t[:locked] }
locks.count(true).should == 1
expect(locks.count(true)).to eql(1)
end
end
end
Expand All @@ -71,10 +71,10 @@ def self.perform(account_id, *args); end
job = Resque::Job.new(:serial_work, { 'class' => 'SerialJob', 'args' => %w[account_one job_one] })

# job is the first SerialJob to run so it can lock the queue and perform
SerialJob.should_receive(:can_lock_queue?).and_return(true)
expect(SerialJob).to receive(:can_lock_queue?).and_return(true)

# but it should also clean up after itself
SerialJob.should_receive(:unlock_queue)
expect(SerialJob).to receive(:unlock_queue)

job.perform
end
Expand All @@ -83,17 +83,17 @@ def self.perform(account_id, *args); end
job = Resque::Job.new(:serial_work, { 'class' => 'SerialJob', 'args' => %w[account_one job_one] })

# job is the first SerialJob to run so it can lock the queue and perform
SerialJob.should_receive(:can_lock_queue?).and_return(true)
expect(SerialJob).to receive(:can_lock_queue?).and_return(true)

# but we have a catastrophic job failure
SerialJob.should_receive(:perform).and_raise(Exception)
expect(SerialJob).to receive(:perform).and_raise(Exception)

# and still it should clean up after itself
SerialJob.should_receive(:unlock_queue)
expect(SerialJob).to receive(:unlock_queue)

# unfortunately, the job will be lost but resque doesn't guarantee jobs
# aren't lost
-> { job.perform }.should raise_error(Exception)
expect { job.perform }.to raise_error(Exception)
end

it 'should place self at the end of the queue if unable to acquire the lock' do
Expand All @@ -102,15 +102,15 @@ def self.perform(account_id, *args); end
Resque::Job.create(:serial_work, 'SerialJob', job1_payload)
Resque::Job.create(:serial_work, 'SerialJob', job2_payload)

SerialJob.should_receive(:can_lock_queue?).and_return(false)
expect(SerialJob).to receive(:can_lock_queue?).and_return(false)

# perform returns false when DontPerform exception is raised in
# before_perform callback
job1 = Resque.reserve(:serial_work)
job1.perform.should be_false
expect(job1.perform).to eql(false)

first_queue_element = Resque.reserve(:serial_work)
first_queue_element.payload["args"].should == [job2_payload]
expect(first_queue_element.payload["args"]).to eql([job2_payload])
end
end

Expand All @@ -119,10 +119,10 @@ def self.perform(account_id, *args); end
job = Resque::Job.new(:serial_work, { 'class' => 'SerialJobWithCustomRedisKey', 'args' => %w[account_one job_one] })

# job is the first SerialJobWithCustomRedisKey to run so it can lock the queue and perform
SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(true)
expect(SerialJobWithCustomRedisKey).to receive(:can_lock_queue?).and_return(true)

# but it should also clean up after itself
SerialJobWithCustomRedisKey.should_receive(:unlock_queue)
expect(SerialJobWithCustomRedisKey).to receive(:unlock_queue)

job.perform
end
Expand All @@ -131,17 +131,17 @@ def self.perform(account_id, *args); end
job = Resque::Job.new(:serial_work, { 'class' => 'SerialJobWithCustomRedisKey', 'args' => %w[account_one job_one] })

# job is the first SerialJobWithCustomRedisKey to run so it can lock the queue and perform
SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(true)
expect(SerialJobWithCustomRedisKey).to receive(:can_lock_queue?).and_return(true)

# but we have a catastrophic job failure
SerialJobWithCustomRedisKey.should_receive(:perform).and_raise(Exception)
expect(SerialJobWithCustomRedisKey).to receive(:perform).and_raise(Exception)

# and still it should clean up after itself
SerialJobWithCustomRedisKey.should_receive(:unlock_queue)
expect(SerialJobWithCustomRedisKey).to receive(:unlock_queue)

# unfortunately, the job will be lost but resque doesn't guarantee jobs
# aren't lost
-> { job.perform }.should raise_error(Exception)
expect { job.perform }.to raise_error(Exception)
end

it 'should place self at the end of the queue if unable to acquire the lock' do
Expand All @@ -150,15 +150,15 @@ def self.perform(account_id, *args); end
Resque::Job.create(:serial_work, 'SerialJobWithCustomRedisKey', job1_payload)
Resque::Job.create(:serial_work, 'SerialJobWithCustomRedisKey', job2_payload)

SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(false)
expect(SerialJobWithCustomRedisKey).to receive(:can_lock_queue?).and_return(false)

# perform returns false when DontPerform exception is raised in
# before_perform callback
job1 = Resque.reserve(:serial_work)
job1.perform.should be_false
expect(job1.perform).to eql(false)

first_queue_element = Resque.reserve(:serial_work)
first_queue_element.payload["args"].should == [job2_payload]
expect(first_queue_element.payload["args"]).to eql([job2_payload])
end
end
end
Expand Down

0 comments on commit dcbcfe2

Please sign in to comment.