subelsky_power_tools 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # API inspired by http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/
2
+
3
+ module Kernel
4
+ def retryable(options = {})
5
+ opts = { :tries => 1, :on => [StandardError] }.merge(options)
6
+ tries = opts[:tries]
7
+
8
+ exceptions = opts[:on]
9
+ count = 0
10
+ sleep_seconds = opts[:sleep] || 0
11
+
12
+ begin
13
+ count += 1
14
+ yield
15
+ rescue *exceptions => e
16
+ if count < tries
17
+ Kernel.sleep(sleep_seconds)
18
+ retry
19
+ else
20
+ raise e
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module SubelskyPowerTools
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+ require "subelsky_power_tools/ext/kernel"
3
+
4
+ describe "Kernel#retryable" do
5
+ it "retries exceptions for the specified number of times" do
6
+ count = 0
7
+
8
+ begin
9
+ retryable(tries: 3) do
10
+ count += 1
11
+ raise ArgumentError
12
+ end
13
+ rescue ArgumentError
14
+ end
15
+
16
+ count.should == 3
17
+ end
18
+
19
+ it "reraises exceptions after exhausting all tries" do
20
+ expect {
21
+ retryable(tries: 2) { raise ArgumentError }
22
+ }.to raise_error(ArgumentError)
23
+ end
24
+
25
+ it "waits specified number of seconds between tries" do
26
+ # this seems like a better way to measure sleep than using the clock
27
+ Kernel.should_receive(:sleep).with(0.2).exactly(2).times
28
+
29
+ begin
30
+ retryable(tries: 3,sleep: 0.2) { raise ArgumentError }
31
+ rescue ArgumentError
32
+ end
33
+
34
+ end
35
+
36
+ it "only retries given exceptions" do
37
+ expect {
38
+ retryable(tries: 2,on: ScriptError) { raise ZeroDivisionError }
39
+ }.to raise_error(ZeroDivisionError)
40
+ end
41
+
42
+ it "returns result of operation if it ultimately succeeds" do
43
+ result = retryable do
44
+ 49
45
+ end
46
+
47
+ result.should == 49
48
+ end
49
+
50
+ end
data/spec/spec_helper.rb CHANGED
@@ -28,4 +28,5 @@ Spork.prefork do
28
28
  end
29
29
 
30
30
  Spork.each_run do
31
+ #load "subelsky_power_tools/ext/kernel.rb"
31
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subelsky_power_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-05 00:00:00.000000000 -04:00
12
+ date: 2011-08-01 00:00:00.000000000 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2157564940 !ruby/object:Gem::Requirement
17
+ requirement: &2153319800 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2157564940
25
+ version_requirements: *2153319800
26
26
  description: ! "This is a collection of Ruby extensions and utilities I've been carting
27
27
  around from project to project. \nMany are taken from the Facets project (I just
28
28
  don't want to include that whole gem in my codebases).\n"
@@ -44,9 +44,11 @@ files:
44
44
  - lib/subelsky_power_tools.rb
45
45
  - lib/subelsky_power_tools/ext/exception.rb
46
46
  - lib/subelsky_power_tools/ext/hash.rb
47
+ - lib/subelsky_power_tools/ext/kernel.rb
47
48
  - lib/subelsky_power_tools/version.rb
48
49
  - spec/exception_spec.rb
49
50
  - spec/hash_spec.rb
51
+ - spec/kernel_spec.rb
50
52
  - spec/spec_helper.rb
51
53
  - subelsky_power_tools.gemspec
52
54
  has_rdoc: true
@@ -80,4 +82,5 @@ summary: This is a collection of Ruby extensions and utilities I've been carting
80
82
  test_files:
81
83
  - spec/exception_spec.rb
82
84
  - spec/hash_spec.rb
85
+ - spec/kernel_spec.rb
83
86
  - spec/spec_helper.rb