Skip to content

Commit

Permalink
Added some more failure handling stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Dec 31, 2010
1 parent 39e4cee commit 53463d4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
46 changes: 28 additions & 18 deletions spec/adapter/memcached_spec.rb
Expand Up @@ -20,44 +20,54 @@
let(:lock_value) { 'locked' }

it "defaults expiration to 1" do
client.should_receive(:add).with(lock_key.to_s, lock_value, 1)
adapter.lock(lock_key) { }
handle_failed_connections do
client.should_receive(:add).with(lock_key.to_s, lock_value, 1)
adapter.lock(lock_key) { }
end
end

it "allows setting expiration" do
client.should_receive(:add).with(lock_key.to_s, lock_value, 5)
adapter.lock(lock_key, :expiration => 5) { }
handle_failed_connections do
client.should_receive(:add).with(lock_key.to_s, lock_value, 5)
adapter.lock(lock_key, :expiration => 5) { }
end
end

describe "with no existing lock" do
it "acquires lock, performs block, and clears lock" do
result = false
adapter.lock(lock_key) { result = true }
handle_failed_connections do
result = false
adapter.lock(lock_key) { result = true }

result.should be_true
adapter.read(lock_key).should be_nil
result.should be_true
adapter.read(lock_key).should be_nil
end
end
end

describe "with lock set" do
it "waits for unlock, performs block, and clears lock" do
result = false
client.add(lock_key.to_s, lock_value, 1)
adapter.lock(lock_key, :timeout => 2) { result = true }
handle_failed_connections do
result = false
client.add(lock_key.to_s, lock_value, 1)
adapter.lock(lock_key, :timeout => 2) { result = true }

result.should be_true
adapter.read(lock_key).should be_nil
result.should be_true
adapter.read(lock_key).should be_nil
end
end
end

describe "with lock set that does not expire before timeout" do
it "raises lock timeout error" do
result = false
client.add(lock_key.to_s, lock_value, 2)
handle_failed_connections do
result = false
client.add(lock_key.to_s, lock_value, 2)

lambda do
adapter.lock(lock_key, :timeout => 1) { result = true }
end.should raise_error(Adapter::LockTimeout, 'Timeout on lock add_game exceeded 1 sec')
lambda do
adapter.lock(lock_key, :timeout => 1) { result = true }
end.should raise_error(Adapter::LockTimeout, 'Timeout on lock add_game exceeded 1 sec')
end
end
end
end
Expand Down
58 changes: 34 additions & 24 deletions spec/adapter/redis_spec.rb
Expand Up @@ -19,52 +19,62 @@
let(:lock_key) { :add_game }

it "defaults expiration to 1" do
now = Time.mktime(2010, 10, 10, 5, 5, 5)
Timecop.freeze(now) do
expiration = now.to_i + 1
client.should_receive(:setnx).with(lock_key.to_s, expiration).and_return(true)
adapter.lock(lock_key) { }
handle_failed_connections do
now = Time.mktime(2010, 10, 10, 5, 5, 5)
Timecop.freeze(now) do
expiration = now.to_i + 1
client.should_receive(:setnx).with(lock_key.to_s, expiration).and_return(true)
adapter.lock(lock_key) { }
end
end
end

it "allows setting expiration" do
now = Time.mktime(2010, 10, 10, 5, 5, 5)
Timecop.freeze(now) do
expiration = now.to_i + 5
client.should_receive(:setnx).with(lock_key.to_s, expiration).and_return(true)
adapter.lock(lock_key, :expiration => 5) { }
handle_failed_connections do
now = Time.mktime(2010, 10, 10, 5, 5, 5)
Timecop.freeze(now) do
expiration = now.to_i + 5
client.should_receive(:setnx).with(lock_key.to_s, expiration).and_return(true)
adapter.lock(lock_key, :expiration => 5) { }
end
end
end

describe "with no existing lock" do
it "acquires lock, performs block, and clears lock" do
result = false
adapter.lock(lock_key) { result = true }
handle_failed_connections do
result = false
adapter.lock(lock_key) { result = true }

result.should be_true
adapter.read(lock_key).should be_nil
result.should be_true
adapter.read(lock_key).should be_nil
end
end
end

describe "with lock set" do
it "waits for unlock, performs block, and clears lock" do
result = false
client.set(lock_key.to_s, adapter.generate_expiration(1))
adapter.lock(lock_key, :timeout => 2) { result = true }
handle_failed_connections do
result = false
client.set(lock_key.to_s, adapter.generate_expiration(1))
adapter.lock(lock_key, :timeout => 2) { result = true }

result.should be_true
adapter.read(lock_key).should be_nil
result.should be_true
adapter.read(lock_key).should be_nil
end
end
end

describe "with lock set that does not expire before timeout" do
it "raises lock timeout error" do
result = false
client.set(lock_key.to_s, adapter.generate_expiration(2))
handle_failed_connections do
result = false
client.set(lock_key.to_s, adapter.generate_expiration(2))

lambda do
adapter.lock(lock_key, :timeout => 1) { result = true }
end.should raise_error(Adapter::LockTimeout, 'Timeout on lock add_game exceeded 1 sec')
lambda do
adapter.lock(lock_key, :timeout => 1) { result = true }
end.should raise_error(Adapter::LockTimeout, 'Timeout on lock add_game exceeded 1 sec')
end
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions spec/helper.rb
Expand Up @@ -43,13 +43,11 @@ def clear
end

def handle_failed_connections
begin
yield
rescue => e
if e.message =~ /connect/i
pending
end
end
yield
rescue => e
puts e.inspect
puts e.message unless e.message.nil?
pending
end
end

Expand Down

0 comments on commit 53463d4

Please sign in to comment.