retrying 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/lib/retrying.rb +2 -1
- data/lib/retrying/version.rb +1 -1
- data/spec/retrying_spec.rb +23 -6
- metadata +2 -2
data/README.md
CHANGED
data/lib/retrying.rb
CHANGED
@@ -2,7 +2,7 @@ require 'retrying/version'
|
|
2
2
|
|
3
3
|
module Retrying
|
4
4
|
def retrying(options = {}, &block)
|
5
|
-
options = { :on => StandardError, :tries => 2 }.merge(options)
|
5
|
+
options = { :on => StandardError, :tries => 2, :sleep => 0 }.merge(options)
|
6
6
|
exceptions = Array(options[:on])
|
7
7
|
|
8
8
|
attempts = 0
|
@@ -11,6 +11,7 @@ module Retrying
|
|
11
11
|
rescue *exceptions => ex
|
12
12
|
attempts += 1
|
13
13
|
raise if attempts >= options[:tries]
|
14
|
+
sleep(options[:sleep]) if options[:sleep] > 0
|
14
15
|
retry
|
15
16
|
end
|
16
17
|
end
|
data/lib/retrying/version.rb
CHANGED
data/spec/retrying_spec.rb
CHANGED
@@ -2,6 +2,8 @@ require 'retrying'
|
|
2
2
|
|
3
3
|
include Retrying
|
4
4
|
|
5
|
+
TestError = Class.new(StandardError)
|
6
|
+
|
5
7
|
describe Retrying do
|
6
8
|
describe '#retrying' do
|
7
9
|
it 'retries a block once with no args' do
|
@@ -11,9 +13,9 @@ describe Retrying do
|
|
11
13
|
begin
|
12
14
|
retrying do
|
13
15
|
tries += 1
|
14
|
-
raise
|
16
|
+
raise TestError
|
15
17
|
end
|
16
|
-
rescue
|
18
|
+
rescue TestError
|
17
19
|
raised = true
|
18
20
|
end
|
19
21
|
|
@@ -28,9 +30,9 @@ describe Retrying do
|
|
28
30
|
begin
|
29
31
|
retrying(:tries => 3) do
|
30
32
|
tries += 1
|
31
|
-
raise
|
33
|
+
raise TestError
|
32
34
|
end
|
33
|
-
rescue
|
35
|
+
rescue TestError
|
34
36
|
raised = true
|
35
37
|
end
|
36
38
|
|
@@ -45,14 +47,29 @@ describe Retrying do
|
|
45
47
|
begin
|
46
48
|
retrying(:on => IOError) do
|
47
49
|
tries += 1
|
48
|
-
raise
|
50
|
+
raise TestError
|
49
51
|
end
|
50
|
-
rescue
|
52
|
+
rescue TestError
|
51
53
|
raised = true
|
52
54
|
end
|
53
55
|
|
54
56
|
expect(tries).to eq(1)
|
55
57
|
expect(raised).to be_true
|
56
58
|
end
|
59
|
+
|
60
|
+
it 'sleeps before retrying' do
|
61
|
+
def sleep(time)
|
62
|
+
@slept = time
|
63
|
+
end
|
64
|
+
|
65
|
+
begin
|
66
|
+
retrying(:sleep => 1) do
|
67
|
+
raise TestError
|
68
|
+
end
|
69
|
+
rescue TestError
|
70
|
+
end
|
71
|
+
|
72
|
+
expect(@slept).to eq(1)
|
73
|
+
end
|
57
74
|
end
|
58
75
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retrying
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|