redness 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- redis.discard
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
@@ -1,4 +1,4 @@
1
1
  module Redness
2
- VERSION="0.1.2"
2
+ VERSION="0.1.3"
3
3
  MAJOR, MINOR, TINY = VERSION.split(".")
4
4
  end
@@ -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(2) { raise Red::RedisUnavailable }.should == 2
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(2) { raise Redis::CannotConnectError }.should == 2
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(2) { raise Redis::TimeoutError }.should == 2
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
- it "should not try call discard if the multi fails" do
20
- Red.redis.stub(:multi).and_raise(Redis::TimeoutError)
21
- red = Red.new
22
- lambda do
23
- red.multi_with_caution{}
24
- end.should_not raise_error
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
- it "should exec the transaction if the block does not raise an exception" do
28
- Red.redis.should_receive(:multi)
29
- Red.redis.should_receive(:exec)
30
- Red.redis.should_not_receive(:discard)
31
- Red.new.multi_with_caution{}
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
- it "should discard the transaction if the block raises an exception" do
35
- error_class = Class.new(RuntimeError)
36
- Red.redis.should_receive(:multi)
37
- Red.redis.should_not_receive(:exec)
38
- Red.redis.should_receive(:discard)
39
- begin
40
- Red.new.multi_with_caution{raise error_class}
41
- rescue error_class
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.2
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-03 00:00:00.000000000 Z
17
+ date: 2012-08-05 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: json