mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59d3385d8a | ||
|
|
3d5c102c08 | ||
|
|
7bb28cc007 | ||
|
|
65cae9aa58 | ||
|
|
6b6eb4e590 | ||
|
|
485f2bff37 | ||
|
|
3a37a74982 | ||
|
|
b02c256c25 | ||
|
|
d57f6a15ac | ||
|
|
d293ef6fcf | ||
|
|
6e74322ff1 | ||
|
|
b9d3f1b7a1 | ||
|
|
270c05b80e | ||
|
|
60e167e146 | ||
|
|
ad08c8b5ea | ||
|
|
9b8ef6c244 | ||
|
|
b8a1d7d9ac | ||
|
|
c58a247156 | ||
|
|
8c37c24ee6 | ||
|
|
29da8cf090 | ||
|
|
8ed488f071 | ||
|
|
152b6acf9c | ||
|
|
5e10afe534 | ||
|
|
0423eb9e12 | ||
|
|
ca46f5f369 | ||
|
|
1022a6f9d3 | ||
|
|
6be3a5bdda | ||
|
|
aa4da5d739 | ||
|
|
fdb0b7f9d5 | ||
|
|
a13edcf7d1 | ||
|
|
af1c476f08 | ||
|
|
58fae54022 | ||
|
|
2088fd90b3 | ||
|
|
05661e143c | ||
|
|
a23282dcc6 | ||
|
|
323caaee9b | ||
|
|
745d49466f | ||
|
|
161d50deb9 | ||
|
|
81e4a3e143 | ||
|
|
2960c14a4d | ||
|
|
308e918e60 | ||
|
|
aaee69a2df | ||
|
|
c6d1c29ada | ||
|
|
14e442e99d | ||
|
|
498073b92e |
38
.github/workflows/CI.yml
vendored
Normal file
38
.github/workflows/CI.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
ruby:
|
||||||
|
- '2.5'
|
||||||
|
- '2.6'
|
||||||
|
- '2.7'
|
||||||
|
- '3.0'
|
||||||
|
- ruby-head
|
||||||
|
continue-on-error: ${{ matrix.ruby == 'ruby-head' }}
|
||||||
|
services:
|
||||||
|
memcached:
|
||||||
|
image: memcached
|
||||||
|
ports:
|
||||||
|
- 11211:11211
|
||||||
|
redis:
|
||||||
|
image: redis
|
||||||
|
ports:
|
||||||
|
- 6379:6379
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: ruby/setup-ruby@v1
|
||||||
|
with:
|
||||||
|
ruby-version: ${{ matrix.ruby }}
|
||||||
|
bundler-cache: true
|
||||||
|
- run: |
|
||||||
|
bundle exec rake
|
||||||
@@ -74,7 +74,7 @@ Style/SpaceInsideBrackets:
|
|||||||
Style/AndOr:
|
Style/AndOr:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
Style/TrailingComma:
|
Style/TrailingCommaInLiteral:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceBeforeComma:
|
Style/SpaceBeforeComma:
|
||||||
@@ -98,7 +98,7 @@ Style/SpaceAfterColon:
|
|||||||
Style/SpaceAfterComma:
|
Style/SpaceAfterComma:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceAfterControlKeyword:
|
Style/SpaceAroundKeyword:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceAfterNot:
|
Style/SpaceAfterNot:
|
||||||
@@ -163,7 +163,7 @@ Style/StringLiterals:
|
|||||||
EnforcedStyle: double_quotes
|
EnforcedStyle: double_quotes
|
||||||
|
|
||||||
Metrics/CyclomaticComplexity:
|
Metrics/CyclomaticComplexity:
|
||||||
Max: 8
|
Max: 10
|
||||||
|
|
||||||
Metrics/LineLength:
|
Metrics/LineLength:
|
||||||
Max: 128
|
Max: 128
|
||||||
@@ -214,3 +214,6 @@ Metrics/ParameterLists:
|
|||||||
|
|
||||||
Metrics/PerceivedComplexity:
|
Metrics/PerceivedComplexity:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Style/Documentation:
|
||||||
|
Enabled: false
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
language: ruby
|
|
||||||
rvm:
|
|
||||||
- 2.2.0
|
|
||||||
services:
|
|
||||||
- memcached
|
|
||||||
- redis-server
|
|
||||||
34
CHANGELOG.md
34
CHANGELOG.md
@@ -1,3 +1,35 @@
|
|||||||
|
## 0.4.0
|
||||||
|
|
||||||
|
- Monotonic clock for locks, avoiding issues with DST (thanks @doits)
|
||||||
|
- Pooled connection support (thanks @mlarraz)
|
||||||
|
- Switch to Github actions for tests (thanks @mlarraz)
|
||||||
|
- Update supported Ruby versions (thanks @mlarraz & @pat)
|
||||||
|
|
||||||
|
## 0.3.4
|
||||||
|
|
||||||
|
- Support for connection pooling when using memcached locks, via `with` blocks using Dalli (thanks to Lev).
|
||||||
|
|
||||||
|
## 0.3.3
|
||||||
|
|
||||||
|
- Default TTL for keys to allow for short-lived locking keys (thanks to Ian Remillard) without leaking memory.
|
||||||
|
- Vastly improve initial lock acquisition, especially on Redis (thanks to Jeremy Wadscak).
|
||||||
|
|
||||||
|
## 0.3.2
|
||||||
|
|
||||||
|
- Custom lock tokens (thanks to avokhmin).
|
||||||
|
|
||||||
|
## 0.3.1
|
||||||
|
|
||||||
|
- Slight memory leak fix.
|
||||||
|
|
||||||
|
## 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
|
## 0.2.2
|
||||||
|
|
||||||
- Fix bug with refresh - typo would've prevented real use.
|
- Fix bug with refresh - typo would've prevented real use.
|
||||||
@@ -25,7 +57,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
|
||||||
|
|
||||||
|
|||||||
46
README.md
46
README.md
@@ -1,8 +1,8 @@
|
|||||||
# Suo [](https://travis-ci.org/nickelser/suo) [](https://codeclimate.com/github/nickelser/suo) [](https://codeclimate.com/github/nickelser/suo) [](http://badge.fury.io/rb/suo)
|
# Suo [](https://github.com/nickelser/suo/actions?query=workflow%3ACI) [](https://codeclimate.com/github/nickelser/suo) [](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,37 +18,40 @@ 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
|
||||||
# the return value from lock without a block is a unique token valid only for the current lock
|
# the return value from lock without a block is a unique token valid only for the current lock
|
||||||
# which must be unlocked manually
|
# which must be unlocked manually
|
||||||
lock = suo.lock("a_key")
|
token = suo.lock
|
||||||
foo.baz!
|
foo.baz!
|
||||||
suo.unlock("a_key", lock)
|
suo.unlock(token)
|
||||||
|
|
||||||
# custom stale lock expiration (cleaning of dead locks)
|
# 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
|
||||||
@@ -58,23 +61,32 @@ suo = Suo::Client::Redis.new(client: some_redis_client, stale_lock_expiration: 6
|
|||||||
To re-acquire a lock in the middle of a block, you can use the refresh method on client.
|
To re-acquire a lock in the middle of a block, you can use the refresh method on client.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
suo = Suo::Client::Redis.new
|
suo = Suo::Client::Redis.new("foo")
|
||||||
|
|
||||||
# lock is the same token as seen in the manual example, above
|
# lock is the same token as seen in the manual example, above
|
||||||
suo.lock("foo") do |lock|
|
suo.lock do |token|
|
||||||
5.times do
|
5.times do
|
||||||
baz.bar!
|
baz.bar!
|
||||||
suo.refresh("foo", lock)
|
suo.refresh(token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Time To Live
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Suo::Client::Redis.new("bar_resource", ttl: 60) #ttl in seconds
|
||||||
|
```
|
||||||
|
|
||||||
|
A key representing a set of lockable resources is removed once the last resource lock is released and the `ttl` time runs out. When another lock is acquired and the key has been removed the key has to be recreated.
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
- more race condition tests
|
- more race condition tests
|
||||||
|
|
||||||
## History
|
## History
|
||||||
|
|
||||||
View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md)
|
View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md).
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ require "securerandom"
|
|||||||
require "monitor"
|
require "monitor"
|
||||||
|
|
||||||
require "dalli"
|
require "dalli"
|
||||||
require "dalli/cas/client"
|
|
||||||
|
|
||||||
require "redis"
|
require "redis"
|
||||||
|
|
||||||
|
|||||||
@@ -4,99 +4,102 @@ 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,
|
||||||
|
ttl: 60,
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
attr_accessor :client
|
BLANK_STR = "".freeze
|
||||||
|
|
||||||
|
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(custom_token = nil)
|
||||||
token = acquire_lock(key, resources)
|
token = acquire_lock(custom_token)
|
||||||
|
|
||||||
if block_given? && token
|
if block_given? && token
|
||||||
begin
|
begin
|
||||||
yield(token)
|
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
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
cleared_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?
|
cas = initial_set if val.nil?
|
||||||
initial_set(key)
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
refresh_lock(cleared_locks, acquisition_token)
|
refresh_lock(cleared_locks, token)
|
||||||
|
|
||||||
break if set(key, serialize_locks(cleared_locks), cas)
|
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||||
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?
|
||||||
|
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
acquisition_lock = remove_lock(cleared_locks, acquisition_token)
|
acquisition_lock = remove_lock(cleared_locks, token)
|
||||||
|
|
||||||
break unless acquisition_lock
|
break unless acquisition_lock
|
||||||
break if set(key, serialize_locks(cleared_locks), cas)
|
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||||
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
|
|
||||||
token = SecureRandom.base64(16)
|
|
||||||
|
|
||||||
retry_with_timeout(key) do
|
def acquire_lock(token = nil)
|
||||||
val, cas = get(key)
|
token ||= SecureRandom.base64(16)
|
||||||
|
|
||||||
if val.nil?
|
retry_with_timeout do
|
||||||
initial_set(key)
|
val, cas = get
|
||||||
next
|
|
||||||
end
|
cas = initial_set if val.nil?
|
||||||
|
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
@@ -105,44 +108,41 @@ module Suo
|
|||||||
|
|
||||||
newval = serialize_locks(cleared_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
|
end
|
||||||
|
|
||||||
acquisition_token
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(key) # rubocop:disable Lint/UnusedMethodArgument
|
def get
|
||||||
fail NotImplementedError
|
fail NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, cas) # rubocop:disable Lint/UnusedMethodArgument
|
def set(newval, cas) # rubocop:disable Lint/UnusedMethodArgument
|
||||||
fail NotImplementedError
|
fail NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(key, val = "") # rubocop:disable Lint/UnusedMethodArgument
|
def initial_set(val = BLANK_STR) # 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 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
|
|
||||||
@retry_count.times do
|
retry_count.times do
|
||||||
now = Time.now.to_f
|
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - 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
|
||||||
@@ -157,7 +157,7 @@ module Suo
|
|||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_locks(val)
|
def deserialize_locks(val)
|
||||||
unpacked = (val.nil? || val == "") ? [] : MessagePack.unpack(val)
|
unpacked = (val.nil? || val == BLANK_STR) ? [] : MessagePack.unpack(val)
|
||||||
|
|
||||||
unpacked.map do |time, token|
|
unpacked.map do |time, token|
|
||||||
[Time.at(time), token]
|
[Time.at(time), token]
|
||||||
@@ -167,7 +167,7 @@ module Suo
|
|||||||
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
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,35 @@
|
|||||||
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.with { |client| client.delete(@key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get(key)
|
def get
|
||||||
@client.get_cas(key)
|
@client.with { |client| client.get_cas(@key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, cas)
|
def set(newval, cas, expire: false)
|
||||||
@client.set_cas(key, newval, cas)
|
if expire
|
||||||
|
@client.with { |client| client.set_cas(@key, newval, cas, @options[:ttl]) }
|
||||||
|
else
|
||||||
|
@client.with { |client| client.set_cas(@key, newval, cas) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(key, val = "")
|
def initial_set(val = BLANK_STR)
|
||||||
@client.set(key, val)
|
@client.with do |client|
|
||||||
|
client.set(@key, val)
|
||||||
|
_val, cas = client.get_cas(@key)
|
||||||
|
cas
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,39 +1,54 @@
|
|||||||
module Suo
|
module Suo
|
||||||
module Client
|
module Client
|
||||||
class Redis < Base
|
class Redis < Base
|
||||||
def initialize(options = {})
|
OK_STR = "OK".freeze
|
||||||
|
|
||||||
|
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)
|
with { |r| r.del(@key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get(key)
|
def with(&block)
|
||||||
[@client.get(key), nil]
|
if @client.respond_to?(:with)
|
||||||
|
@client.with(&block)
|
||||||
|
else
|
||||||
|
yield @client
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, newval, _)
|
def get
|
||||||
ret = @client.multi do |multi|
|
[with { |r| r.get(@key) }, nil]
|
||||||
multi.set(key, newval)
|
|
||||||
end
|
|
||||||
|
|
||||||
ret && ret[0] == "OK"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def synchronize(key)
|
def set(newval, _, expire: false)
|
||||||
@client.watch(key) do
|
ret = with do |r|
|
||||||
yield
|
r.multi do |rr|
|
||||||
|
if expire
|
||||||
|
rr.setex(@key, @options[:ttl], newval)
|
||||||
|
else
|
||||||
|
rr.set(@key, newval)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ret && ret[0] == OK_STR
|
||||||
|
end
|
||||||
|
|
||||||
|
def synchronize
|
||||||
|
with { |r| r.watch(@key) { yield } }
|
||||||
ensure
|
ensure
|
||||||
@client.unwatch
|
with { |r| r.unwatch }
|
||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(key, val = "")
|
def initial_set(val = BLANK_STR)
|
||||||
@client.set(key, val)
|
set(val, nil)
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Suo
|
module Suo
|
||||||
VERSION = "0.2.2"
|
VERSION = "0.4.0".freeze
|
||||||
end
|
end
|
||||||
|
|||||||
13
suo.gemspec
13
suo.gemspec
@@ -9,26 +9,25 @@ 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"
|
||||||
|
|
||||||
spec.files = `git ls-files -z`.split("\x0")
|
spec.files = `git ls-files -z`.split("\x0")
|
||||||
spec.bindir = "bin"
|
spec.bindir = "bin"
|
||||||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
||||||
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.required_ruby_version = ">= 2.5"
|
||||||
|
|
||||||
spec.add_dependency "dalli"
|
spec.add_dependency "dalli"
|
||||||
spec.add_dependency "redis"
|
spec.add_dependency "redis"
|
||||||
spec.add_dependency "msgpack"
|
spec.add_dependency "msgpack"
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 1.5"
|
spec.add_development_dependency "bundler"
|
||||||
spec.add_development_dependency "rake", "~> 10.0"
|
spec.add_development_dependency "rake", "~> 13.0"
|
||||||
spec.add_development_dependency "rubocop", "~> 0.30.0"
|
spec.add_development_dependency "rubocop", "~> 0.49.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"
|
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,249 +3,253 @@ require "test_helper"
|
|||||||
TEST_KEY = "suo_test_key".freeze
|
TEST_KEY = "suo_test_key".freeze
|
||||||
|
|
||||||
module ClientTests
|
module ClientTests
|
||||||
def client(options)
|
def client(options = {})
|
||||||
@client.class.new(options.merge(client: @client.client))
|
@client.class.new(options[:key] || TEST_KEY, options.merge(client: @client.client))
|
||||||
end
|
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_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_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, TEST_KEY, "bad value")
|
@client.send(:initial_set, "bad value")
|
||||||
locked = @client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
assert_equal false, locked
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_clear
|
def test_clear
|
||||||
lock1 = @client.lock(TEST_KEY, 1)
|
lock1 = @client.lock
|
||||||
refute_nil lock1
|
refute_nil lock1
|
||||||
|
|
||||||
@client.clear(TEST_KEY)
|
@client.clear
|
||||||
|
|
||||||
locked = @client.locked?(TEST_KEY, 1)
|
assert_equal false, @client.locked?
|
||||||
|
|
||||||
assert_equal false, locked
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_multiple_resource_locking
|
def test_multiple_resource_locking
|
||||||
lock1 = @client.lock(TEST_KEY, 2)
|
@client = client(resources: 2)
|
||||||
|
|
||||||
|
lock1 = @client.lock
|
||||||
refute_nil lock1
|
refute_nil lock1
|
||||||
|
|
||||||
locked = @client.locked?(TEST_KEY, 2)
|
assert_equal false, @client.locked?
|
||||||
assert_equal false, locked
|
|
||||||
|
|
||||||
lock2 = @client.lock(TEST_KEY, 2)
|
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 0.5 } }
|
threads << Thread.new { @client.lock { output << "One"; sleep 0.5 } }
|
||||||
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Two"; sleep 0.5 } }
|
threads << Thread.new { @client.lock { output << "Two"; sleep 0.5 } }
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Three" } }
|
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 %w(One Two), ret
|
assert_equal %w(One Two), ret
|
||||||
assert_equal false, @client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_block_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(acquisition_timeout: 0.9)
|
@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?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_block_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(acquisition_timeout: 3)
|
@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?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unstale_lock_acquisition
|
def test_unstale_lock_acquisition
|
||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
client = client(stale_lock_expiration: 0.5)
|
@client = client(stale_lock_expiration: 0.5)
|
||||||
|
|
||||||
t1 = Thread.new { client.lock(TEST_KEY) { sleep 0.6; success_counter << 1 } }
|
t1 = Thread.new { @client.lock { sleep 0.6; success_counter << 1 } }
|
||||||
sleep 0.3
|
sleep 0.3
|
||||||
t2 = Thread.new do
|
t2 = Thread.new do
|
||||||
locked = client.lock(TEST_KEY) { success_counter << 1 }
|
locked = @client.lock { success_counter << 1 }
|
||||||
failure_counter << 1 unless locked
|
failure_counter << 1 unless locked
|
||||||
end
|
end
|
||||||
|
|
||||||
[t1, t2].map(&:join)
|
[t1, t2].each(&:join)
|
||||||
|
|
||||||
assert_equal 1, success_counter.size
|
assert_equal 1, success_counter.size
|
||||||
assert_equal 1, failure_counter.size
|
assert_equal 1, failure_counter.size
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_stale_lock_acquisition
|
def test_stale_lock_acquisition
|
||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
client = client(stale_lock_expiration: 0.5)
|
@client = client(stale_lock_expiration: 0.5)
|
||||||
|
|
||||||
t1 = Thread.new { client.lock(TEST_KEY) { sleep 0.6; success_counter << 1 } }
|
t1 = Thread.new { @client.lock { sleep 0.6; success_counter << 1 } }
|
||||||
sleep 0.55
|
sleep 0.55
|
||||||
t2 = Thread.new do
|
t2 = Thread.new do
|
||||||
locked = client.lock(TEST_KEY) { success_counter << 1 }
|
locked = @client.lock { success_counter << 1 }
|
||||||
failure_counter << 1 unless locked
|
failure_counter << 1 unless locked
|
||||||
end
|
end
|
||||||
|
|
||||||
[t1, t2].map(&:join)
|
[t1, t2].each(&:join)
|
||||||
|
|
||||||
assert_equal 2, success_counter.size
|
assert_equal 2, success_counter.size
|
||||||
assert_equal 0, failure_counter.size
|
assert_equal 0, failure_counter.size
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_refresh
|
def test_refresh
|
||||||
client = client(stale_lock_expiration: 0.5)
|
@client = client(stale_lock_expiration: 0.5)
|
||||||
|
|
||||||
lock1 = client.lock(TEST_KEY)
|
lock1 = @client.lock
|
||||||
|
|
||||||
assert_equal true, client.locked?(TEST_KEY)
|
assert_equal true, @client.locked?
|
||||||
|
|
||||||
client.refresh(TEST_KEY, lock1)
|
@client.refresh(lock1)
|
||||||
|
|
||||||
assert_equal true, client.locked?(TEST_KEY)
|
assert_equal true, @client.locked?
|
||||||
|
|
||||||
sleep 0.55
|
sleep 0.55
|
||||||
|
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
|
|
||||||
lock2 = client.lock(TEST_KEY)
|
lock2 = @client.lock
|
||||||
|
|
||||||
client.refresh(TEST_KEY, lock1)
|
@client.refresh(lock1)
|
||||||
|
|
||||||
assert_equal true, client.locked?(TEST_KEY)
|
assert_equal true, @client.locked?
|
||||||
|
|
||||||
client.unlock(TEST_KEY, lock1)
|
@client.unlock(lock1)
|
||||||
|
|
||||||
# edge case with refresh lock in the middle
|
# edge case with refresh lock in the middle
|
||||||
assert_equal true, client.locked?(TEST_KEY)
|
assert_equal true, @client.locked?
|
||||||
|
|
||||||
client.clear(TEST_KEY)
|
@client.clear
|
||||||
|
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
|
|
||||||
client.refresh(TEST_KEY, lock2)
|
@client.refresh(lock2)
|
||||||
|
|
||||||
assert_equal true, client.locked?(TEST_KEY)
|
assert_equal true, @client.locked?
|
||||||
|
|
||||||
client.unlock(TEST_KEY, lock2)
|
@client.unlock(lock2)
|
||||||
|
|
||||||
# now finally unlocked
|
# now finally unlocked
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_block_refresh
|
def test_block_refresh
|
||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
client = client(stale_lock_expiration: 0.5)
|
@client = client(stale_lock_expiration: 0.5)
|
||||||
|
|
||||||
t1 = Thread.new do
|
t1 = Thread.new do
|
||||||
client.lock(TEST_KEY) do |token|
|
@client.lock do |token|
|
||||||
sleep 0.6
|
sleep 0.6
|
||||||
client.refresh(TEST_KEY, token)
|
@client.refresh(token)
|
||||||
sleep 1
|
sleep 1
|
||||||
success_counter << 1
|
success_counter << 1
|
||||||
end
|
end
|
||||||
@@ -253,27 +257,27 @@ module ClientTests
|
|||||||
|
|
||||||
t2 = Thread.new do
|
t2 = Thread.new do
|
||||||
sleep 0.8
|
sleep 0.8
|
||||||
locked = client.lock(TEST_KEY) { success_counter << 1 }
|
locked = @client.lock { success_counter << 1 }
|
||||||
failure_counter << 1 unless locked
|
failure_counter << 1 unless locked
|
||||||
end
|
end
|
||||||
|
|
||||||
[t1, t2].map(&:join)
|
[t1, t2].each(&:join)
|
||||||
|
|
||||||
assert_equal 1, success_counter.size
|
assert_equal 1, success_counter.size
|
||||||
assert_equal 1, failure_counter.size
|
assert_equal 1, failure_counter.size
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
assert_equal false, @client.locked?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_refresh_multi
|
def test_refresh_multi
|
||||||
success_counter = Queue.new
|
success_counter = Queue.new
|
||||||
failure_counter = Queue.new
|
failure_counter = Queue.new
|
||||||
|
|
||||||
client = client(stale_lock_expiration: 0.5)
|
@client = client(stale_lock_expiration: 0.5, resources: 2)
|
||||||
|
|
||||||
t1 = Thread.new do
|
t1 = Thread.new do
|
||||||
client.lock(TEST_KEY, 2) do |token|
|
@client.lock do |token|
|
||||||
sleep 0.4
|
sleep 0.4
|
||||||
client.refresh(TEST_KEY, token)
|
@client.refresh(token)
|
||||||
success_counter << 1
|
success_counter << 1
|
||||||
sleep 0.5
|
sleep 0.5
|
||||||
end
|
end
|
||||||
@@ -281,7 +285,7 @@ module ClientTests
|
|||||||
|
|
||||||
t2 = Thread.new do
|
t2 = Thread.new do
|
||||||
sleep 0.55
|
sleep 0.55
|
||||||
locked = client.lock(TEST_KEY, 2) do
|
locked = @client.lock do
|
||||||
success_counter << 1
|
success_counter << 1
|
||||||
sleep 0.5
|
sleep 0.5
|
||||||
end
|
end
|
||||||
@@ -291,38 +295,69 @@ module ClientTests
|
|||||||
|
|
||||||
t3 = Thread.new do
|
t3 = Thread.new do
|
||||||
sleep 0.75
|
sleep 0.75
|
||||||
locked = client.lock(TEST_KEY, 2) { success_counter << 1 }
|
locked = @client.lock { success_counter << 1 }
|
||||||
failure_counter << 1 unless locked
|
failure_counter << 1 unless locked
|
||||||
end
|
end
|
||||||
|
|
||||||
[t1, t2, t3].map(&:join)
|
[t1, t2, t3].each(&:join)
|
||||||
|
|
||||||
assert_equal 2, success_counter.size
|
assert_equal 2, success_counter.size
|
||||||
assert_equal 1, failure_counter.size
|
assert_equal 1, failure_counter.size
|
||||||
assert_equal false, client.locked?(TEST_KEY)
|
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
|
end
|
||||||
|
|
||||||
assert_raises(NotImplementedError) do
|
assert_raises(NotImplementedError) do
|
||||||
@client.send(:set, TEST_KEY, "", "")
|
@client.send(:set, "", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raises(NotImplementedError) do
|
assert_raises(NotImplementedError) do
|
||||||
@client.send(:initial_set, TEST_KEY)
|
@client.send(:initial_set)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raises(NotImplementedError) do
|
assert_raises(NotImplementedError) do
|
||||||
@client.send(:clear, TEST_KEY)
|
@client.send(:clear)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -332,7 +367,7 @@ 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
|
teardown
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -346,7 +381,7 @@ 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
|
teardown
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user