|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | | -describe 'Kernel.retryable' do |
| 3 | +RSpec.describe 'Kernel.retryable' do |
4 | 4 | before(:each) do |
5 | 5 | Retryable.enable |
6 | 6 | @attempt = 0 |
7 | 7 | end |
8 | 8 |
|
9 | 9 | it 'catch StandardError only by default' do |
10 | | - lambda do |
| 10 | + expect do |
11 | 11 | count_retryable(:tries => 2) { |tries, ex| raise Exception if tries < 1 } |
12 | | - end.should raise_error Exception |
13 | | - @try_count.should == 1 |
| 12 | + end.to raise_error Exception |
| 13 | + expect(@try_count).to eq(1) |
14 | 14 | end |
15 | 15 |
|
16 | 16 | it 'retries on default exception' do |
17 | | - Kernel.should_receive(:sleep).once.with(1) |
| 17 | + expect(Kernel).to receive(:sleep).once.with(1) |
18 | 18 |
|
19 | 19 | count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 } |
20 | | - @try_count.should == 2 |
| 20 | + expect(@try_count).to eq(2) |
21 | 21 | end |
22 | 22 |
|
23 | 23 | it 'does not retry if disabled' do |
24 | 24 | Retryable.disable |
25 | 25 |
|
26 | | - lambda do |
| 26 | + expect do |
27 | 27 | count_retryable(:tries => 2) { raise } |
28 | | - end.should raise_error RuntimeError |
29 | | - @try_count.should == 1 |
| 28 | + end.to raise_error RuntimeError |
| 29 | + expect(@try_count).to eq(1) |
30 | 30 | end |
31 | 31 |
|
32 | 32 | it 'executes *ensure* clause' do |
33 | 33 | ensure_cb = Proc.new do |retries| |
34 | | - retries.should == 0 |
| 34 | + expect(retries).to eq(0) |
35 | 35 | end |
36 | 36 |
|
37 | 37 | Kernel.retryable(:ensure => ensure_cb) { } |
38 | 38 | end |
39 | 39 |
|
40 | 40 | it 'passes retry count and exception on retry' do |
41 | | - Kernel.should_receive(:sleep).once.with(1) |
| 41 | + expect(Kernel).to receive(:sleep).once.with(1) |
42 | 42 |
|
43 | 43 | count_retryable(:tries => 2) do |tries, ex| |
44 | | - ex.class.should == StandardError if tries > 0 |
| 44 | + expect(ex.class).to eq(StandardError) if tries > 0 |
45 | 45 | raise StandardError if tries < 1 |
46 | 46 | end |
47 | | - @try_count.should == 2 |
| 47 | + expect(@try_count).to eq(2) |
48 | 48 | end |
49 | 49 |
|
50 | 50 | it 'makes another try if exception is covered by :on' do |
51 | | - Kernel.stub(:sleep) |
| 51 | + allow(Kernel).to receive(:sleep) |
52 | 52 | count_retryable(:on => [StandardError, ArgumentError, RuntimeError] ) { |tries, ex| raise ArgumentError if tries < 1 } |
53 | | - @try_count.should == 2 |
| 53 | + expect(@try_count).to eq(2) |
54 | 54 | end |
55 | 55 |
|
56 | 56 | it 'does not try on unexpected exception' do |
57 | | - Kernel.stub(:sleep) |
58 | | - lambda do |
| 57 | + allow(Kernel).to receive(:sleep) |
| 58 | + expect do |
59 | 59 | count_retryable(:on => RuntimeError ) { |tries, ex| raise StandardError if tries < 1 } |
60 | | - end.should raise_error StandardError |
61 | | - @try_count.should == 1 |
| 60 | + end.to raise_error StandardError |
| 61 | + expect(@try_count).to eq(1) |
62 | 62 | end |
63 | 63 |
|
64 | 64 | it 'retries three times' do |
65 | | - Kernel.stub(:sleep) |
| 65 | + allow(Kernel).to receive(:sleep) |
66 | 66 | count_retryable(:tries => 3) { |tries, ex| raise StandardError if tries < 2 } |
67 | | - @try_count.should == 3 |
| 67 | + expect(@try_count).to eq(3) |
68 | 68 | end |
69 | 69 |
|
70 | 70 | it 'retries on default exception' do |
71 | | - Kernel.should_receive(:sleep).once.with(1) |
| 71 | + expect(Kernel).to receive(:sleep).once.with(1) |
72 | 72 |
|
73 | 73 | count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 } |
74 | | - @try_count.should == 2 |
| 74 | + expect(@try_count).to eq(2) |
75 | 75 | end |
76 | 76 |
|
77 | 77 | it 'executes exponential backoff scheme for :sleep option' do |
78 | | - [1, 4, 16, 64].each { |i| Kernel.should_receive(:sleep).once.ordered.with(i) } |
79 | | - lambda do |
| 78 | + [1, 4, 16, 64].each { |i| expect(Kernel).to receive(:sleep).once.ordered.with(i) } |
| 79 | + expect do |
80 | 80 | Kernel.retryable(:tries => 5, :sleep => lambda { |n| 4**n }) { raise RangeError } |
81 | | - end.should raise_error RangeError |
| 81 | + end.to raise_error RangeError |
82 | 82 | end |
83 | 83 |
|
84 | 84 | it 'does not retry any exception if :on is empty list' do |
85 | | - lambda do |
| 85 | + expect do |
86 | 86 | count_retryable(:on => []) { raise } |
87 | | - end.should raise_error RuntimeError |
88 | | - @try_count.should == 1 |
| 87 | + end.to raise_error RuntimeError |
| 88 | + expect(@try_count).to eq(1) |
89 | 89 | end |
90 | 90 |
|
91 | 91 | it 'catches an exception that matches the regex' do |
92 | | - Kernel.should_receive(:sleep).once.with(1) |
| 92 | + expect(Kernel).to receive(:sleep).once.with(1) |
93 | 93 | count_retryable(:matching => /IO timeout/) { |c,e| raise "yo, IO timeout!" if c == 0 } |
94 | | - @try_count.should == 2 |
| 94 | + expect(@try_count).to eq(2) |
95 | 95 | end |
96 | 96 |
|
97 | 97 | it 'does not catch an exception that does not match the regex' do |
98 | | - should_not_receive :sleep |
99 | | - lambda do |
| 98 | + expect(Kernel).not_to receive(:sleep) |
| 99 | + expect do |
100 | 100 | count_retryable(:matching => /TimeError/) { raise "yo, IO timeout!" } |
101 | | - end.should raise_error RuntimeError |
102 | | - @try_count.should == 1 |
| 101 | + end.to raise_error RuntimeError |
| 102 | + expect(@try_count).to eq(1) |
103 | 103 | end |
104 | 104 |
|
105 | 105 | it 'does not allow invalid options' do |
106 | | - lambda do |
| 106 | + expect do |
107 | 107 | retryable(:bad_option => 2) { raise "this is bad" } |
108 | | - end.should raise_error ArgumentError, '[Retryable] Invalid options: bad_option' |
| 108 | + end.to raise_error ArgumentError, '[Retryable] Invalid options: bad_option' |
109 | 109 | end |
110 | 110 |
|
111 | 111 | it 'accepts a callback to run after an exception is rescued' do |
112 | | - lambda do |
| 112 | + expect do |
113 | 113 | retryable(:sleep => 0, :exception_cb => Proc.new {|e| @raised = e.to_s }) {|tries, ex| raise StandardError.new("this is fun!") if tries < 1 } |
114 | | - end.should_not raise_error |
| 114 | + end.not_to raise_error |
115 | 115 |
|
116 | | - @raised.should == "this is fun!" |
| 116 | + expect(@raised).to eq("this is fun!") |
117 | 117 | end |
118 | 118 | end |
0 commit comments