-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ioki | ||
module Retry | ||
class MaximumReached < ::StandardError; end | ||
|
||
def self.n_times(max_retries, sleep_seconds = 1) | ||
retries = max_retries | ||
|
||
begin | ||
yield | ||
rescue Ioki::Error::OauthRefreshToken => e | ||
raise e | ||
rescue StandardError => e | ||
retries -= 1 | ||
|
||
raise MaximumReached, "Gave up after #{max_retries} retries: #{e.message}" if retries <= 0 | ||
|
||
sleep(sleep_seconds) | ||
retry | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'ioki/apis/endpoints/endpoints' | ||
require 'ioki/retry' | ||
require 'webmock' | ||
|
||
class DummyApi | ||
ENDPOINTS = | ||
[ | ||
Ioki::Endpoints::Index.new( | ||
:ping, | ||
base_path: ['driver'], | ||
model_class: Ioki::Model::Base | ||
) | ||
].freeze | ||
end | ||
|
||
RSpec.describe Ioki::Retry do | ||
include WebMock::API | ||
|
||
it 'executes the given block' do | ||
called = false | ||
|
||
described_class.n_times(3) do | ||
called = true | ||
end | ||
|
||
expect(called).to be true | ||
end | ||
|
||
it 'retries when an exception is raised' do | ||
first_run = true | ||
called_count = 0 | ||
|
||
described_class.n_times(3) do | ||
called_count += 1 | ||
|
||
if first_run | ||
first_run = false | ||
raise Ioki::Error::Unauthorized, nil | ||
end | ||
end | ||
|
||
expect(called_count).to eq 2 | ||
end | ||
|
||
it 'retries only to the maximum' do | ||
called_count = 0 | ||
|
||
expect do | ||
described_class.n_times(3) do | ||
called_count += 1 | ||
|
||
raise Ioki::Error::Unauthorized, nil | ||
end | ||
end.to raise_error(Ioki::Retry::MaximumReached) | ||
|
||
expect(called_count).to eq 3 | ||
end | ||
|
||
context 'with a Ioki client' do | ||
let(:client) { Ioki::Client.new(Ioki::Configuration.new, DummyApi) } | ||
|
||
before do | ||
WebMock.enable! | ||
VCR.turn_off! | ||
end | ||
|
||
after do | ||
WebMock.disable! | ||
VCR.turn_on! | ||
end | ||
|
||
it 'retries failed requests' do | ||
ping_request = stub_request(:get, 'https://app.io.ki/api/driver/ping') | ||
.to_return( | ||
{ status: 500, body: '{"data": []}', headers: { content_type: 'application/json' } }, | ||
{ status: 200, body: '{"data": []}', headers: { content_type: 'application/json' } } | ||
) | ||
|
||
client.ping | ||
|
||
expect(ping_request).to have_been_requested.twice | ||
end | ||
|
||
it 'raises after max retries' do | ||
ping_request = stub_request(:get, 'https://app.io.ki/api/driver/ping') | ||
.to_return(status: 500, body: '{"data": []}', headers: { content_type: 'application/json' }) | ||
|
||
expect do | ||
client.ping | ||
end.to raise_error Ioki::Retry::MaximumReached | ||
|
||
expect(ping_request).to have_been_requested.times(3) | ||
end | ||
end | ||
end |