mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce0a4d8d86 | ||
|
|
1fd769eec2 | ||
|
|
37be5ae27b | ||
|
|
a1a226fb59 | ||
|
|
8d7ddaf35a | ||
|
|
1aacc0c1a1 | ||
|
|
8166c6b51d |
@@ -1,3 +1,12 @@
|
|||||||
|
## 0.2.0
|
||||||
|
|
||||||
|
- Refactor class methods into instance methods to simplify implementation.
|
||||||
|
- Increase thread safety with Memcached implementation.
|
||||||
|
|
||||||
|
## 0.1.3
|
||||||
|
|
||||||
|
- Properly throw Suo::LockClientError when the connection itself fails (Memcache server not reachable, etc.)
|
||||||
|
|
||||||
## 0.1.2
|
## 0.1.2
|
||||||
|
|
||||||
- Fix retry_timeout to properly use the full time (was being calculated incorrectly).
|
- Fix retry_timeout to properly use the full time (was being calculated incorrectly).
|
||||||
|
|||||||
22
README.md
22
README.md
@@ -31,12 +31,22 @@ suo.lock("some_key") do
|
|||||||
@puppies.pet!
|
@puppies.pet!
|
||||||
end
|
end
|
||||||
|
|
||||||
2.times do
|
Thread.new { suo.lock("other_key", 2) { puts "One"; sleep 2 } }
|
||||||
Thread.new do
|
Thread.new { suo.lock("other_key", 2) { puts "Two"; sleep 2 } }
|
||||||
# second argument is the number of resources - so this will run twice
|
Thread.new { suo.lock("other_key", 2) { puts "Three" } }
|
||||||
suo.lock("other_key", 2, timeout: 0.5) { puts "Will run twice!" }
|
|
||||||
end
|
# will print "One" "Two", but not "Three", as there are only 2 resources
|
||||||
end
|
|
||||||
|
# custom acquisition timeouts (time to acquire)
|
||||||
|
suo = Suo::Client::Memcached.new(client: some_dalli_client, acquisition_timeout: 1) # in seconds
|
||||||
|
|
||||||
|
# manually locking/unlocking
|
||||||
|
suo.lock("a_key")
|
||||||
|
foo.baz!
|
||||||
|
suo.unlock("a_key")
|
||||||
|
|
||||||
|
# custom stale lock cleanup (cleaning of dead clients)
|
||||||
|
suo = Suo::Client::Redis.new(client: some_redis_client, stale_lock_expiration: 60*5)
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|||||||
@@ -2,58 +2,110 @@ module Suo
|
|||||||
module Client
|
module Client
|
||||||
class Base
|
class Base
|
||||||
DEFAULT_OPTIONS = {
|
DEFAULT_OPTIONS = {
|
||||||
retry_timeout: 0.1,
|
acquisition_timeout: 0.1,
|
||||||
retry_delay: 0.01,
|
acquisition_delay: 0.01,
|
||||||
stale_lock_expiration: 3600
|
stale_lock_expiration: 3600
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
|
attr_accessor :client
|
||||||
|
|
||||||
|
include MonitorMixin
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@options = self.class.merge_defaults(options)
|
fail "Client required" unless options[:client]
|
||||||
|
@options = DEFAULT_OPTIONS.merge(options)
|
||||||
|
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
|
||||||
|
@client = @options[:client]
|
||||||
|
super()
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock(key, resources = 1, options = {})
|
def lock(key, resources = 1)
|
||||||
options = self.class.merge_defaults(@options.merge(options))
|
token = acquire_lock(key, resources)
|
||||||
token = self.class.lock(key, resources, options)
|
|
||||||
|
|
||||||
if token
|
if block_given? && token
|
||||||
begin
|
begin
|
||||||
yield if block_given?
|
yield
|
||||||
ensure
|
ensure
|
||||||
self.class.unlock(key, token, options)
|
unlock(key, token)
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
|
||||||
else
|
else
|
||||||
false
|
token
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def locked?(key, resources = 1)
|
def locked?(key, resources = 1)
|
||||||
self.class.locked?(key, resources, @options)
|
locks(key).size >= resources
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
def locks(key)
|
||||||
def lock(key, resources = 1, options = {})
|
val, _ = get(key)
|
||||||
options = merge_defaults(options)
|
locks = deserialize_locks(val)
|
||||||
acquisition_token = nil
|
|
||||||
token = SecureRandom.base64(16)
|
|
||||||
|
|
||||||
retry_with_timeout(key, options) do
|
locks
|
||||||
val, cas = get(key, options)
|
end
|
||||||
|
|
||||||
|
def refresh(key, acquisition_token)
|
||||||
|
retry_with_timeout(key) do
|
||||||
|
val, cas = get(key)
|
||||||
|
|
||||||
if val.nil?
|
if val.nil?
|
||||||
set_initial(key, options)
|
set_initial(key)
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
locks = deserialize_and_clear_locks(val, options)
|
locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
|
refresh_lock(locks, acquisition_token)
|
||||||
|
|
||||||
|
break if set(key, serialize_locks(locks), cas)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def unlock(key, acquisition_token)
|
||||||
|
return unless acquisition_token
|
||||||
|
|
||||||
|
retry_with_timeout(key) do
|
||||||
|
val, cas = get(key)
|
||||||
|
|
||||||
|
break if val.nil?
|
||||||
|
|
||||||
|
locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
|
acquisition_lock = remove_lock(locks, acquisition_token)
|
||||||
|
|
||||||
|
break unless acquisition_lock
|
||||||
|
break if set(key, serialize_locks(locks), cas)
|
||||||
|
end
|
||||||
|
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
|
||||||
|
# ignore - assume success due to optimistic locking
|
||||||
|
end
|
||||||
|
|
||||||
|
def clear(key) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
|
fail NotImplementedError
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def acquire_lock(key, resources = 1)
|
||||||
|
acquisition_token = nil
|
||||||
|
token = SecureRandom.base64(16)
|
||||||
|
|
||||||
|
retry_with_timeout(key) do
|
||||||
|
val, cas = get(key)
|
||||||
|
|
||||||
|
if val.nil?
|
||||||
|
set_initial(key)
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
if locks.size < resources
|
if locks.size < resources
|
||||||
add_lock(locks, token)
|
add_lock(locks, token)
|
||||||
|
|
||||||
newval = serialize_locks(locks)
|
newval = serialize_locks(locks)
|
||||||
|
|
||||||
if set(key, newval, cas, options)
|
if set(key, newval, cas)
|
||||||
acquisition_token = token
|
acquisition_token = token
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -63,115 +115,45 @@ module Suo
|
|||||||
acquisition_token
|
acquisition_token
|
||||||
end
|
end
|
||||||
|
|
||||||
def locked?(key, resources = 1, options = {})
|
def get(key) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
locks(key, options).size >= resources
|
|
||||||
end
|
|
||||||
|
|
||||||
def locks(key, options)
|
|
||||||
options = merge_defaults(options)
|
|
||||||
val, _ = get(key, options)
|
|
||||||
locks = deserialize_locks(val)
|
|
||||||
|
|
||||||
locks
|
|
||||||
end
|
|
||||||
|
|
||||||
def refresh(key, acquisition_token, options = {})
|
|
||||||
options = merge_defaults(options)
|
|
||||||
|
|
||||||
retry_with_timeout(key, options) do
|
|
||||||
val, cas = get(key, options)
|
|
||||||
|
|
||||||
if val.nil?
|
|
||||||
set_initial(key, options)
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
locks = deserialize_and_clear_locks(val, options)
|
|
||||||
|
|
||||||
refresh_lock(locks, acquisition_token)
|
|
||||||
|
|
||||||
break if set(key, serialize_locks(locks), cas, options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def unlock(key, acquisition_token, options = {})
|
|
||||||
options = merge_defaults(options)
|
|
||||||
|
|
||||||
return unless acquisition_token
|
|
||||||
|
|
||||||
retry_with_timeout(key, options) do
|
|
||||||
val, cas = get(key, options)
|
|
||||||
|
|
||||||
break if val.nil?
|
|
||||||
|
|
||||||
locks = deserialize_and_clear_locks(val, options)
|
|
||||||
|
|
||||||
acquisition_lock = remove_lock(locks, acquisition_token)
|
|
||||||
|
|
||||||
break unless acquisition_lock
|
|
||||||
break if set(key, serialize_locks(locks), cas, options)
|
|
||||||
end
|
|
||||||
rescue FailedToAcquireLock => _ # rubocop:disable Lint/HandleExceptions
|
|
||||||
# ignore - assume success due to optimistic locking
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear(key, options = {}) # rubocop:disable Lint/UnusedMethodArgument
|
|
||||||
fail NotImplementedError
|
fail NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge_defaults(options = {})
|
def set(key, newval, oldval) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
options = self::DEFAULT_OPTIONS.merge(options)
|
|
||||||
|
|
||||||
fail "Client required" unless options[:client]
|
|
||||||
|
|
||||||
options[:retry_count] = (options[:retry_timeout] / options[:retry_delay].to_f).ceil
|
|
||||||
|
|
||||||
options
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def get(key, options) # rubocop:disable Lint/UnusedMethodArgument
|
|
||||||
fail NotImplementedError
|
fail NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, oldval, options) # rubocop:disable Lint/UnusedMethodArgument
|
def set_initial(key) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
fail NotImplementedError
|
fail NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_initial(key, options) # rubocop:disable Lint/UnusedMethodArgument
|
def synchronize(key) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
fail NotImplementedError
|
mon_synchronize { yield }
|
||||||
end
|
end
|
||||||
|
|
||||||
def synchronize(key, options)
|
def retry_with_timeout(key)
|
||||||
yield(key, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def retry_with_timeout(key, options)
|
|
||||||
start = Time.now.to_f
|
start = Time.now.to_f
|
||||||
|
|
||||||
options[:retry_count].times do
|
@retry_count.times do
|
||||||
if options[:retry_timeout]
|
|
||||||
now = Time.now.to_f
|
now = Time.now.to_f
|
||||||
break if now - start > options[:retry_timeout]
|
break if now - start > @options[:acquisition_timeout]
|
||||||
end
|
|
||||||
|
|
||||||
synchronize(key, options) do
|
synchronize(key) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
|
||||||
sleep(rand(options[:retry_delay] * 1000).to_f / 1000)
|
sleep(rand(@options[:acquisition_delay] * 1000).to_f / 1000)
|
||||||
end
|
end
|
||||||
rescue => _
|
rescue => _
|
||||||
raise FailedToAcquireLock
|
raise LockClientError
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_locks(locks)
|
def serialize_locks(locks)
|
||||||
MessagePack.pack(locks.map { |time, token| [time.to_f, token] })
|
MessagePack.pack(locks.map { |time, token| [time.to_f, token] })
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_and_clear_locks(val, options)
|
def deserialize_and_clear_locks(val)
|
||||||
clear_expired_locks(deserialize_locks(val), options)
|
clear_expired_locks(deserialize_locks(val))
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_locks(val)
|
def deserialize_locks(val)
|
||||||
@@ -184,8 +166,8 @@ module Suo
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_expired_locks(locks, options)
|
def clear_expired_locks(locks)
|
||||||
expired = Time.now - options[:stale_lock_expiration]
|
expired = Time.now - @options[:stale_lock_expiration]
|
||||||
locks.reject { |time, _| time < expired }
|
locks.reject { |time, _| time < expired }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -205,4 +187,3 @@ module Suo
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
module Suo
|
|
||||||
module Client
|
|
||||||
module Errors
|
|
||||||
class FailedToAcquireLock < StandardError; end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -6,25 +6,22 @@ module Suo
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
def clear(key)
|
||||||
def clear(key, options = {})
|
@client.delete(key)
|
||||||
options = merge_defaults(options)
|
|
||||||
options[:client].delete(key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get(key, options)
|
def get(key)
|
||||||
options[:client].get_cas(key)
|
@client.get_cas(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, cas, options)
|
def set(key, newval, cas)
|
||||||
options[:client].set_cas(key, newval, cas)
|
@client.set_cas(key, newval, cas)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_initial(key, options)
|
def set_initial(key)
|
||||||
options[:client].set(key, "")
|
@client.set(key, "")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,37 +6,34 @@ module Suo
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
def clear(key)
|
||||||
def clear(key, options = {})
|
@client.del(key)
|
||||||
options = merge_defaults(options)
|
|
||||||
options[:client].del(key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get(key, options)
|
def get(key)
|
||||||
[options[:client].get(key), nil]
|
[@client.get(key), nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, _, options)
|
def set(key, newval, _)
|
||||||
ret = options[:client].multi do |multi|
|
ret = @client.multi do |multi|
|
||||||
multi.set(key, newval)
|
multi.set(key, newval)
|
||||||
end
|
end
|
||||||
|
|
||||||
ret[0] == "OK"
|
ret[0] == "OK"
|
||||||
end
|
end
|
||||||
|
|
||||||
def synchronize(key, options)
|
def synchronize(key)
|
||||||
options[:client].watch(key) do
|
@client.watch(key) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
options[:client].unwatch
|
@client.unwatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_initial(key, options)
|
def set_initial(key)
|
||||||
options[:client].set(key, "")
|
@client.set(key, "")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ require "redis"
|
|||||||
|
|
||||||
require "msgpack"
|
require "msgpack"
|
||||||
|
|
||||||
require "suo/client/errors"
|
require "suo/errors"
|
||||||
require "suo/client/base"
|
require "suo/client/base"
|
||||||
require "suo/client/memcached"
|
require "suo/client/memcached"
|
||||||
require "suo/client/redis"
|
require "suo/client/redis"
|
||||||
|
|||||||
3
lib/suo/errors.rb
Normal file
3
lib/suo/errors.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module Suo
|
||||||
|
class LockClientError < StandardError; end
|
||||||
|
end
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
module Suo
|
module Suo
|
||||||
VERSION = "0.1.2"
|
VERSION = "0.2.0"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|||||||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
||||||
spec.require_paths = ["lib"]
|
spec.require_paths = ["lib"]
|
||||||
|
|
||||||
|
spec.required_ruby_version = "~> 2.0"
|
||||||
|
|
||||||
spec.add_dependency "dalli"
|
spec.add_dependency "dalli"
|
||||||
spec.add_dependency "redis"
|
spec.add_dependency "redis"
|
||||||
spec.add_dependency "msgpack"
|
spec.add_dependency "msgpack"
|
||||||
|
|||||||
@@ -3,56 +3,55 @@ require "test_helper"
|
|||||||
TEST_KEY = "suo_test_key".freeze
|
TEST_KEY = "suo_test_key".freeze
|
||||||
|
|
||||||
module ClientTests
|
module ClientTests
|
||||||
def test_requires_client
|
def test_throws_failed_error_on_bad_client
|
||||||
exception = assert_raises(RuntimeError) do
|
assert_raises(Suo::LockClientError) do
|
||||||
@klass.lock(TEST_KEY, 1)
|
client = @client.class.new(client: {})
|
||||||
|
client.lock(TEST_KEY, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Client required", exception.message
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_class_single_resource_locking
|
def test_class_single_resource_locking
|
||||||
lock1 = @klass.lock(TEST_KEY, 1, client: @klass_client)
|
lock1 = @client.lock(TEST_KEY, 1)
|
||||||
refute_nil lock1
|
refute_nil lock1
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 1)
|
||||||
assert_equal true, locked
|
assert_equal true, locked
|
||||||
|
|
||||||
lock2 = @klass.lock(TEST_KEY, 1, client: @klass_client)
|
lock2 = @client.lock(TEST_KEY, 1)
|
||||||
assert_nil lock2
|
assert_nil lock2
|
||||||
|
|
||||||
@klass.unlock(TEST_KEY, lock1, client: @klass_client)
|
@client.unlock(TEST_KEY, lock1)
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 1)
|
||||||
|
|
||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_class_multiple_resource_locking
|
def test_class_multiple_resource_locking
|
||||||
lock1 = @klass.lock(TEST_KEY, 2, client: @klass_client)
|
lock1 = @client.lock(TEST_KEY, 2)
|
||||||
refute_nil lock1
|
refute_nil lock1
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 2, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 2)
|
||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
|
|
||||||
lock2 = @klass.lock(TEST_KEY, 2, client: @klass_client)
|
lock2 = @client.lock(TEST_KEY, 2)
|
||||||
refute_nil lock2
|
refute_nil lock2
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 2, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 2)
|
||||||
assert_equal true, locked
|
assert_equal true, locked
|
||||||
|
|
||||||
@klass.unlock(TEST_KEY, lock1, client: @klass_client)
|
@client.unlock(TEST_KEY, lock1)
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 1)
|
||||||
assert_equal true, locked
|
assert_equal true, locked
|
||||||
|
|
||||||
@klass.unlock(TEST_KEY, lock2, client: @klass_client)
|
@client.unlock(TEST_KEY, lock2)
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 1)
|
||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_instance_single_resource_locking
|
def test_block_single_resource_locking
|
||||||
locked = false
|
locked = false
|
||||||
|
|
||||||
@client.lock(TEST_KEY, 1) { locked = true }
|
@client.lock(TEST_KEY, 1) { locked = true }
|
||||||
@@ -60,23 +59,46 @@ module ClientTests
|
|||||||
assert_equal true, locked
|
assert_equal true, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_instance_unlocks_on_exception
|
def test_block_unlocks_on_exception
|
||||||
assert_raises(RuntimeError) do
|
assert_raises(RuntimeError) do
|
||||||
@client.lock(TEST_KEY, 1) { fail "Test" }
|
@client.lock(TEST_KEY, 1) { fail "Test" }
|
||||||
end
|
end
|
||||||
|
|
||||||
locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
|
locked = @client.locked?(TEST_KEY, 1)
|
||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_readme_example
|
||||||
|
output = Queue.new
|
||||||
|
threads = []
|
||||||
|
|
||||||
|
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "One"; sleep 2 } }
|
||||||
|
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Two"; sleep 2 } }
|
||||||
|
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Three" } }
|
||||||
|
|
||||||
|
threads.map(&:join)
|
||||||
|
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
ret << output.pop
|
||||||
|
ret << output.pop
|
||||||
|
|
||||||
|
ret.sort!
|
||||||
|
|
||||||
|
assert_equal 0, output.size
|
||||||
|
assert_equal ["One", "Two"], ret
|
||||||
|
end
|
||||||
|
|
||||||
def test_instance_multiple_resource_locking
|
def test_instance_multiple_resource_locking
|
||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
|
client = @client.class.new(acquisition_timeout: 0.9, client: @client.client)
|
||||||
|
|
||||||
100.times.map do |i|
|
100.times.map do |i|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
success = @client.lock(TEST_KEY, 50, retry_timeout: 0.5) do
|
success = @client.lock(TEST_KEY, 50) do
|
||||||
sleep(2)
|
sleep(3)
|
||||||
success_counter << i
|
success_counter << i
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -92,9 +114,11 @@ module ClientTests
|
|||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
|
client = @client.class.new(acquisition_timeout: 3, client: @client.client)
|
||||||
|
|
||||||
100.times.map do |i|
|
100.times.map do |i|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
success = @client.lock(TEST_KEY, 50, retry_timeout: 2) do
|
success = client.lock(TEST_KEY, 50) do
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
success_counter << i
|
success_counter << i
|
||||||
end
|
end
|
||||||
@@ -110,12 +134,12 @@ end
|
|||||||
|
|
||||||
class TestBaseClient < Minitest::Test
|
class TestBaseClient < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@klass = Suo::Client::Base
|
@client = Suo::Client::Base.new(client: {})
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_not_implemented
|
def test_not_implemented
|
||||||
assert_raises(NotImplementedError) do
|
assert_raises(NotImplementedError) do
|
||||||
@klass.send(:get, TEST_KEY, {})
|
@client.send(:get, TEST_KEY)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -124,13 +148,12 @@ class TestMemcachedClient < Minitest::Test
|
|||||||
include ClientTests
|
include ClientTests
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@klass = Suo::Client::Memcached
|
@dalli = Dalli::Client.new("127.0.0.1:11211")
|
||||||
@client = @klass.new
|
@client = Suo::Client::Memcached.new
|
||||||
@klass_client = Dalli::Client.new("127.0.0.1:11211")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
@klass_client.delete(TEST_KEY)
|
@dalli.delete(TEST_KEY)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -138,13 +161,12 @@ class TestRedisClient < Minitest::Test
|
|||||||
include ClientTests
|
include ClientTests
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@klass = Suo::Client::Redis
|
@redis = Redis.new
|
||||||
@client = @klass.new
|
@client = Suo::Client::Redis.new
|
||||||
@klass_client = Redis.new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
@klass_client.del(TEST_KEY)
|
@redis.del(TEST_KEY)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user