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 +10 -13
- data/lib/redness/version.rb +1 -1
- data/spec/redness/red_spec.rb +8 -21
- metadata +1 -1
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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)
|
data/lib/redness/version.rb
CHANGED
data/spec/redness/red_spec.rb
CHANGED
@@ -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
|
33
|
-
|
34
|
-
|
35
|
-
|
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)
|