spec/client_spec.rb in redlock-0.1.7 vs spec/client_spec.rb in redlock-0.1.8

- old
+ new

@@ -41,16 +41,25 @@ @lock_info = lock_manager.lock(resource_key, ttl, extend: my_lock_info) expect(@lock_info).to be_lock_info_for(resource_key) expect(@lock_info[:value]).to eq(my_lock_info[:value]) end - 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'}) - expect(@lock_info).to be_lock_info_for(resource_key) - expect(@lock_info[:value]).to eq('hello world') # really we should test what's in redis + context 'when extend_life flag is given' do + it 'does not extend a non-existent lock' do + @lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'}, extend_life: true) + expect(@lock_info).to eq(false) + end end + context 'when extend_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_life: false) + expect(@lock_info).to be_lock_info_for(resource_key) + expect(@lock_info[:value]).to eq('hello world') # really we should test what's in redis + end + end + it "doesn't extend somebody else's lock" do @lock_info = lock_manager.lock(resource_key, ttl) second_attempt = lock_manager.lock(resource_key, ttl) expect(second_attempt).to eq(false) end @@ -168,44 +177,10 @@ expect(resource_key).to be_lockable(lock_manager, ttl) end end - describe "extend" do - context 'when lock is available' do - before { @lock_info = lock_manager.lock(resource_key, ttl) } - after(:each) { lock_manager.unlock(@lock_info) if @lock_info } - - it 'can extend its own lock' do - lock_info = lock_manager.extend_life(@lock_info, ttl) - expect(lock_info).to be_lock_info_for(resource_key) - end - - it "can't extend a nonexistent lock" do - lock_manager.unlock(@lock_info) - lock_info = lock_manager.extend_life(@lock_info, ttl) - expect(lock_info).to eq(false) - end - end - end - - describe "extend!" do - context 'when lock is available' do - before { @lock_info = lock_manager.lock(resource_key, ttl) } - after(:each) { lock_manager.unlock(@lock_info) if @lock_info } - - it 'can extend its own lock' do - expect{ lock_manager.extend_life!(@lock_info, ttl) }.to_not raise_error - end - - it "can't extend a nonexistent lock" do - lock_manager.unlock(@lock_info) - expect{ lock_manager.extend_life!(@lock_info, ttl) }.to raise_error(Redlock::LockError) - end - end - end - describe 'lock!' do context 'when lock is available' do it 'locks' do lock_manager.lock!(resource_key, ttl) do expect(resource_key).to_not be_lockable(lock_manager, ttl) @@ -223,9 +198,14 @@ end it 'automatically unlocks when block raises exception' do lock_manager.lock!(resource_key, ttl) { fail } rescue nil expect(resource_key).to be_lockable(lock_manager, ttl) + end + + it 'passes the extension parameter' do + my_lock_info = lock_manager.lock(resource_key, ttl) + expect{ lock_manager.lock!(resource_key, ttl, extend: my_lock_info){} }.to_not raise_error end end context 'when lock is not available' do before { @another_lock_info = lock_manager.lock(resource_key, ttl) }