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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # Retrying
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/retrying.png)](http://badge.fury.io/rb/retrying)
4
+
3
5
  Runs a code block, and retries it when an exception occurs.
4
6
 
5
7
  It's configured using optional parameters `:tries` and `:on`.
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Retrying
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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 FloatDomainError
16
+ raise TestError
15
17
  end
16
- rescue FloatDomainError
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 StandardError
33
+ raise TestError
32
34
  end
33
- rescue StandardError
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 FloatDomainError
50
+ raise TestError
49
51
  end
50
- rescue FloatDomainError
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.1
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-16 00:00:00.000000000 Z
12
+ date: 2013-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler