Skip to content

Commit 0497006

Browse files
committed
Add test for max retries and refine the implementation
1 parent 6a2f3b0 commit 0497006

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ def download(glotpress_url)
2929
UI.message("Received 301 for `#{response.uri}`. Following redirect...")
3030
download(response.header['location'])
3131
when '429' # We got rate-limited, auto_retry or offer to try again with a prompt
32-
if @auto_retry && @auto_retry_attempt_counter <= MAX_AUTO_RETRY_ATTEMPTS
33-
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
34-
sleep(@auto_retry_sleep_time)
35-
@auto_retry_attempt_counter += 1
36-
download(response.uri)
32+
if @auto_retry
33+
if @auto_retry_attempt_counter < @auto_retry_max_attempts
34+
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
35+
sleep(@auto_retry_sleep_time)
36+
@auto_retry_attempt_counter += 1
37+
download(response.uri)
38+
else
39+
UI.error("Abandoning `#{response.uri}` download after #{@auto_retry_attempt_counter} retries.")
40+
end
3741
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
3842
download(response.uri)
3943
else
@@ -51,9 +55,6 @@ def download(glotpress_url)
5155
module Fastlane
5256
module Helper
5357
class MetadataDownloader
54-
AUTO_RETRY_SLEEP_TIME = 20
55-
MAX_AUTO_RETRY_ATTEMPTS = 30
56-
5758
attr_reader :target_folder, :target_files
5859

5960
def initialize(target_folder, target_files, auto_retry)
@@ -159,22 +160,6 @@ def handle_glotpress_download(response:, locale:, is_source:)
159160
loc_data = JSON.parse(response.body) rescue loc_data = nil
160161
parse_data(locale, loc_data, is_source)
161162
reparse_alternates(target_locale, loc_data, is_source) unless @alternates.empty?
162-
when '301'
163-
# Follow the redirect
164-
UI.message("Received 301 for `#{locale}`. Following redirect...")
165-
download(locale, response.header['location'], is_source)
166-
when '429'
167-
# We got rate-limited, auto_retry or offer to try again with a prompt
168-
if @auto_retry && @auto_retry_attempt_counter <= MAX_AUTO_RETRY_ATTEMPTS
169-
UI.message("Received 429 for `#{locale}`. Auto retrying in #{AUTO_RETRY_SLEEP_TIME} seconds...")
170-
sleep(AUTO_RETRY_SLEEP_TIME)
171-
@auto_retry_attempt_counter += 1
172-
download(locale, response.uri, is_source)
173-
elsif UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?")
174-
download(locale, response.uri, is_source)
175-
else
176-
UI.error("Abandoning `#{locale}` download as requested.")
177-
end
178163
else
179164
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
180165
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")

spec/glotpress_downloader_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
describe Fastlane::Helper::GlotpressDownloader do
4+
describe 'downloading' do
5+
context 'when auto retry is enabled' do
6+
context 'when GlotPress returs a 429 code' do
7+
it 'retries automatically' do
8+
sleep_time = 0.1
9+
downloader = described_class.new(auto_retry: true, auto_retry_sleep_time: sleep_time)
10+
fake_url = 'https://test.com'
11+
12+
count = 0
13+
stub_request(:get, fake_url).to_return do
14+
count += 1
15+
if count == 1
16+
{ status: 429, body: 'Too Many Requests' }
17+
else
18+
{ status: 200, body: 'OK' }
19+
end
20+
end
21+
22+
expect(Fastlane::UI).to receive(:message)
23+
.with(/Received 429 for `#{fake_url}`. Auto retrying in #{sleep_time} seconds.../)
24+
25+
response = downloader.download(fake_url)
26+
27+
expect(count).to eq(2)
28+
expect(response.code).to eq('200')
29+
end
30+
31+
context 'when the maximum number of retries is reached' do
32+
it 'aborts' do
33+
sleep_time = 0.1
34+
max_retries = 3
35+
downloader = described_class.new(auto_retry: true, auto_retry_sleep_time: sleep_time, auto_retry_max_attempts: max_retries)
36+
fake_url = 'https://test.com'
37+
38+
count = 0
39+
stub_request(:get, fake_url).to_return do
40+
count += 1
41+
{ status: 429, body: 'Too Many Requests' }
42+
end
43+
44+
expect(Fastlane::UI).to receive(:message)
45+
.with(/Received 429 for `#{fake_url}`. Auto retrying in #{sleep_time} seconds.../)
46+
.exactly(max_retries).times
47+
48+
expect(Fastlane::UI).to receive(:error)
49+
.with(/Abandoning `#{fake_url}` download after #{max_retries} retries./)
50+
51+
downloader.download(fake_url)
52+
53+
expect(count).to eq(max_retries + 1) # the original request plus the retries
54+
end
55+
end
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)