Skip to content

Commit

Permalink
Merge pull request #64 from leandromoreira/fix-issue-60-rescue-connec…
Browse files Browse the repository at this point in the history
…tion-error

Fix issue 60 rescue connection error
  • Loading branch information
leandromoreira committed May 7, 2018
2 parents f9ea286 + 23f77b8 commit 6042837
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.evalsha @lock_script_sha, keys: [resource], argv: [val, ttl, allow_new_lock]
end
rescue Redis::CannotConnectError
false
end

def unlock(resource, val)
Expand Down
24 changes: 23 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

describe 'initialize' do
it 'accepts both redis URLs and Redis objects' do
print redis1_host
servers = [ "redis://#{redis1_host}:#{redis1_port}", Redis.new(url: "redis://#{redis2_host}:#{redis2_port}") ]
redlock = Redlock::Client.new(servers)

Expand Down Expand Up @@ -141,6 +140,29 @@
end
end

context 'when a server goes away' do
it 'does not raise an error on connection issues' do
# We re-route the lock manager to a (hopefully) non-existent Redis URL.
redis_instance = lock_manager.instance_variable_get(:@servers).first
redis_instance.instance_variable_set(:@redis, Redis.new(url: 'redis://localhost:46864'))

expect {
expect(lock_manager.lock(resource_key, ttl)).to be_falsey
}.to_not raise_error
end
end

context 'when a server comes back' do
it 'recovers from connection issues' do
# Same as above.
redis_instance = lock_manager.instance_variable_get(:@servers).first
redis_instance.instance_variable_set(:@redis, Redis.new(url: 'redis://localhost:46864'))
expect(lock_manager.lock(resource_key, ttl)).to be_falsey
redis_instance.instance_variable_set(:@redis, Redis.new(url: "redis://#{redis1_host}:#{redis1_port}"))
expect(lock_manager.lock(resource_key, ttl)).to be_truthy
end
end

context 'when script cache has been flushed' do
before(:each) do
@manipulated_instance = lock_manager.instance_variable_get(:@servers).first
Expand Down

0 comments on commit 6042837

Please sign in to comment.