diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-19 14:01:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-19 14:01:45 +0300 |
commit | 9297025d0b7ddf095eb618dfaaab2ff8f2018d8b (patch) | |
tree | 865198c01d1824a9b098127baa3ab980c9cd2c06 /spec/lib/bitbucket/exponential_backoff_spec.rb | |
parent | 6372471f43ee03c05a7c1f8b0c6ac6b8a7431dbe (diff) |
Add latest changes from gitlab-org/gitlab@16-7-stable-eev16.7.0-rc42
Diffstat (limited to 'spec/lib/bitbucket/exponential_backoff_spec.rb')
-rw-r--r-- | spec/lib/bitbucket/exponential_backoff_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/lib/bitbucket/exponential_backoff_spec.rb b/spec/lib/bitbucket/exponential_backoff_spec.rb new file mode 100644 index 00000000000..b52a83731f4 --- /dev/null +++ b/spec/lib/bitbucket/exponential_backoff_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Bitbucket::ExponentialBackoff, feature_category: :importers do + let(:service) { dummy_class.new } + let(:body) { 'test' } + let(:parsed_response) { instance_double(Net::HTTPResponse, body: body.to_json) } + let(:response) { double(Faraday::Response, body: body, parsed: parsed_response) } + let(:response_caller) { -> { response } } + + let(:dummy_class) do + Class.new do + def logger + @logger ||= Logger.new(File::NULL) + end + + def dummy_method(response_caller) + retry_with_exponential_backoff do + response_caller.call + end + end + + include Bitbucket::ExponentialBackoff + end + end + + subject(:execute) { service.dummy_method(response_caller) } + + describe '.retry_with_exponential_backoff' do + let(:max_retries) { described_class::MAX_RETRIES } + + context 'when the function succeeds on the first try' do + it 'calls the function once and returns its result' do + expect(response_caller).to receive(:call).once.and_call_original + + expect(Gitlab::Json.parse(execute.parsed.body)).to eq(body) + end + end + + context 'when the function response is an error' do + let(:error) { 'Rate limit for this resource has been exceeded' } + + before do + stub_const("#{described_class.name}::INITIAL_DELAY", 0.0) + allow(Random).to receive(:rand).and_return(0.001) + end + + it 'raises a RateLimitError if the maximum number of retries is exceeded' do + allow(response_caller).to receive(:call).and_raise(OAuth2::Error, error) + + message = "Maximum number of retries (#{max_retries}) exceeded. #{error}" + + expect do + execute + end.to raise_error(described_class::RateLimitError, message) + + expect(response_caller).to have_received(:call).exactly(max_retries).times + end + end + end +end |