retryable 1.2.4 → 1.2.5
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/spec/lib/retryable_spec.rb +86 -0
- data/spec/spec_helper.rb +9 -0
- metadata +8 -5
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'retryable' do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@attempt = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should retry on default exception" do
|
10
|
+
Kernel.should_receive(:sleep).once.with(1)
|
11
|
+
|
12
|
+
count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 }
|
13
|
+
@try_count.should == 2
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should make another try if exception is covered by :on" do
|
17
|
+
Kernel.stub!(:sleep)
|
18
|
+
count_retryable(:on => [StandardError, ArgumentError, RuntimeError] ) { |tries, ex| raise ArgumentError if tries < 1 }
|
19
|
+
@try_count.should == 2
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not try on unexpected exception" do
|
23
|
+
Kernel.stub!(:sleep)
|
24
|
+
lambda do
|
25
|
+
count_retryable(:on => RuntimeError ) { |tries, ex| raise StandardError if tries < 1 }
|
26
|
+
end.should raise_error StandardError
|
27
|
+
@try_count.should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should three times" do
|
31
|
+
Kernel.stub!(:sleep)
|
32
|
+
count_retryable(:tries => 3) { |tries, ex| raise StandardError if tries < 2 }
|
33
|
+
@try_count.should == 3
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should retry on default exception" do
|
37
|
+
Kernel.should_receive(:sleep).once.with(1)
|
38
|
+
|
39
|
+
count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 }
|
40
|
+
@try_count.should == 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it "exponential backoff scheme for :sleep option" do
|
44
|
+
[1, 4, 16, 64].each { |i| Kernel.should_receive(:sleep).once.ordered.with(i) }
|
45
|
+
lambda do
|
46
|
+
Kernel.retryable(:tries => 5, :sleep => lambda { |n| 4**n }) { raise RangeError }
|
47
|
+
end.should raise_error RangeError
|
48
|
+
end
|
49
|
+
|
50
|
+
it "doesn't retry any exception if :on is empty list" do
|
51
|
+
lambda do
|
52
|
+
count_retryable(:on => []) { raise }
|
53
|
+
end.should raise_error RuntimeError
|
54
|
+
@try_count.should == 1
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should catch an exception that matches the regex" do
|
58
|
+
Kernel.should_receive(:sleep).once.with(1)
|
59
|
+
count_retryable(:matching => /IO timeout/) { |c,e| raise "yo, IO timeout!" if c == 0 }
|
60
|
+
@try_count.should == 2
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not catch an exception that doesn't match the regex" do
|
64
|
+
should_not_receive :sleep
|
65
|
+
lambda do
|
66
|
+
count_retryable(:matching => /TimeError/) { raise "yo, IO timeout!" }
|
67
|
+
end.should raise_error RuntimeError
|
68
|
+
@try_count.should == 1
|
69
|
+
end
|
70
|
+
|
71
|
+
it "doesn't allow invalid options" do
|
72
|
+
lambda do
|
73
|
+
retryable(:bad_option => 2) { raise "this is bad" }
|
74
|
+
end.should raise_error InvalidRetryableOptions
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def count_retryable *opts
|
80
|
+
@try_count = 0
|
81
|
+
return Kernel.retryable(*opts) do |*args|
|
82
|
+
@try_count += 1
|
83
|
+
yield *args
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retryable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 5
|
10
|
+
version: 1.2.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nikita Fedyashev
|
@@ -76,6 +76,8 @@ extra_rdoc_files:
|
|
76
76
|
files:
|
77
77
|
- lib/retryable.rb
|
78
78
|
- README.markdown
|
79
|
+
- spec/lib/retryable_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
79
81
|
homepage: http://github.com/nfedyashev/retryable
|
80
82
|
licenses:
|
81
83
|
- MIT
|
@@ -111,6 +113,7 @@ rubygems_version: 1.7.1
|
|
111
113
|
signing_key:
|
112
114
|
specification_version: 3
|
113
115
|
summary: Kernel#retryable, allow for retrying of code blocks.
|
114
|
-
test_files:
|
115
|
-
|
116
|
+
test_files:
|
117
|
+
- spec/lib/retryable_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
116
119
|
has_rdoc:
|