From 85de60e6852a422a5e4f43a69a08c8413646ec0c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 18 Feb 2014 10:51:04 -0500 Subject: [PATCH] Ensuring lock and acquire lua scripts are refreshed on timeout change --- lib/resque/scheduler/lock/resilient.rb | 9 ++++++-- test/scheduler_locking_test.rb | 31 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/resque/scheduler/lock/resilient.rb b/lib/resque/scheduler/lock/resilient.rb index 920bfca8..4facc2f7 100644 --- a/lib/resque/scheduler/lock/resilient.rb +++ b/lib/resque/scheduler/lock/resilient.rb @@ -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 diff --git a/test/scheduler_locking_test.rb b/test/scheduler_locking_test.rb index 33e20d75..c167e556 100644 --- a/test/scheduler_locking_test.rb +++ b/test/scheduler_locking_test.rb @@ -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