Skip to content

Commit

Permalink
Merge pull request resque#371 from resque/299-update-resilient-lock-l…
Browse files Browse the repository at this point in the history
…ua-scripts-with-timeout

Ensuring lock and acquire lua scripts are refreshed on timeout change
  • Loading branch information
ksinkar committed Feb 18, 2014
2 parents 6613982 + 85de60e commit 37be53d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/resque/scheduler/lock/resilient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ def locked?
).to_i == 1
end

def timeout=(t)
@timeout = t if locked?
def timeout=(seconds)
if locked?
@timeout = seconds
@locked_sha = nil
@acquire_sha = nil
end
@timeout
end

private
Expand Down
31 changes: 30 additions & 1 deletion test/scheduler_locking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,50 @@ def lock_is_not_held(lock)
test 'setting the lock timeout changes the key TTL if we hold it' do
@lock.acquire!

@lock.stubs(:locked?).returns(true)
@lock.timeout = 120
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 100]

@lock.stubs(:locked?).returns(true)
@lock.timeout = 180
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 120]
end

test 'setting the lock timeout is a noop if not held' do
test 'setting lock timeout is a noop if not held' do
@lock.acquire!
@lock.timeout = 100
@lock.stubs(:locked?).returns(false)
@lock.timeout = 120
assert_equal 100, @lock.timeout
end

test 'setting lock timeout nils out lock script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@locked_sha)
end

test 'setting lock timeout does not nil out lock script if not held' do
@lock.acquire!
@lock.locked?
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@locked_sha)
end

test 'setting lock timeout nils out acquire script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@acquire_sha)
end

test 'setting lock timeout does not nil out acquire script if not held' do
@lock.acquire!
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@acquire_sha)
end
end
end

0 comments on commit 37be53d

Please sign in to comment.