26 Commits

Author SHA1 Message Date
Nick Elser
323caaee9b update readme 2015-04-15 23:14:06 -07:00
Nick Elser
745d49466f release v0.3.0 2015-04-15 23:11:06 -07:00
Nick Elser
161d50deb9 update tests for new interface 2015-04-15 23:10:34 -07:00
Nick Elser
81e4a3e143 dramatically simpify interface by forcing key at initialization 2015-04-15 23:10:21 -07:00
Nick Elser
2960c14a4d use same language for summary + description 2015-04-13 22:28:42 -07:00
Nick Elser
308e918e60 on second thought, remove confusing language 2015-04-13 22:20:44 -07:00
Nick Elser
aaee69a2df release v0.2.3 2015-04-13 22:15:14 -07:00
Nick Elser
c6d1c29ada add additional deadlock tests 2015-04-13 22:13:15 -07:00
Nick Elser
14e442e99d remove semaphore language and clarify language 2015-04-13 22:13:07 -07:00
Nick Elser
498073b92e tiny code style improvements 2015-04-13 21:55:34 -07:00
Nick Elser
155a3ac40c release v0.2.2 2015-04-13 21:45:47 -07:00
Nick Elser
10d3ab09cf handle invalid lock data 2015-04-13 21:40:54 -07:00
Nick Elser
2724ec6d9d add another test case for the nil refresh case 2015-04-13 21:32:01 -07:00
Nick Elser
185327f59c fix documentation and add another test for refresh 2015-04-13 21:21:45 -07:00
Nick Elser
c8a972da31 more tests for (still not great) refresh method 2015-04-13 20:29:23 -07:00
Nick Elser
7591c08a28 avoid name collision for locks method 2015-04-13 20:29:03 -07:00
Nick Elser
1dee338e16 slightly more coverage 2015-04-13 19:42:50 -07:00
Nick Elser
900c723043 only report to codeclimate when credentials passed in 2015-04-13 19:37:58 -07:00
Nick Elser
6e2afdf80a add tests for refresh and slight refactor 2015-04-13 19:37:49 -07:00
Nick Elser
49a9757d44 fix refresh token 2015-04-13 19:37:34 -07:00
Nick Elser
754d7d8faf report test coverage 2015-04-13 10:12:26 -07:00
Nick Elser
857fc63378 release 0.2.1 2015-04-12 23:45:09 -07:00
Nick Elser
bb6762bbc6 ret is not always an array 2015-04-12 23:45:00 -07:00
Nick Elser
f0977c89f2 remove redundant require file 2015-04-12 23:03:54 -07:00
Nick Elser
4d5c96309f some style fixes 2015-04-12 22:54:53 -07:00
Nick Elser
8d6061b137 rename some tests, fix rare edge condition 2015-04-12 22:49:02 -07:00
11 changed files with 418 additions and 157 deletions

View File

@@ -1,3 +1,22 @@
## 0.3.0
- Dramatically simplify the interface by forcing clients to specify the key & resources at lock initialization instead of every method call.
## 0.2.3
- Clarify documentation further with respect to semaphores.
## 0.2.2
- Fix bug with refresh - typo would've prevented real use.
- Clean up code.
- Improve documentation a bit.
- 100% test coverage.
## 0.2.1
- Fix bug when dealing with real-world Redis error conditions.
## 0.2.0 ## 0.2.0
- Refactor class methods into instance methods to simplify implementation. - Refactor class methods into instance methods to simplify implementation.
@@ -14,7 +33,7 @@
## 0.1.1 ## 0.1.1
- Use [MessagePack](https://github.com/msgpack/msgpack-ruby) for semaphore serialization. - Use [MessagePack](https://github.com/msgpack/msgpack-ruby) for lock serialization.
## 0.1.0 ## 0.1.0

View File

@@ -1,8 +1,8 @@
# Suo [![Build Status](https://travis-ci.org/nickelser/suo.svg?branch=master)](https://travis-ci.org/nickelser/suo) [![Gem Version](https://badge.fury.io/rb/suo.svg)](http://badge.fury.io/rb/suo) # Suo [![Build Status](https://travis-ci.org/nickelser/suo.svg?branch=master)](https://travis-ci.org/nickelser/suo) [![Code Climate](https://codeclimate.com/github/nickelser/suo/badges/gpa.svg)](https://codeclimate.com/github/nickelser/suo) [![Test Coverage](https://codeclimate.com/github/nickelser/suo/badges/coverage.svg)](https://codeclimate.com/github/nickelser/suo) [![Gem Version](https://badge.fury.io/rb/suo.svg)](http://badge.fury.io/rb/suo)
:lock: Distributed semaphores using Memcached or Redis in Ruby. :lock: Distributed semaphores using Memcached or Redis in Ruby.
Suo provides a very performant distributed lock solution using Compare-And-Set (`CAS`) commands in Memcached, and `WATCH/MULTI` in Redis. Suo provides a very performant distributed lock solution using Compare-And-Set (`CAS`) commands in Memcached, and `WATCH/MULTI` in Redis. It allows locking both single exclusion (like a mutex - sharing one resource), as well as multiple resources.
## Installation ## Installation
@@ -18,39 +18,61 @@ gem 'suo'
```ruby ```ruby
# Memcached # Memcached
suo = Suo::Client::Memcached.new(connection: "127.0.0.1:11211") suo = Suo::Client::Memcached.new("foo_resource", connection: "127.0.0.1:11211")
# Redis # Redis
suo = Suo::Client::Redis.new(connection: {host: "10.0.1.1"}) suo = Suo::Client::Redis.new("baz_resource", connection: {host: "10.0.1.1"})
# Pre-existing client # Pre-existing client
suo = Suo::Client::Memcached.new(client: some_dalli_client) suo = Suo::Client::Memcached.new("bar_resource", client: some_dalli_client)
suo.lock("some_key") do suo.lock do
# critical code here # critical code here
@puppies.pet! @puppies.pet!
end end
Thread.new { suo.lock("other_key", 2) { puts "One"; sleep 2 } } # The resources argument is the number of resources the semaphore will allow to lock (defaulting to one - a mutex)
Thread.new { suo.lock("other_key", 2) { puts "Two"; sleep 2 } } suo = Suo::Client::Memcached.new("bar_resource", client: some_dalli_client, resources: 2)
Thread.new { suo.lock("other_key", 2) { puts "Three" } }
Thread.new { suo.lock{ puts "One"; sleep 2 } }
Thread.new { suo.lock { puts "Two"; sleep 2 } }
Thread.new { suo.lock { puts "Three" } }
# will print "One" "Two", but not "Three", as there are only 2 resources # will print "One" "Two", but not "Three", as there are only 2 resources
# custom acquisition timeouts (time to acquire) # custom acquisition timeouts (time to acquire)
suo = Suo::Client::Memcached.new(client: some_dalli_client, acquisition_timeout: 1) # in seconds suo = Suo::Client::Memcached.new("protected_key", client: some_dalli_client, acquisition_timeout: 1) # in seconds
# manually locking/unlocking # manually locking/unlocking
suo.lock("a_key") # the return value from lock without a block is a unique token valid only for the current lock
# which must be unlocked manually
token = suo
foo.baz! foo.baz!
suo.unlock("a_key") suo.unlock(token)
# custom stale lock cleanup (cleaning of dead clients) # custom stale lock expiration (cleaning of dead locks)
suo = Suo::Client::Redis.new(client: some_redis_client, stale_lock_expiration: 60*5) suo = Suo::Client::Redis.new("other_key", client: some_redis_client, stale_lock_expiration: 60*5)
```
### Stale locks
"Stale locks" - those acquired more than `stale_lock_expiration` (defaulting to 3600 or one hour) ago - are automatically cleared during any operation on the key (`lock`, `unlock`, `refresh`). The `locked?` method will not return true if only stale locks exist, but will not modify the key itself.
To re-acquire a lock in the middle of a block, you can use the refresh method on client.
```ruby
suo = Suo::Client::Redis.new("foo")
# lock is the same token as seen in the manual example, above
suo.lock do |token|
5.times do
baz.bar!
suo.refresh(token)
end
end
``` ```
## TODO ## TODO
- better stale key handling (refresh blocks)
- more race condition tests - more race condition tests
## History ## History

View File

@@ -1,2 +1,16 @@
require "securerandom"
require "monitor"
require "dalli"
require "dalli/cas/client"
require "redis"
require "msgpack"
require "suo/version" require "suo/version"
require "suo/clients"
require "suo/errors"
require "suo/client/base"
require "suo/client/memcached"
require "suo/client/redis"

View File

@@ -4,145 +4,146 @@ module Suo
DEFAULT_OPTIONS = { DEFAULT_OPTIONS = {
acquisition_timeout: 0.1, acquisition_timeout: 0.1,
acquisition_delay: 0.01, acquisition_delay: 0.01,
stale_lock_expiration: 3600 stale_lock_expiration: 3600,
resources: 1
}.freeze }.freeze
attr_accessor :client attr_accessor :client, :key, :resources, :options
include MonitorMixin include MonitorMixin
def initialize(options = {}) def initialize(key, options = {})
fail "Client required" unless options[:client] fail "Client required" unless options[:client]
@options = DEFAULT_OPTIONS.merge(options) @options = DEFAULT_OPTIONS.merge(options)
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil @retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
@client = @options[:client] @client = @options[:client]
super() @resources = @options[:resources].to_i
@key = key
super() # initialize Monitor mixin for thread safety
end end
def lock(key, resources = 1) def lock
token = acquire_lock(key, resources) token = acquire_lock
if block_given? && token if block_given? && token
begin begin
yield yield
ensure ensure
unlock(key, token) unlock(token)
end end
else else
token token
end end
end end
def locked?(key, resources = 1) def locked?
locks(key).size >= resources locks.size >= resources
end end
def locks(key) def locks
val, _ = get(key) val, _ = get
locks = deserialize_locks(val) cleared_locks = deserialize_and_clear_locks(val)
locks cleared_locks
end end
def refresh(key, acquisition_token) def refresh(token)
retry_with_timeout(key) do retry_with_timeout do
val, cas = get(key) val, cas = get
if val.nil? if val.nil?
set_initial(key) initial_set
next next
end end
locks = deserialize_and_clear_locks(val) cleared_locks = deserialize_and_clear_locks(val)
refresh_lock(locks, acquisition_token) refresh_lock(cleared_locks, token)
break if set(key, serialize_locks(locks), cas) break if set(serialize_locks(cleared_locks), cas)
end end
end end
def unlock(key, acquisition_token) def unlock(token)
return unless acquisition_token return unless token
retry_with_timeout(key) do retry_with_timeout do
val, cas = get(key) val, cas = get
break if val.nil? break if val.nil?
locks = deserialize_and_clear_locks(val) cleared_locks = deserialize_and_clear_locks(val)
acquisition_lock = remove_lock(locks, acquisition_token) acquisition_lock = remove_lock(cleared_locks, token)
break unless acquisition_lock break unless acquisition_lock
break if set(key, serialize_locks(locks), cas) break if set(serialize_locks(cleared_locks), cas)
end end
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
# ignore - assume success due to optimistic locking # ignore - assume success due to optimistic locking
end end
def clear(key) # rubocop:disable Lint/UnusedMethodArgument def clear
fail NotImplementedError fail NotImplementedError
end end
private private
def acquire_lock(key, resources = 1) attr_accessor :retry_count
acquisition_token = nil
def acquire_lock
token = SecureRandom.base64(16) token = SecureRandom.base64(16)
retry_with_timeout(key) do retry_with_timeout do
val, cas = get(key) val, cas = get
if val.nil? if val.nil?
set_initial(key) initial_set
next next
end end
locks = deserialize_and_clear_locks(val) cleared_locks = deserialize_and_clear_locks(val)
if locks.size < resources if cleared_locks.size < resources
add_lock(locks, token) add_lock(cleared_locks, token)
newval = serialize_locks(locks) newval = serialize_locks(cleared_locks)
if set(key, newval, cas) return token if set(newval, cas)
acquisition_token = token
break
end end
end end
nil
end end
acquisition_token def get
end
def get(key) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError fail NotImplementedError
end end
def set(key, newval, oldval) # rubocop:disable Lint/UnusedMethodArgument def set(newval, cas) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError fail NotImplementedError
end end
def set_initial(key) # rubocop:disable Lint/UnusedMethodArgument def initial_set(val = "") # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError fail NotImplementedError
end end
def synchronize(key) # rubocop:disable Lint/UnusedMethodArgument def synchronize
mon_synchronize { yield } mon_synchronize { yield }
end end
def retry_with_timeout(key) def retry_with_timeout
start = Time.now.to_f start = Time.now.to_f
@retry_count.times do retry_count.times do
now = Time.now.to_f elapsed = Time.now.to_f - start
break if now - start > @options[:acquisition_timeout] break if elapsed >= options[:acquisition_timeout]
synchronize(key) do synchronize do
yield yield
end end
sleep(rand(@options[:acquisition_delay] * 1000).to_f / 1000) sleep(rand(options[:acquisition_delay] * 1000).to_f / 1000)
end end
rescue => _ rescue => _
raise LockClientError raise LockClientError
@@ -162,17 +163,17 @@ module Suo
unpacked.map do |time, token| unpacked.map do |time, token|
[Time.at(time), token] [Time.at(time), token]
end end
rescue EOFError => _ rescue EOFError, MessagePack::MalformedFormatError => _
[] []
end end
def clear_expired_locks(locks) 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
def add_lock(locks, token) def add_lock(locks, token, time = Time.now.to_f)
locks << [Time.now.to_f, token] locks << [time, token]
end end
def remove_lock(locks, acquisition_token) def remove_lock(locks, acquisition_token)
@@ -182,7 +183,7 @@ module Suo
def refresh_lock(locks, acquisition_token) def refresh_lock(locks, acquisition_token)
remove_lock(locks, acquisition_token) remove_lock(locks, acquisition_token)
add_lock(locks, token) add_lock(locks, acquisition_token)
end end
end end
end end

View File

@@ -1,27 +1,27 @@
module Suo module Suo
module Client module Client
class Memcached < Base class Memcached < Base
def initialize(options = {}) def initialize(key, options = {})
options[:client] ||= Dalli::Client.new(options[:connection] || ENV["MEMCACHE_SERVERS"] || "127.0.0.1:11211") options[:client] ||= Dalli::Client.new(options[:connection] || ENV["MEMCACHE_SERVERS"] || "127.0.0.1:11211")
super super
end end
def clear(key) def clear
@client.delete(key) @client.delete(@key)
end end
private private
def get(key) def get
@client.get_cas(key) @client.get_cas(@key)
end end
def set(key, newval, cas) def set(newval, cas)
@client.set_cas(key, newval, cas) @client.set_cas(@key, newval, cas)
end end
def set_initial(key) def initial_set(val = "")
@client.set(key, "") @client.set(@key, val)
end end
end end
end end

View File

@@ -1,39 +1,39 @@
module Suo module Suo
module Client module Client
class Redis < Base class Redis < Base
def initialize(options = {}) def initialize(key, options = {})
options[:client] ||= ::Redis.new(options[:connection] || {}) options[:client] ||= ::Redis.new(options[:connection] || {})
super super
end end
def clear(key) def clear
@client.del(key) @client.del(@key)
end end
private private
def get(key) def get
[@client.get(key), nil] [@client.get(@key), nil]
end end
def set(key, newval, _) def set(newval, _)
ret = @client.multi do |multi| ret = @client.multi do |multi|
multi.set(key, newval) multi.set(@key, newval)
end end
ret[0] == "OK" ret && ret[0] == "OK"
end end
def synchronize(key) def synchronize
@client.watch(key) do @client.watch(@key) do
yield yield
end end
ensure ensure
@client.unwatch @client.unwatch
end end
def set_initial(key) def initial_set(val = "")
@client.set(key, "") @client.set(@key, val)
end end
end end
end end

View File

@@ -1,14 +0,0 @@
require "securerandom"
require "monitor"
require "dalli"
require "dalli/cas/client"
require "redis"
require "msgpack"
require "suo/errors"
require "suo/client/base"
require "suo/client/memcached"
require "suo/client/redis"

View File

@@ -1,3 +1,3 @@
module Suo module Suo
VERSION = "0.2.0" VERSION = "0.3.0"
end end

View File

@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Nick Elser"] spec.authors = ["Nick Elser"]
spec.email = ["nick.elser@gmail.com"] spec.email = ["nick.elser@gmail.com"]
spec.summary = %q(Distributed semaphores using Memcached or Redis.) spec.summary = %q(Distributed locks (mutexes & semaphores) using Memcached or Redis.)
spec.description = %q(Distributed semaphores using Memcached or Redis.) spec.description = %q(Distributed locks (mutexes & semaphores) using Memcached or Redis.)
spec.homepage = "https://github.com/nickelser/suo" spec.homepage = "https://github.com/nickelser/suo"
spec.license = "MIT" spec.license = "MIT"
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rubocop", "~> 0.30.0" spec.add_development_dependency "rubocop", "~> 0.30.0"
spec.add_development_dependency "minitest", "~> 5.5.0" spec.add_development_dependency "minitest", "~> 5.5.0"
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
end end

View File

@@ -3,143 +3,355 @@ require "test_helper"
TEST_KEY = "suo_test_key".freeze TEST_KEY = "suo_test_key".freeze
module ClientTests module ClientTests
def client(options = {})
@client.class.new(options[:key] || TEST_KEY, options.merge(client: @client.client))
end
def test_throws_failed_error_on_bad_client def test_throws_failed_error_on_bad_client
assert_raises(Suo::LockClientError) do assert_raises(Suo::LockClientError) do
client = @client.class.new(client: {}) client = @client.class.new(TEST_KEY, client: {})
client.lock(TEST_KEY, 1) client.lock
end end
end end
def test_class_single_resource_locking def test_single_resource_locking
lock1 = @client.lock(TEST_KEY, 1) lock1 = @client.lock
refute_nil lock1 refute_nil lock1
locked = @client.locked?(TEST_KEY, 1) locked = @client.locked?
assert_equal true, locked assert_equal true, locked
lock2 = @client.lock(TEST_KEY, 1) lock2 = @client.lock
assert_nil lock2 assert_nil lock2
@client.unlock(TEST_KEY, lock1) @client.unlock(lock1)
locked = @client.locked?(TEST_KEY, 1) locked = @client.locked?
assert_equal false, locked assert_equal false, locked
end end
def test_class_multiple_resource_locking def test_empty_lock_on_invalid_data
lock1 = @client.lock(TEST_KEY, 2) @client.send(:initial_set, "bad value")
assert_equal false, @client.locked?
end
def test_clear
lock1 = @client.lock
refute_nil lock1 refute_nil lock1
locked = @client.locked?(TEST_KEY, 2) @client.clear
assert_equal false, locked
lock2 = @client.lock(TEST_KEY, 2) assert_equal false, @client.locked?
end
def test_multiple_resource_locking
@client = client(resources: 2)
lock1 = @client.lock
refute_nil lock1
assert_equal false, @client.locked?
lock2 = @client.lock
refute_nil lock2 refute_nil lock2
locked = @client.locked?(TEST_KEY, 2) assert_equal true, @client.locked?
assert_equal true, locked
@client.unlock(TEST_KEY, lock1) @client.unlock(lock1)
locked = @client.locked?(TEST_KEY, 1) assert_equal false, @client.locked?
assert_equal true, locked
@client.unlock(TEST_KEY, lock2) assert_equal 1, @client.locks.size
locked = @client.locked?(TEST_KEY, 1) @client.unlock(lock2)
assert_equal false, locked
assert_equal false, @client.locked?
assert_equal 0, @client.locks.size
end end
def test_block_single_resource_locking def test_block_single_resource_locking
locked = false locked = false
@client.lock(TEST_KEY, 1) { locked = true } @client.lock { locked = true }
assert_equal true, locked assert_equal true, locked
end end
def test_block_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{ fail "Test" }
end end
locked = @client.locked?(TEST_KEY, 1) assert_equal false, @client.locked?
assert_equal false, locked
end end
def test_readme_example def test_readme_example
output = Queue.new output = Queue.new
@client = client(resources: 2)
threads = [] threads = []
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "One"; sleep 2 } } threads << Thread.new { @client.lock { output << "One"; sleep 0.5 } }
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Two"; sleep 2 } } threads << Thread.new { @client.lock { output << "Two"; sleep 0.5 } }
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Three" } } sleep 0.1
threads << Thread.new { @client.lock { output << "Three" } }
threads.map(&:join) threads.each(&:join)
ret = [] ret = []
ret << output.pop ret << (output.size > 0 ? output.pop : nil)
ret << output.pop ret << (output.size > 0 ? output.pop : nil)
ret.sort! ret.sort!
assert_equal 0, output.size assert_equal 0, output.size
assert_equal ["One", "Two"], ret assert_equal %w(One Two), ret
assert_equal false, @client.locked?
end end
def test_instance_multiple_resource_locking def test_block_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) @client = client(acquisition_timeout: 0.9, resources: 50)
100.times.map do |i| 100.times.map do |i|
Thread.new do Thread.new do
success = @client.lock(TEST_KEY, 50) do success = @client.lock do
sleep(3) sleep(3)
success_counter << i success_counter << i
end end
failure_counter << i unless success failure_counter << i unless success
end end
end.map(&:join) end.each(&:join)
assert_equal 50, success_counter.size assert_equal 50, success_counter.size
assert_equal 50, failure_counter.size assert_equal 50, failure_counter.size
assert_equal false, @client.locked?
end end
def test_instance_multiple_resource_locking_longer_timeout def test_block_multiple_resource_locking_longer_timeout
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) @client = client(acquisition_timeout: 3, resources: 50)
100.times.map do |i| 100.times.map do |i|
Thread.new do Thread.new do
success = client.lock(TEST_KEY, 50) do success = @client.lock do
sleep(0.5) sleep(0.5)
success_counter << i success_counter << i
end end
failure_counter << i unless success failure_counter << i unless success
end end
end.map(&:join) end.each(&:join)
assert_equal 100, success_counter.size assert_equal 100, success_counter.size
assert_equal 0, failure_counter.size assert_equal 0, failure_counter.size
assert_equal false, @client.locked?
end
def test_unstale_lock_acquisition
success_counter = Queue.new
failure_counter = Queue.new
@client = client(stale_lock_expiration: 0.5)
t1 = Thread.new { @client.lock { sleep 0.6; success_counter << 1 } }
sleep 0.3
t2 = Thread.new do
locked = @client.lock { success_counter << 1 }
failure_counter << 1 unless locked
end
[t1, t2].each(&:join)
assert_equal 1, success_counter.size
assert_equal 1, failure_counter.size
assert_equal false, @client.locked?
end
def test_stale_lock_acquisition
success_counter = Queue.new
failure_counter = Queue.new
@client = client(stale_lock_expiration: 0.5)
t1 = Thread.new { @client.lock { sleep 0.6; success_counter << 1 } }
sleep 0.55
t2 = Thread.new do
locked = @client.lock { success_counter << 1 }
failure_counter << 1 unless locked
end
[t1, t2].each(&:join)
assert_equal 2, success_counter.size
assert_equal 0, failure_counter.size
assert_equal false, @client.locked?
end
def test_refresh
@client = client(stale_lock_expiration: 0.5)
lock1 = @client.lock
assert_equal true, @client.locked?
@client.refresh(lock1)
assert_equal true, @client.locked?
sleep 0.55
assert_equal false, @client.locked?
lock2 = @client.lock
@client.refresh(lock1)
assert_equal true, @client.locked?
@client.unlock(lock1)
# edge case with refresh lock in the middle
assert_equal true, @client.locked?
@client.clear
assert_equal false, @client.locked?
@client.refresh(lock2)
assert_equal true, @client.locked?
@client.unlock(lock2)
# now finally unlocked
assert_equal false, @client.locked?
end
def test_block_refresh
success_counter = Queue.new
failure_counter = Queue.new
@client = client(stale_lock_expiration: 0.5)
t1 = Thread.new do
@client.lock do |token|
sleep 0.6
@client.refresh(token)
sleep 1
success_counter << 1
end
end
t2 = Thread.new do
sleep 0.8
locked = @client.lock { success_counter << 1 }
failure_counter << 1 unless locked
end
[t1, t2].each(&:join)
assert_equal 1, success_counter.size
assert_equal 1, failure_counter.size
assert_equal false, @client.locked?
end
def test_refresh_multi
success_counter = Queue.new
failure_counter = Queue.new
@client = client(stale_lock_expiration: 0.5, resources: 2)
t1 = Thread.new do
@client.lock do |token|
sleep 0.4
@client.refresh(token)
success_counter << 1
sleep 0.5
end
end
t2 = Thread.new do
sleep 0.55
locked = @client.lock do
success_counter << 1
sleep 0.5
end
failure_counter << 1 unless locked
end
t3 = Thread.new do
sleep 0.75
locked = @client.lock { success_counter << 1 }
failure_counter << 1 unless locked
end
[t1, t2, t3].each(&:join)
assert_equal 2, success_counter.size
assert_equal 1, failure_counter.size
assert_equal false, @client.locked?
end
def test_increment_reused_client
i = 0
threads = 2.times.map do
Thread.new do
@client.lock { i += 1 }
end
end
threads.each(&:join)
assert_equal 2, i
assert_equal false, @client.locked?
end
def test_increment_new_client
i = 0
threads = 2.times.map do
Thread.new do
# note this is the method that generates a *new* client
client.lock { i += 1 }
end
end
threads.each(&:join)
assert_equal 2, i
assert_equal false, @client.locked?
end end
end end
class TestBaseClient < Minitest::Test class TestBaseClient < Minitest::Test
def setup def setup
@client = Suo::Client::Base.new(client: {}) @client = Suo::Client::Base.new(TEST_KEY, client: {})
end end
def test_not_implemented def test_not_implemented
assert_raises(NotImplementedError) do assert_raises(NotImplementedError) do
@client.send(:get, TEST_KEY) @client.send(:get)
end
assert_raises(NotImplementedError) do
@client.send(:set, "", "")
end
assert_raises(NotImplementedError) do
@client.send(:initial_set)
end
assert_raises(NotImplementedError) do
@client.send(:clear)
end end
end end
end end
@@ -149,7 +361,8 @@ class TestMemcachedClient < Minitest::Test
def setup def setup
@dalli = Dalli::Client.new("127.0.0.1:11211") @dalli = Dalli::Client.new("127.0.0.1:11211")
@client = Suo::Client::Memcached.new @client = Suo::Client::Memcached.new(TEST_KEY)
teardown
end end
def teardown def teardown
@@ -162,7 +375,8 @@ class TestRedisClient < Minitest::Test
def setup def setup
@redis = Redis.new @redis = Redis.new
@client = Suo::Client::Redis.new @client = Suo::Client::Redis.new(TEST_KEY)
teardown
end end
def teardown def teardown

View File

@@ -1,9 +1,13 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
if ENV["CODECLIMATE_REPO_TOKEN"]
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
end
require "suo" require "suo"
require "thread" require "thread"
require "minitest/autorun" require "minitest/autorun"
require "minitest/benchmark" require "minitest/benchmark"
ENV["SUO_TEST"] = "true" ENV["SUO_TEST"] = "true"