rescue_me 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/rescue_me/version.rb +1 -1
- data/spec/rescue_me_spec.rb +38 -63
- metadata +1 -2
- data/.document +0 -5
data/Rakefile
CHANGED
data/lib/rescue_me/version.rb
CHANGED
data/spec/rescue_me_spec.rb
CHANGED
@@ -1,85 +1,60 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
require 'net/smtp'
|
7
|
-
attr_reader :method_called_count
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
@method_called_count = 0
|
11
|
-
end
|
12
|
-
|
13
|
-
def exception_free
|
14
|
-
@method_called_count += 1
|
15
|
-
"This method does not raise exceptions"
|
16
|
-
end
|
17
|
-
|
18
|
-
def raise_zero_division_error
|
19
|
-
@method_called_count += 1
|
20
|
-
12/0
|
21
|
-
end
|
22
|
-
|
23
|
-
# Will raise SMTPServerBusy the first x times this method is called,
|
24
|
-
# after which this method does nothing.
|
25
|
-
def raise_smtp_exception_until_call(times)
|
26
|
-
@method_called_count += 1
|
27
|
-
@smtp_exception_count = (@smtp_exception_count ||= 0) + 1
|
28
|
-
raise Net::SMTPServerBusy if times > @smtp_exception_count
|
29
|
-
end
|
30
|
-
|
31
|
-
end # ExceptionCounter
|
3
|
+
class TestLogger # required because RSpec mocks have their own #warn
|
4
|
+
def warn(msg); STDERR.puts msg; end
|
5
|
+
end
|
32
6
|
|
33
7
|
describe 'rescue_me' do
|
34
8
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
9
|
+
let(:logger) { TestLogger.new }
|
10
|
+
let(:test_class) { double "TestClass" }
|
38
11
|
|
39
12
|
it "runs an exception-free block of code once" do
|
40
|
-
|
41
|
-
|
42
|
-
}
|
43
|
-
@exception_counter.method_called_count.should == 1
|
13
|
+
test_class.should_receive(:sendmail).once
|
14
|
+
rescue_and_retry { test_class.sendmail }
|
44
15
|
end
|
45
16
|
|
46
17
|
it "attempts to run a block that raises an unexpected exception only once" do
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
|
52
|
-
@exception_counter.method_called_count.should == 1
|
18
|
+
test_class.stub(:sendmail).and_raise(NameError)
|
19
|
+
test_class.should_receive(:sendmail).once
|
20
|
+
expect {
|
21
|
+
rescue_and_retry(4, IOError) { test_class.sendmail }
|
22
|
+
}.to raise_error
|
53
23
|
end
|
54
24
|
|
55
25
|
it "re-runs the block of code for exactly max_attempt number of times" do
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
end
|
62
|
-
@exception_counter.method_called_count.should == 3
|
26
|
+
test_class.stub(:sendmail).and_raise(IOError)
|
27
|
+
test_class.should_receive(:sendmail).exactly(2).times
|
28
|
+
expect {
|
29
|
+
rescue_and_retry(2, IOError) { test_class.sendmail }
|
30
|
+
}.to raise_error
|
63
31
|
end
|
64
32
|
|
65
33
|
it "re-runs the block of code for exactly a single time when specified" do
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
rescue
|
72
|
-
end
|
73
|
-
@exception_counter.method_called_count.should == 1
|
34
|
+
test_class.stub(:sendmail).and_raise(IOError)
|
35
|
+
test_class.should_receive(:sendmail).once
|
36
|
+
expect {
|
37
|
+
rescue_and_retry(1, IOError) { test_class.sendmail }
|
38
|
+
}.to raise_error
|
74
39
|
end
|
75
40
|
|
76
41
|
it "does not re-run the block of code after it has run successfully" do
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
42
|
+
test_class.stub(:sendmail) {
|
43
|
+
@x ||= 0; @x += 1; raise IOError if @x < 3
|
44
|
+
}
|
45
|
+
test_class.should_receive(:sendmail).exactly(3).times
|
46
|
+
expect {
|
47
|
+
rescue_and_retry(5, IOError) { test_class.sendmail }
|
48
|
+
}.to_not raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it "names the enclosing method from which an exception arises" do
|
52
|
+
test_class.stub(:sendmail).and_raise(IOError)
|
53
|
+
test_class.stub(:logger).and_return(logger)
|
54
|
+
logger.should_receive(:warn).with(/rescue_me_spec/)
|
55
|
+
expect {
|
56
|
+
rescue_and_retry(1, IOError) { test_class.sendmail }
|
57
|
+
}.to raise_error
|
83
58
|
end
|
84
59
|
|
85
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rescue_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -101,7 +101,6 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
- .document
|
105
104
|
- .gitignore
|
106
105
|
- .ruby-version
|
107
106
|
- Gemfile
|