test/lib/redis_lock_test.rb in sidekiq-lock-0.5.0 vs test/lib/redis_lock_test.rb in sidekiq-lock-0.6.0

- old
+ new

@@ -2,16 +2,20 @@ module Sidekiq module Lock describe RedisLock do before do - Sidekiq.redis = REDIS + if Sidekiq::VERSION >= '7' + Sidekiq.configure_client do |config| + config.redis = { url: REDIS_URL } + end + else + Sidekiq.redis = REDIS + end Sidekiq.redis { |c| c.flushdb } end - let(:args) { [{'timeout' => 100, 'name' => 'test-lock'}, []] } - it "raises an error on missing timeout&name values" do assert_raises ArgumentError do RedisLock.new({},[]) end end @@ -50,49 +54,62 @@ assert_equal 'hello-sidekiq', redis("get", lock.name) lock.release! end it "can acquire a lock" do - lock = RedisLock.new(*args) + lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) assert lock.acquire! end - it "cannot aquire lock if it's already taken by other process/thread" do - faster_lock = RedisLock.new(*args) + it "sets proper lock value on first and second acquire" do + lock = RedisLock.new({'timeout' => 1000, 'name' => 'test-lock', 'value' => 'lock value'}, []) + assert lock.acquire! + assert_equal 'lock value', redis("get", lock.name) + assert lock.release! + # at this point script should be used from evalsha + assert lock.acquire! + assert_equal 'lock value', redis("get", lock.name) + + redis("script", "flush") + assert lock.acquire! + assert_equal 'lock value', redis("get", lock.name) + end + + it "cannot acquire lock if it's already taken by other process/thread" do + faster_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) assert faster_lock.acquire! - slower_lock = RedisLock.new(*args) + slower_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) refute slower_lock.acquire! end it "releases taken lock" do - lock = RedisLock.new(*args) + lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) lock.acquire! assert redis("get", "test-lock") - lock.release! + assert lock.release! assert_nil redis("get", "test-lock") end it "releases lock taken by another process without deleting lock key" do - lock = RedisLock.new(*args) + lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) lock.acquire! lock_value = redis("get", "test-lock") assert lock_value sleep 0.11 # timeout lock - new_lock = RedisLock.new(*args) + new_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, []) new_lock.acquire! new_lock_value = redis("get", "test-lock") - lock.release! + refute lock.release! assert_equal new_lock_value, redis("get", "test-lock") end it "releases taken lock" do - custom_args = [args.first.merge('value' => 'custom_value'), []] - lock = RedisLock.new(*custom_args) + lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock', 'value' => 'custom_value'}, []) lock.acquire! assert redis("get", "test-lock") lock.release! assert_nil redis("get", "test-lock")