mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Merge pull request #1 from Shuttlerock/master
Allow to use custom token for lock
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
rvm:
|
rvm:
|
||||||
- 2.2.0
|
- 2.2.0
|
||||||
|
- 2.3.1
|
||||||
services:
|
services:
|
||||||
- memcached
|
- memcached
|
||||||
- redis-server
|
- redis-server
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ module Suo
|
|||||||
super() # initialize Monitor mixin for thread safety
|
super() # initialize Monitor mixin for thread safety
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock
|
def lock(custom_token = nil)
|
||||||
token = acquire_lock
|
token = acquire_lock(custom_token)
|
||||||
|
|
||||||
if block_given? && token
|
if block_given? && token
|
||||||
begin
|
begin
|
||||||
@@ -95,8 +95,8 @@ module Suo
|
|||||||
|
|
||||||
attr_accessor :retry_count
|
attr_accessor :retry_count
|
||||||
|
|
||||||
def acquire_lock
|
def acquire_lock(token = nil)
|
||||||
token = SecureRandom.base64(16)
|
token ||= SecureRandom.base64(16)
|
||||||
|
|
||||||
retry_with_timeout do
|
retry_with_timeout do
|
||||||
val, cas = get
|
val, cas = get
|
||||||
|
|||||||
@@ -31,6 +31,12 @@ module ClientTests
|
|||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_lock_with_custom_token
|
||||||
|
token = 'foo-bar'
|
||||||
|
lock = @client.lock token
|
||||||
|
assert_equal lock, token
|
||||||
|
end
|
||||||
|
|
||||||
def test_empty_lock_on_invalid_data
|
def test_empty_lock_on_invalid_data
|
||||||
@client.send(:initial_set, "bad value")
|
@client.send(:initial_set, "bad value")
|
||||||
assert_equal false, @client.locked?
|
assert_equal false, @client.locked?
|
||||||
|
|||||||
Reference in New Issue
Block a user