From 05661e143c16ef9b76ae73d8856462b3084bf422 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 5 Oct 2016 13:47:10 +0300 Subject: [PATCH] Allow to use custom token for lock --- .travis.yml | 1 + lib/suo/client/base.rb | 8 ++++---- test/client_test.rb | 6 ++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 97bb599..fb0e1a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: ruby rvm: - 2.2.0 + - 2.3.1 services: - memcached - redis-server diff --git a/lib/suo/client/base.rb b/lib/suo/client/base.rb index 5444631..7f37355 100644 --- a/lib/suo/client/base.rb +++ b/lib/suo/client/base.rb @@ -26,8 +26,8 @@ def initialize(key, options = {}) super() # initialize Monitor mixin for thread safety end - def lock - token = acquire_lock + def lock(custom_token = nil) + token = acquire_lock(custom_token) if block_given? && token begin @@ -95,8 +95,8 @@ def clear attr_accessor :retry_count - def acquire_lock - token = SecureRandom.base64(16) + def acquire_lock(token = nil) + token ||= SecureRandom.base64(16) retry_with_timeout do val, cas = get diff --git a/test/client_test.rb b/test/client_test.rb index 249115d..8b7b3ed 100644 --- a/test/client_test.rb +++ b/test/client_test.rb @@ -31,6 +31,12 @@ def test_single_resource_locking assert_equal false, locked 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 @client.send(:initial_set, "bad value") assert_equal false, @client.locked?