Skip to content

Commit

Permalink
Added spec to test setnx/getset unlock expired lock behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Jan 23, 2013
1 parent f8791b1 commit 6bfac42
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions resque-lonely_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'debugger'
gem.add_development_dependency 'timecop'

gem.description = <<desc
Ensures that for a given queue, only one worker is working on a job at any given time.
Expand Down
31 changes: 31 additions & 0 deletions spec/lib/lonely_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ def self.perform(account_id, *args); end
SerialJob.can_lock_queue?(:serial_work).should be_true
SerialJob.can_lock_queue?(:serial_work).should be_false
end

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

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

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

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

Timecop.travel(Date.today + 10) do
threads = (1..10).to_a.map {
Thread.new {
Thread.current[:locked] = SerialJob.can_lock_queue?(:serial_work)
}
}

# Only one worker should acquire lock
locks = threads.map {|t| t.join; t[:locked] }
locks.count(true).should == 1
end
end
end

describe ".perform" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'mock_redis'
require 'resque'
require 'resque-lonely_job'
require 'timecop'

RSpec.configure do |config|
config.before(:suite) do
Expand Down

0 comments on commit 6bfac42

Please sign in to comment.