Permalink
Browse files

Don't discard transaction if multi fails.

Also slap some tests on #multi_with_caution.
  • Loading branch information...
1 parent 57bc3ed commit 02d8fee977f4d3d8dfb158dd614e8b7235e58a42 @oggy oggy committed Aug 3, 2012
Showing with 39 additions and 6 deletions.
  1. +8 −6 lib/redness/red.rb
  2. +31 −0 spec/redness/red_spec.rb
View
@@ -26,12 +26,14 @@ def execute_with_uncertainty(fail_return = [])
end
def multi_with_caution(fail_return = [])
- redis.multi
- yield
- redis.exec
- rescue
- redis.discard
- fail_return
+ redis.multi rescue return
+ begin
+ yield
+ redis.exec
+ rescue
+ redis.discard
+ fail_return
+ end
end
def method_missing(method, *args)
View
@@ -0,0 +1,31 @@
+require_relative '../spec_integration_helper'
+
+describe Red do
+ describe "#multi_with_caution" do
+ it "should not try call discard if the multi fails" do
+ Red.redis.stub(:multi).and_raise(Redis::TimeoutError)
+ red = Red.new
+ lambda do
+ red.multi_with_caution{}
+ end.should_not raise_error
+ end
+
+ it "should exec the transaction if the block does not raise an exception" do
+ Red.redis.should_receive(:multi)
+ Red.redis.should_receive(:exec)
+ Red.redis.should_not_receive(:discard)
+ Red.new.multi_with_caution{}
+ end
+
+ it "should discard the transaction if the block raises an exception" do
+ error_class = Class.new(RuntimeError)
+ Red.redis.should_receive(:multi)
+ Red.redis.should_not_receive(:exec)
+ Red.redis.should_receive(:discard)
+ begin
+ Red.new.multi_with_caution{raise error_class}
+ rescue error_class
+ end
+ end
+ end
+end

0 comments on commit 02d8fee

Please sign in to comment.