retry-this 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/retry_this.rb +7 -4
- data/test/retry_this_test.rb +63 -11
- metadata +6 -4
data/lib/retry_this.rb
CHANGED
@@ -16,15 +16,18 @@
|
|
16
16
|
|
17
17
|
module RetryThis
|
18
18
|
def retry_this(options = {})
|
19
|
-
|
19
|
+
max_attempts = 1 + (options[:times] || 1)
|
20
20
|
error_types = options[:error_types] || [StandardError]
|
21
|
-
(1
|
21
|
+
(1..max_attempts).inject(nil) do |result, attempt_number|
|
22
22
|
begin
|
23
|
-
return yield if block_given?
|
23
|
+
return yield attempt_number if block_given?
|
24
24
|
rescue *error_types => e
|
25
|
-
raise e
|
25
|
+
raise e if attempt_number == max_attempts
|
26
|
+
sleep options[:sleep] if options[:sleep]
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
30
|
+
|
31
|
+
module_function :retry_this
|
29
32
|
end
|
30
33
|
|
data/test/retry_this_test.rb
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
=end
|
16
16
|
|
17
17
|
require 'test/unit'
|
18
|
-
require File.
|
18
|
+
require File.expand_path('../lib/retry_this', File.dirname(__FILE__))
|
19
19
|
|
20
20
|
class RetryThisTest < Test::Unit::TestCase
|
21
21
|
include RetryThis
|
@@ -23,12 +23,12 @@ class RetryThisTest < Test::Unit::TestCase
|
|
23
23
|
def test_should_retry_block_for_given_number_of_times_for_given_error_types_and_raise_error_if_all_attempts_fail
|
24
24
|
attempts = 0
|
25
25
|
retry_this(:times => 3, :error_types => [TypeError, RuntimeError]) do
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
attempts += 1
|
27
|
+
if attempts % 2 == 0
|
28
|
+
raise 'some runtime error'
|
29
|
+
else
|
30
|
+
'hi' + 1 # TypeError
|
31
|
+
end
|
32
32
|
end
|
33
33
|
rescue => e
|
34
34
|
assert e.kind_of?(TypeError) || e.kind_of?(RuntimeError)
|
@@ -38,8 +38,8 @@ class RetryThisTest < Test::Unit::TestCase
|
|
38
38
|
def test_should_retry_block_for_default_1_time_for_default_standard_error_and_raise_error_if_all_attempts_fail
|
39
39
|
attempts = 0
|
40
40
|
retry_this do
|
41
|
-
|
42
|
-
|
41
|
+
attempts += 1
|
42
|
+
raise StandardError, 'some error'
|
43
43
|
end
|
44
44
|
rescue => e
|
45
45
|
assert_equal('some error', e.message)
|
@@ -49,8 +49,8 @@ class RetryThisTest < Test::Unit::TestCase
|
|
49
49
|
def test_should_pass_on_an_error_that_is_not_among_given_errors
|
50
50
|
attempts = 0
|
51
51
|
retry_this(:error_types => [TypeError]) do
|
52
|
-
|
53
|
-
|
52
|
+
attempts += 1
|
53
|
+
raise StandardError, 'some error'
|
54
54
|
end
|
55
55
|
rescue => e
|
56
56
|
assert_equal('some error', e.message)
|
@@ -67,4 +67,56 @@ class RetryThisTest < Test::Unit::TestCase
|
|
67
67
|
assert_equal('hi', value)
|
68
68
|
assert_equal(1 + 1, attempts)
|
69
69
|
end
|
70
|
+
|
71
|
+
def test_should_return_value_of_block_in_one_attempt_if_no_error
|
72
|
+
attempts = 0
|
73
|
+
value = retry_this(:error_types => [IOError]) do
|
74
|
+
attempts += 1
|
75
|
+
'hi'
|
76
|
+
end
|
77
|
+
assert_equal('hi', value)
|
78
|
+
assert_equal(1, attempts)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_should_allow_block_to_return_explicity_using_return_keyword_if_invoked_from_method
|
82
|
+
klass = Class.new do
|
83
|
+
include RetryThis
|
84
|
+
attr_reader :attempts
|
85
|
+
def do_something
|
86
|
+
@attempts ||= 0
|
87
|
+
value = retry_this do
|
88
|
+
@attempts += 1
|
89
|
+
return 'hi'
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
object = klass.new
|
94
|
+
assert_equal('hi', object.do_something)
|
95
|
+
assert_equal(1, object.attempts)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_should_expose_retry_this_as_module_function
|
99
|
+
assert RetryThis.respond_to? :retry_this
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_should_yield_attempt_number_to_block
|
103
|
+
attempts = []
|
104
|
+
retry_this(:times => 3) do |attempt_number|
|
105
|
+
attempts << attempt_number
|
106
|
+
raise StandardError, 'some error'
|
107
|
+
end
|
108
|
+
rescue
|
109
|
+
assert_equal([1, 2, 3, 4], attempts)
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_should_sleep_for_specified_seconds_before_a_retry
|
113
|
+
sleep_durations = []
|
114
|
+
class << self; self; end.send(:define_method, :sleep) { |secs| sleep_durations << secs }
|
115
|
+
|
116
|
+
retry_this(:times => 3, :sleep => 10) do
|
117
|
+
raise StandardError, 'some error'
|
118
|
+
end
|
119
|
+
rescue
|
120
|
+
assert_equal([10, 10, 10], sleep_durations)
|
121
|
+
end
|
70
122
|
end
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
version: "1.
|
7
|
+
- 1
|
8
|
+
version: "1.1"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Aman King
|
@@ -13,7 +13,7 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date:
|
16
|
+
date: 2010-12-01 00:00:00 +05:30
|
17
17
|
default_executable:
|
18
18
|
dependencies: []
|
19
19
|
|
@@ -40,6 +40,7 @@ rdoc_options: []
|
|
40
40
|
require_paths:
|
41
41
|
- lib
|
42
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
43
44
|
requirements:
|
44
45
|
- - ">="
|
45
46
|
- !ruby/object:Gem::Version
|
@@ -47,6 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
48
|
- 0
|
48
49
|
version: "0"
|
49
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
50
52
|
requirements:
|
51
53
|
- - ">="
|
52
54
|
- !ruby/object:Gem::Version
|
@@ -56,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
58
|
requirements: []
|
57
59
|
|
58
60
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.3.
|
61
|
+
rubygems_version: 1.3.7
|
60
62
|
signing_key:
|
61
63
|
specification_version: 3
|
62
64
|
summary: RetryThis provides a method that takes a block which it will invoke a given number of times before giving up for specified errors
|