2013-08-13 06:46:15 -04:00
|
|
|
require_relative 'helper'
|
|
|
|
|
|
|
|
class TestLock < Sidetiq::TestCase
|
|
|
|
def test_locking
|
|
|
|
lock_name = SecureRandom.hex(8)
|
|
|
|
key = SecureRandom.hex(8)
|
|
|
|
|
|
|
|
Sidekiq.redis do |redis|
|
|
|
|
redis.set(key, 0)
|
|
|
|
|
|
|
|
5.times.map do
|
|
|
|
Thread.start do
|
|
|
|
locked(lock_name) do |r|
|
|
|
|
sleep 0.1
|
|
|
|
r.incr(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.each(&:join)
|
|
|
|
|
|
|
|
assert_equal "1", redis.get(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-16 08:49:56 -04:00
|
|
|
def test_lock_sets_correct_meta_data
|
|
|
|
key = SecureRandom.hex(8)
|
|
|
|
internal_key = "sidetiq:#{key}:lock"
|
|
|
|
|
|
|
|
locked(key) do |redis|
|
|
|
|
json = redis.get(internal_key)
|
|
|
|
md = Sidetiq::Lock::MetaData.from_json(json)
|
|
|
|
|
|
|
|
assert_equal Sidetiq::Lock::MetaData::OWNER, md.owner
|
|
|
|
assert_equal internal_key, md.key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-13 06:46:15 -04:00
|
|
|
def locked(lock_name)
|
2013-09-16 08:49:56 -04:00
|
|
|
Sidetiq::Lock::Redis.new(lock_name).synchronize do |redis|
|
2013-08-13 06:46:15 -04:00
|
|
|
yield redis
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|