@@ -9,54 +9,94 @@ module Concurrent
99 context 'happy execution' do
1010
1111 let ( :task ) { Proc . new { 42 } }
12- let ( :executor ) { SafeTaskExecutor . new ( task ) }
12+ subject { SafeTaskExecutor . new ( task ) }
1313
1414 it 'should return success' do
15- success , value , reason = executor . execute
15+ success , value , reason = subject . execute
1616 success . should be_true
1717 end
1818
1919 it 'should return task value' do
20- success , value , reason = executor . execute
20+ success , value , reason = subject . execute
2121 value . should eq 42
2222 end
2323
2424 it 'should return a nil reason' do
25- success , value , reason = executor . execute
25+ success , value , reason = subject . execute
2626 reason . should be_nil
2727 end
2828
29- it 'passes all arguments to #execute to the task'
29+ it 'passes all arguments to #execute to the task' do
30+ expected = nil
31+ task = proc { |*args | expected = args }
32+ SafeTaskExecutor . new ( task ) . execute ( 1 , 2 , 3 )
33+ expected . should eq [ 1 , 2 , 3 ]
34+ end
3035
31- it 'protectes #execute with a mutex'
36+ it 'protectes #execute with a mutex' do
37+ mutex = double ( :mutex )
38+ Mutex . should_receive ( :new ) . with ( no_args ) . and_return ( mutex )
39+ mutex . should_receive ( :synchronize ) . with ( no_args )
40+ subject . execute
41+ end
3242 end
3343
3444 context 'failing execution' do
3545
3646 let ( :task ) { Proc . new { raise StandardError . new ( 'an error' ) } }
37- let ( :executor ) { SafeTaskExecutor . new ( task ) }
47+ subject { SafeTaskExecutor . new ( task ) }
3848
3949 it 'should return false success' do
40- success , value , reason = executor . execute
50+ success , value , reason = subject . execute
4151 success . should be_false
4252 end
4353
4454 it 'should return a nil value' do
45- success , value , reason = executor . execute
55+ success , value , reason = subject . execute
4656 value . should be_nil
4757 end
4858
4959 it 'should return the reason' do
50- success , value , reason = executor . execute
60+ success , value , reason = subject . execute
5161 reason . should be_a ( StandardError )
5262 reason . message . should eq 'an error'
5363 end
5464
55- it 'rescues Exception when :rescue_exception is true'
65+ it 'rescues Exception when :rescue_exception is true' do
66+ task = proc { raise Exception }
67+ subject = SafeTaskExecutor . new ( task , rescue_exception : true )
68+ expect {
69+ subject . execute
70+ } . to_not raise_error
71+ end
5672
57- it 'rescues StandardError when :rescue_exception is false'
73+ it 'rescues StandardError when :rescue_exception is false' do
74+ task = proc { raise StandardError }
75+ subject = SafeTaskExecutor . new ( task , rescue_exception : false )
76+ expect {
77+ subject . execute
78+ } . to_not raise_error
79+
80+ task = proc { raise Exception }
81+ subject = SafeTaskExecutor . new ( task , rescue_exception : false )
82+ expect {
83+ subject . execute
84+ } . to raise_error ( Exception )
85+ end
5886
59- it 'rescues StandardError by default'
87+ it 'rescues StandardError by default' do
88+ task = proc { raise StandardError }
89+ subject = SafeTaskExecutor . new ( task )
90+ expect {
91+ subject . execute
92+ } . to_not raise_error
93+
94+ task = proc { raise Exception }
95+ subject = SafeTaskExecutor . new ( task )
96+ expect {
97+ subject . execute
98+ } . to raise_error ( Exception )
99+ end
60100 end
61101 end
62102 end
0 commit comments