diff options
| author | Robert | 2017-07-31 08:03:13 +0200 |
|---|---|---|
| committer | Robert | 2017-07-31 08:03:13 +0200 |
| commit | 609b774388a7f57703ec14a224363c88f3564eaf (patch) | |
| tree | 4610e6b5a82a6f472d2b6ecc7901385b7415f296 /spec/services | |
| parent | aa5028a21f28a2bee9f64b5e87e70828c9c8b75f (diff) | |
| download | chouette-core-609b774388a7f57703ec14a224363c88f3564eaf.tar.bz2 | |
Refs: #3507_1726@6h Code Review
Diffstat (limited to 'spec/services')
| -rw-r--r-- | spec/services/retry_service_spec.rb | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/spec/services/retry_service_spec.rb b/spec/services/retry_service_spec.rb index 22957b565..ce150f808 100644 --- a/spec/services/retry_service_spec.rb +++ b/spec/services/retry_service_spec.rb @@ -6,11 +6,11 @@ RSpec.describe RetryService do expect( subject ).not_to receive(:sleep) end - it 'returns a tuple :ok and the result' do - expect( subject.execute { 42 } ).to eq([:ok, 42]) + it 'returns an ok result' do + expect( subject.execute { 42 } ).to eq(Result.ok(42)) end it 'does not fail on nil' do - expect( subject.execute { nil } ).to eq([:ok, nil]) + expect( subject.execute { nil } ).to eq(Result.ok(nil)) end it 'fails wihout retries if raising un unregistered exception' do @@ -26,13 +26,13 @@ RSpec.describe RetryService do end it 'fails after raising a registered exception n times' do result = subject.execute{ raise ArgumentError } - expect( result.first ).to eq(:error) - expect( result.last ).to be_kind_of(ArgumentError) + expect( result.status ).to eq(:error) + expect( result.value ).to be_kind_of(ArgumentError) end it 'fails with an explicit try again (automatically registered exception)' do result = subject.execute{ raise RetryService::Retry } - expect( result.first ).to eq(:error) - expect( result.last ).to be_kind_of(RetryService::Retry) + expect( result.status ).to eq(:error) + expect( result.value ).to be_kind_of(RetryService::Retry) end end @@ -43,11 +43,11 @@ RSpec.describe RetryService do end it 'succeds the second time' do - expect( subject.execute{ succeed_later(ArgumentError){ 42 } } ).to eq([:ok, 42]) + expect( subject.execute{ succeed_later(ArgumentError){ 42 } } ).to eq(Result.ok(42)) end it 'succeeds the second time with try again (automatically registered exception)' do - expect( subject.execute{ succeed_later(RetryService::Retry){ 42 } } ).to eq([:ok, 42]) + expect( subject.execute{ succeed_later(RetryService::Retry){ 42 } } ).to eq(Result.ok(42)) end end @@ -58,17 +58,25 @@ RSpec.describe RetryService do expect( subject ).to receive(:sleep).with(3) end it 'succeeds the third time with try again (automatically registered exception)' do - expect( subject.execute{ succeed_later(RetryService::Retry, count: 2){ 42 } } ).to eq([:ok, 42]) + expect( subject.execute{ succeed_later(RetryService::Retry, count: 2){ 42 } } ).to eq(Result.ok(42)) end end context 'failure callback once' do + subject do + described_class.new delays: [2, 3], rescue_from: [NameError, ArgumentError] do |reason, count| + @reason=reason + @callback_count=count + @failures += 1 + end + end + before do @failures = 0 @count = 0 expect( subject ).to receive(:sleep).with(2) - subject.register_failure_callback { |reason, count| @reason=reason; @callback_count=count; @failures += 1 } end + it 'succeeds the second time and calls the failure_callback once' do subject.execute{ succeed_later(RetryService::Retry){ 42 } } expect( @failures ).to eq(1) @@ -81,13 +89,19 @@ RSpec.describe RetryService do end context 'failure callback twice' do + subject do + described_class.new delays: [2, 3], rescue_from: [NameError, ArgumentError] do |_reason, _count| + @failures += 1 + end + end + before do @failures = 0 @count = 0 expect( subject ).to receive(:sleep).with(2) expect( subject ).to receive(:sleep).with(3) - subject.register_failure_callback { @failures += 1 } end + it 'succeeds the third time and calls the failure_callback twice' do subject.execute{ succeed_later(NameError, count: 2){ 42 } } expect( @failures ).to eq(2) |
