Skip to content

Commit

Permalink
Wrapping all calls that use connection with error handling. First pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Nov 12, 2010
1 parent bf1d9af commit ddd8460
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 75 deletions.
8 changes: 5 additions & 3 deletions spec/adapter/cassandra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

describe "Cassandra adapter" do
before do
@client = Cassandra.new("Keyspace1")
@adapter = Adapter[:cassandra].new(@client, :column_family => 'Standard2')
@adapter.clear
handle_failed_connections do
@client = Cassandra.new("Keyspace1")
@adapter = Adapter[:cassandra].new(@client, :column_family => 'Standard2')
@adapter.clear
end
end

let(:adapter) { @adapter }
Expand Down
8 changes: 5 additions & 3 deletions spec/adapter/memcached_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

describe "Memcached adapter" do
before do
@client = Memcached.new('localhost:11211', :namespace => 'adapter_spec')
@adapter = Adapter[:memcached].new(@client)
@adapter.clear
handle_failed_connections do
@client = Memcached.new('localhost:11211', :namespace => 'adapter_spec')
@adapter = Adapter[:memcached].new(@client)
@adapter.clear
end
end

let(:adapter) { @adapter }
Expand Down
8 changes: 5 additions & 3 deletions spec/adapter/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

describe "Redis adapter" do
before do
@client = Redis.new
@adapter = Adapter[:redis].new(@client)
@adapter.clear
handle_failed_connections do
@client = Redis.new
@adapter = Adapter[:redis].new(@client)
@adapter.clear
end
end

let(:adapter) { @adapter }
Expand Down
36 changes: 22 additions & 14 deletions spec/adapter/riak_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

describe "Riak adapter" do
before do
@client = Riak::Client.new['adapter_spec']
@adapter = Adapter[:riak].new(@client)
@adapter.clear
handle_failed_connections do
@client = Riak::Client.new['adapter_spec']
@adapter = Adapter[:riak].new(@client)
@adapter.clear
end
end

let(:adapter) { @adapter }
Expand All @@ -15,24 +17,30 @@

describe "reading key with conflicts" do
before do
client.allow_mult = true
other_adapter = Adapter[:riak].new(Riak::Client.new['adapter_spec'])
handle_failed_connections do
client.allow_mult = true
other_adapter = Adapter[:riak].new(Riak::Client.new['adapter_spec'])

threads = []
threads << Thread.new { adapter.write('foo', 'bar') }
threads << Thread.new { other_adapter.write('foo', 'baz') }
threads.each(&:join)
threads = []
threads << Thread.new { adapter.write('foo', 'bar') }
threads << Thread.new { other_adapter.write('foo', 'baz') }
threads.each(&:join)
end
end

it "raises conflict error" do
lambda { adapter.read('foo') }.should raise_error(Adapter::Riak::Conflict)
handle_failed_connections do
lambda { adapter.read('foo') }.should raise_error(Adapter::Riak::Conflict)
end
end

it "exposes robject to exception" do
begin
adapter.read('foo')
rescue Adapter::Riak::Conflict => e
e.robject.should be_instance_of(Riak::RObject)
handle_failed_connections do
begin
adapter.read('foo')
rescue Adapter::Riak::Conflict => e
e.robject.should be_instance_of(Riak::RObject)
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def clear
end
end
end

def handle_failed_connections
begin
yield
rescue => e
if e.message =~ /connect/i
pending
end
end
end
end

Spec::Runner.configure do |config|
Expand Down
142 changes: 92 additions & 50 deletions spec/support/an_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,158 @@

AdapterTestTypes.each do |type, (key, key2)|
it "reads from keys that are #{type}s like a Hash" do
adapter[key].should == nil
handle_failed_connections do
adapter[key].should == nil
end
end

it "writes String values to keys that are #{type}s like a Hash" do
adapter[key] = "value"
adapter[key].should == "value"
handle_failed_connections do
adapter[key] = "value"
adapter[key].should == "value"
end
end

it "guarantees that a different String value is retrieved from the #{type} key" do
value = "value"
adapter[key] = value
adapter[key].should_not be_equal(value)
handle_failed_connections do
value = "value"
adapter[key] = value
adapter[key].should_not be_equal(value)
end
end

it "guarantees that a different Object value is retrieved from the #{type} key" do
value = {:foo => :bar}
adapter[key] = value
adapter[key].should_not be_equal(:foo => :bar)
handle_failed_connections do
value = {:foo => :bar}
adapter[key] = value
adapter[key].should_not be_equal(:foo => :bar)
end
end

it "returns false from key? if a #{type} key is not available" do
adapter.key?(key).should be_false
handle_failed_connections do
adapter.key?(key).should be_false
end
end

it "returns true from key? if a #{type} key is available" do
adapter[key] = "value"
adapter.key?(key).should be_true
handle_failed_connections do
adapter[key] = "value"
adapter.key?(key).should be_true
end
end

it "removes and return an element with a #{type} key from the backing store via delete if it exists" do
adapter[key] = "value"
adapter.delete(key).should == "value"
adapter.key?(key).should be_false
handle_failed_connections do
adapter[key] = "value"
adapter.delete(key).should == "value"
adapter.key?(key).should be_false
end
end

it "returns nil from delete if an element for a #{type} key does not exist" do
adapter.delete(key).should be_nil
handle_failed_connections do
adapter.delete(key).should be_nil
end
end

it "removes all #{type} keys from the store with clear" do
adapter[key] = "value"
adapter[key2] = "value2"
adapter.clear
adapter.key?(key).should_not be_true
adapter.key?(key2).should_not be_true
handle_failed_connections do
adapter[key] = "value"
adapter[key2] = "value2"
adapter.clear
adapter.key?(key).should_not be_true
adapter.key?(key2).should_not be_true
end
end

it "fetches a #{type} key with a default value with fetch, if the key is not available" do
adapter.fetch(key, "value").should == "value"
handle_failed_connections do
adapter.fetch(key, "value").should == "value"
end
end

it "fetches a #{type} key with a block with fetch, if the key is not available" do
adapter.fetch(key) { |k| "value" }.should == "value"
handle_failed_connections do
adapter.fetch(key) { |k| "value" }.should == "value"
end
end

it "does not run the block if the #{type} key is available" do
adapter[key] = "value"
unaltered = "unaltered"
adapter.fetch(key) { unaltered = "altered" }
unaltered.should == "unaltered"
handle_failed_connections do
adapter[key] = "value"
unaltered = "unaltered"
adapter.fetch(key) { unaltered = "altered" }
unaltered.should == "unaltered"
end
end

it "fetches a #{type} key with a default value with fetch, if the key is available" do
adapter[key] = "value2"
adapter.fetch(key, "value").should == "value2"
handle_failed_connections do
adapter[key] = "value2"
adapter.fetch(key, "value").should == "value2"
end
end

it "writes #{key} values with #write" do
adapter.write(key, "value")
adapter[key].should == "value"
handle_failed_connections do
adapter.write(key, "value")
adapter[key].should == "value"
end
end
end

it "refuses to #[] from keys that cannot be marshalled" do
lambda do
adapter[Struct.new(:foo).new(:bar)]
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter[Struct.new(:foo).new(:bar)]
end.should raise_error(TypeError)
end
end

it "refuses to fetch from keys that cannot be marshalled" do
lambda do
adapter.fetch(Struct.new(:foo).new(:bar), true)
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter.fetch(Struct.new(:foo).new(:bar), true)
end.should raise_error(TypeError)
end
end

it "refuses to #[]= to keys that cannot be marshalled" do
lambda do
adapter[Struct.new(:foo).new(:bar)] = "value"
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter[Struct.new(:foo).new(:bar)] = "value"
end.should raise_error(TypeError)
end
end

it "refuses to store to keys that cannot be marshalled" do
lambda do
adapter.write Struct.new(:foo).new(:bar), "value"
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter.write Struct.new(:foo).new(:bar), "value"
end.should raise_error(TypeError)
end
end

it "refuses to check for key? if the key cannot be marshalled" do
lambda do
adapter.key? Struct.new(:foo).new(:bar)
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter.key? Struct.new(:foo).new(:bar)
end.should raise_error(TypeError)
end
end

it "refuses to delete a key if the key cannot be marshalled" do
lambda do
adapter.delete Struct.new(:foo).new(:bar)
end.should raise_error(TypeError)
handle_failed_connections do
lambda do
adapter.delete Struct.new(:foo).new(:bar)
end.should raise_error(TypeError)
end
end

it "specifies that it is writable via frozen?" do
adapter.should_not be_frozen
handle_failed_connections do
adapter.should_not be_frozen
end
end
end
6 changes: 4 additions & 2 deletions spec/support/marshal_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

AdapterTestTypes.each do |type, (key, key2)|
it "writes Object values to keys that are #{type}s like a Hash" do
adapter[key] = {:foo => :bar}
adapter[key].should == {:foo => :bar}
handle_failed_connections do
adapter[key] = {:foo => :bar}
adapter[key].should == {:foo => :bar}
end
end
end
end

0 comments on commit ddd8460

Please sign in to comment.