redness 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/redness/red.rb +8 -2
- data/lib/redness/version.rb +1 -1
- data/spec/redness/red_spec.rb +55 -22
- metadata +2 -2
data/lib/redness/red.rb
CHANGED
@@ -26,12 +26,18 @@ class Red
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def multi_with_caution(fail_return = [])
|
29
|
-
redis.multi rescue return
|
29
|
+
redis.multi rescue return fail_return
|
30
30
|
begin
|
31
31
|
yield
|
32
32
|
redis.exec
|
33
33
|
rescue
|
34
|
-
|
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
|
35
41
|
fail_return
|
36
42
|
end
|
37
43
|
end
|
data/lib/redness/version.rb
CHANGED
data/spec/redness/red_spec.rb
CHANGED
@@ -3,42 +3,75 @@ require_relative '../spec_integration_helper'
|
|
3
3
|
describe Red do
|
4
4
|
describe "#execute_with_uncertainty" do
|
5
5
|
it "should return the given value if the block raises a Red::RedisUnavailable" do
|
6
|
-
Red.new.execute_with_uncertainty(
|
6
|
+
Red.new.execute_with_uncertainty(:boom) { raise Red::RedisUnavailable }.should == :boom
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should return the given value if the block raises a Redis::CannotConnectError" do
|
10
|
-
Red.new.execute_with_uncertainty(
|
10
|
+
Red.new.execute_with_uncertainty(:boom) { raise Redis::CannotConnectError }.should == :boom
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return the given value if the block raises a Redis::TimeoutError" do
|
14
|
-
Red.new.execute_with_uncertainty(
|
14
|
+
Red.new.execute_with_uncertainty(:boom) { raise Redis::TimeoutError }.should == :boom
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "#multi_with_caution" do
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
context "when the multi fails" do
|
20
|
+
before do
|
21
|
+
Red.redis.stub(:multi).and_raise(Redis::TimeoutError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return the failure result given, if any" do
|
25
|
+
Red.new.multi_with_caution(:boom){}.should == :boom
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return an empty array if no failure result is given" do
|
29
|
+
Red.new.multi_with_caution { raise error_class }.should == []
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not call discard" do
|
33
|
+
lambda do
|
34
|
+
Red.new.multi_with_caution{}
|
35
|
+
end.should_not raise_error
|
36
|
+
end
|
25
37
|
end
|
26
38
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
39
|
+
describe "when the block raises an exception" do
|
40
|
+
let(:error_class) { Class.new(RuntimeError) }
|
41
|
+
|
42
|
+
it "should return the failure result given, if any" do
|
43
|
+
Red.new.multi_with_caution(:boom) { raise error_class }.should == :boom
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return an empty array if no failure result is given" do
|
47
|
+
Red.new.multi_with_caution { raise error_class }.should == []
|
48
|
+
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
|
32
59
|
end
|
33
60
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
61
|
+
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
|
+
it "should return the results of the exec'd commands" do
|
70
|
+
result = Red.new.multi_with_caution do
|
71
|
+
Red.redis.set('a', 1)
|
72
|
+
Red.redis.set('b', 1)
|
73
|
+
end
|
74
|
+
result.size.should == 2
|
42
75
|
end
|
43
76
|
end
|
44
77
|
end
|
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.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ authors:
|
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date: 2012-08-
|
17
|
+
date: 2012-08-05 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: json
|