Skip to content

Commit

Permalink
Merge 749e7eb into 6671b35
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansch committed Aug 26, 2015
2 parents 6671b35 + 749e7eb commit feca1af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/redlock/client.rb
Expand Up @@ -30,13 +30,27 @@ def initialize(servers = DEFAULT_REDIS_URLS, options = {})
@retry_delay = options[:retry_delay] || DEFAULT_RETRY_DELAY
end

def testing=(mode)
@testing_mode = mode
end

# Locks a resource for a given time.
# Params:
# +resource+:: the resource (or key) string to be locked.
# +ttl+:: The time-to-live in ms for the lock.
# +block+:: an optional block that automatically unlocks the lock.
def lock(resource, ttl, &block)
lock_info = try_lock_instances(resource, ttl)
if @testing_mode == :bypass
lock_info = {
validity: ttl,
resource: resource,
value: SecureRandom.uuid
}
elsif @testing_mode == :fail
return false
else
lock_info = try_lock_instances(resource, ttl)
end

if block_given?
begin
Expand All @@ -54,6 +68,8 @@ def lock(resource, ttl, &block)
# Params:
# +lock_info+:: the lock that has been acquired when you locked the resource.
def unlock(lock_info)
return if @testing_mode == :bypass

@servers.each { |s| s.unlock(lock_info[:resource], lock_info[:value]) }
end

Expand Down
24 changes: 24 additions & 0 deletions spec/client_spec.rb
Expand Up @@ -94,6 +94,30 @@
end
end
end

context 'when testing with bypass mode' do
before { lock_manager.testing = :bypass }
after { lock_manager.testing = nil }

it 'bypasses the redis servers' do
expect(lock_manager).to_not receive(:try_lock_instances)
lock_manager.lock(resource_key, ttl) do |lock_info|
expect(lock_info).to be_lock_info_for(resource_key)
end
end
end

context 'when testing with fail mode' do
before { lock_manager.testing = :fail }
after { lock_manager.testing = nil }

it 'fails' do
expect(lock_manager).to_not receive(:try_lock_instances)
lock_manager.lock(resource_key, ttl) do |lock_info|
expect(lock_info).to eql(false)
end
end
end
end

describe 'unlock' do
Expand Down

0 comments on commit feca1af

Please sign in to comment.