Skip to content

Commit

Permalink
Merge pull request #61 from leandromoreira/ttl-specs
Browse files Browse the repository at this point in the history
TTL specs refactored
  • Loading branch information
maltoe committed Feb 27, 2018
2 parents c99d0f7 + 037595a commit 1e165c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions spec/client_spec.rb
@@ -1,10 +1,12 @@
require 'spec_helper'
require 'securerandom'
require 'redis'

RSpec.describe Redlock::Client do
# It is recommended to have at least 3 servers in production
let(:lock_manager_opts) { { retry_count: 3 } }
let(:lock_manager) { Redlock::Client.new(Redlock::Client::DEFAULT_REDIS_URLS, lock_manager_opts) }
let(:redis_client) { Redis.new }
let(:resource_key) { SecureRandom.hex(3) }
let(:ttl) { 1000 }
let(:redis1_host) { ENV["REDIS1_HOST"] || "localhost" }
Expand Down Expand Up @@ -42,6 +44,12 @@
expect(@lock_info).to be_lock_info_for(resource_key)
end

it 'interprets lock time as milliseconds' do
ttl = 20000
@lock_info = lock_manager.lock(resource_key, ttl)
expect(redis_client.pttl(resource_key)).to be_within(200).of(ttl)
end

it 'can extend its own lock' do
my_lock_info = lock_manager.lock(resource_key, ttl)
@lock_info = lock_manager.lock(resource_key, ttl, extend: my_lock_info)
Expand All @@ -64,6 +72,16 @@
end
end

it '(when extending) resets the TTL, rather than adding extra time to it' do
ttl = 20000
lock_info = lock_manager.lock(resource_key, ttl)
expect(resource_key).to_not be_lockable(lock_manager, ttl)

lock_info = lock_manager.lock(resource_key, ttl, extend: lock_info, extend_life: true)
expect(lock_info).not_to be_nil
expect(redis_client.pttl(resource_key)).to be_within(200).of(ttl)
end

context 'when extend_only_if_life flag is not given' do
it "sets the given value when trying to extend a non-existent lock" do
@lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'}, extend_only_if_life: false)
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -41,3 +41,11 @@ def correct_resource?(actual, resource)
"expected that #{resource_key} would be lockable"
end
end

RSpec.configure do |c|
# NOTE: this protects against erroneous "focus: true" commits
unless ENV['CI'] == 'true'
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end
end

0 comments on commit 1e165c1

Please sign in to comment.