redness 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redness/red.rb CHANGED
@@ -25,21 +25,18 @@ class Red
25
25
  fail_return
26
26
  end
27
27
 
28
- def multi_with_caution(fail_return = [])
29
- redis.multi rescue return fail_return
28
+ def multi_with_caution(fail_return = [], &block)
30
29
  begin
31
- yield
32
- redis.exec
33
- rescue
34
- begin
35
- redis.discard
36
- rescue Redis::CommandError
37
- # It's possible the multi failed, but didn't raise an exception -
38
- # perhaps due to write(2) not being reattempted in the redis client
39
- # (likely a bug).
40
- end
41
- fail_return
30
+ redis.multi(&block) || fail_return
31
+ rescue Redis::TimeoutError
32
+ # The redis client pipelines the commands internally. It's possible the
33
+ # MULTI succeeds, but there's a timeout before reaching the EXEC. Try to
34
+ # issue an extra discard to ensure the transaction is closed.
35
+ redis.discard # may raise Redis::CommandError if MULTI never succeeded
36
+ raise
42
37
  end
38
+ rescue
39
+ fail_return
43
40
  end
44
41
 
45
42
  def method_missing(method, *args)
@@ -1,4 +1,4 @@
1
1
  module Redness
2
- VERSION="0.1.3"
2
+ VERSION="0.1.4"
3
3
  MAJOR, MINOR, TINY = VERSION.split(".")
4
4
  end
@@ -29,10 +29,14 @@ describe Red do
29
29
  Red.new.multi_with_caution { raise error_class }.should == []
30
30
  end
31
31
 
32
- it "should not call discard" do
33
- lambda do
34
- Red.new.multi_with_caution{}
35
- end.should_not raise_error
32
+ it "should attempt to discard the transaction in case it's incomplete" do
33
+ Red.redis.should_receive(:discard)
34
+ Red.new.multi_with_caution{}
35
+ end
36
+
37
+ it "should handle a Redis::CommandError when discarding the transaction, in case the MULTI never fired" do
38
+ Red.redis.stub(:discard).and_raise(Redis::CommandError)
39
+ -> { Red.new.multi_with_caution{} }.should_not raise_error
36
40
  end
37
41
  end
38
42
 
@@ -46,26 +50,9 @@ describe Red do
46
50
  it "should return an empty array if no failure result is given" do
47
51
  Red.new.multi_with_caution { raise error_class }.should == []
48
52
  end
49
-
50
- it "should discard the transaction" do
51
- Red.redis.should_receive(:multi)
52
- Red.redis.should_not_receive(:exec)
53
- Red.redis.should_receive(:discard)
54
- begin
55
- Red.new.multi_with_caution{raise error_class}
56
- rescue error_class
57
- end
58
- end
59
53
  end
60
54
 
61
55
  context "when the block does not raise an exception" do
62
- it "should exec the transaction" do
63
- Red.redis.should_receive(:multi)
64
- Red.redis.should_receive(:exec)
65
- Red.redis.should_not_receive(:discard)
66
- Red.new.multi_with_caution{}
67
- end
68
-
69
56
  it "should return the results of the exec'd commands" do
70
57
  result = Red.new.multi_with_caution do
71
58
  Red.redis.set('a', 1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: