Skip to content

Commit feb3e30

Browse files
authored
Merge pull request #102 from figma/ckwarteng/initialize-ci-queue-before-retry
Minitest: Initialize test runs before checking retry expiry
2 parents 0370167 + d569aa5 commit feb3e30

5 files changed

Lines changed: 126 additions & 14 deletions

File tree

‎ruby/Gemfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ source 'https://rubygems.org'
44
gemspec
55

66
gem 'activesupport', '~> 5.2.0'
7+
# ActiveSupport 5.2's JSON encoder passes quirks_mode, removed in JSON 3.
8+
gem 'json', '< 3'
79
gem 'rexml'

‎ruby/lib/ci/queue/static.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def created_at=(timestamp)
5555
end
5656

5757
def expired?
58-
(@created_at.to_f TEN_MINUTES) < CI::Queue.time_now.to_f
58+
(@created_at.to_f + TEN_MINUTES) < CI::Queue.time_now.to_f
5959
end
6060

6161
def populated?

‎ruby/lib/minitest/queue/runner.rb‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def retry_command
4949

5050
def run_command
5151
require_worker_id!
52+
# A CI job retry can be the first attempt to reach the test runner.
53+
# Redis uses SETNX here, so an existing run keeps its original age.
54+
queue.rescue_connection_errors { queue.created_at = CI::Queue.time_now.to_f }
55+
5256
if queue.retrying? || retry?
5357
if queue.expired?
5458
abort! "The test run is too old and can't be retried"
@@ -67,8 +71,6 @@ def run_command
6771
end
6872
end
6973

70-
queue.rescue_connection_errors { queue.created_at = CI::Queue.time_now.to_f }
71-
7274
set_load_path
7375
Minitest.queue = queue
7476
reporters = [

‎ruby/test/ci/queue/static_test.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ def test_shutdown_stops_polling
1616
refute_predicate @queue, :exhausted?
1717
end
1818

19+
def test_expiry_uses_original_start_time
20+
@queue.created_at = 1000
21+
@queue.created_at = 2000
22+
23+
CI::Queue.stub(:time_now, Time.at(1000)) do
24+
refute_predicate @queue, :expired?
25+
end
26+
CI::Queue.stub(:time_now, Time.at(1600)) do
27+
refute_predicate @queue, :expired?
28+
end
29+
CI::Queue.stub(:time_now, Time.at(1601)) do
30+
assert_predicate @queue, :expired?
31+
end
32+
end
33+
1934
private
2035

2136
def test_from_uri

‎ruby/test/integration/minitest_redis_test.rb‎

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require 'test_helper'
3+
require 'tempfile'
34
require 'tmpdir'
45
require 'active_support/testing/time_helpers'
56
require 'concurrent/set'
@@ -221,10 +222,10 @@ def test_retry_success
221222
end
222223

223224
def test_worker_history_retry_replays_all_reserved_tests
224-
run_worker_history_worker(retry_count: 0)
225+
run_retry_worker(retry_count: 0)
225226
assert_predicate $?, :success?
226227

227-
out, = run_worker_history_worker(retry_count: 1)
228+
out, = run_retry_worker(retry_count: 1)
228229

229230
assert_predicate $?, :success?
230231
assert_includes out, 'Replaying 100 tests from worker history.'
@@ -233,22 +234,111 @@ def test_worker_history_retry_replays_all_reserved_tests
233234
end
234235

235236
def test_worker_history_retry_rejoins_queue_without_worker_reservations
236-
run_worker_history_worker(retry_count: 0, build_id: 'missing-history', worker_id: '1')
237+
run_retry_worker(retry_count: 0, build_id: 'missing-history', worker_id: '1')
237238

238-
out, = run_worker_history_worker(retry_count: 1, build_id: 'missing-history', worker_id: '2')
239+
out, = run_retry_worker(retry_count: 1, build_id: 'missing-history', worker_id: '2')
239240

240241
assert_predicate $?, :success?
241242
assert_includes out, "The retry queue does not contain any failure, we'll process the main queue instead."
242243
assert_includes out, 'All tests were ran already'
243244
end
244245

246+
def test_buildkite_retry_starts_a_test_run_after_setup_failed
247+
%w[failed-tests worker-history].each do |selection|
248+
[1, 2].each do |retry_count|
249+
build_id = "setup-failure-#{selection}-#{retry_count}"
250+
out, err = run_retry_worker(
251+
retry_count: retry_count,
252+
build_id: build_id,
253+
retry_selection: selection,
254+
extra_args: ['--namespace', 'sinatra-tests']
255+
)
256+
257+
assert_predicate $?, :success?, out + err
258+
assert_includes normalize(out), 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
259+
assert_equal 100, @redis.scard("build:sinatra-tests:#{build_id}:processed")
260+
end
261+
end
262+
end
263+
264+
def test_semaphore_retry_starts_a_test_run_after_setup_failed
265+
out, err = run_retry_worker(
266+
retry_count: 0,
267+
build_id: 'semaphore-setup-failure',
268+
retry_selection: 'failed-tests',
269+
environment: { 'SEMAPHORE_PIPELINE_RERUN' => 'true' }
270+
)
271+
272+
assert_predicate $?, :success?, out + err
273+
assert_includes normalize(out), 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
274+
end
275+
276+
def test_buildkite_retry_replays_file_queue
277+
Tempfile.create(['ci-queue-retry', '.txt']) do |file|
278+
file.write((0...100).map { |index| "PassingTest#test_passing_#{index}\n" }.join)
279+
file.flush
280+
281+
out, err = run_retry_worker(
282+
retry_count: 1,
283+
retry_selection: 'failed-tests',
284+
queue_url: file.path
285+
)
286+
287+
assert_predicate $?, :success?, out + err
288+
assert_includes normalize(out), 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
289+
end
290+
end
291+
292+
def test_retry_preserves_existing_test_run_timestamp
293+
%w[failed-tests worker-history].each do |selection|
294+
build_id = "existing-run-#{selection}"
295+
run_retry_worker(retry_count: 0, build_id: build_id)
296+
assert_predicate $?, :success?
297+
created_at = (Time.now.to_f - 3600).to_s
298+
@redis.set("build:#{build_id}:created-at", created_at)
299+
300+
out, err = run_retry_worker(
301+
retry_count: 1,
302+
build_id: build_id,
303+
retry_selection: selection
304+
)
305+
306+
assert_predicate $?, :success?, out + err
307+
assert_equal created_at, @redis.get("build:#{build_id}:created-at")
308+
if selection == 'worker-history'
309+
assert_includes out, 'Replaying 100 tests from worker history.'
310+
else
311+
assert_includes out, 'All tests were ran already'
312+
end
313+
end
314+
end
315+
316+
def test_retry_rejects_expired_run_without_worker_history
317+
%w[failed-tests worker-history].each do |selection|
318+
build_id = "expired-run-#{selection}"
319+
created_at = (Time.now.to_f - 24 * 60 * 60).to_s
320+
@redis.set("build:#{build_id}:created-at", created_at)
321+
322+
out, err = run_retry_worker(
323+
retry_count: 1,
324+
build_id: build_id,
325+
retry_selection: selection
326+
)
327+
328+
refute_predicate $?, :success?, out + err
329+
assert_includes out, "The test run is too old and can't be retried"
330+
assert_equal created_at, @redis.get("build:#{build_id}:created-at")
331+
assert_equal 0, @redis.scard("build:#{build_id}:processed")
332+
end
333+
end
334+
245335
def test_worker_history_retry_fails_when_replay_stops_early
246-
run_worker_history_worker(
336+
run_retry_worker(
247337
retry_count: 0,
248338
build_id: 'incomplete-history',
249339
test_file: 'test/failing_test.rb'
250340
)
251-
out, err = run_worker_history_worker(
341+
out, err = run_retry_worker(
252342
retry_count: 1,
253343
build_id: 'incomplete-history',
254344
test_file: 'test/failing_test.rb',
@@ -827,28 +917,31 @@ def test_utf8_tests_and_marshal
827917

828918
private
829919

830-
def run_worker_history_worker(
920+
def run_retry_worker(
831921
retry_count:,
832922
build_id: 'worker-history',
833923
worker_id: '1',
834924
test_file: 'test/passing_test.rb',
835-
extra_args: []
925+
extra_args: [],
926+
retry_selection: 'worker-history',
927+
environment: {},
928+
queue_url: @redis_url
836929
)
837930
args = [
838931
@exe, 'run',
839-
'--queue', @redis_url,
932+
'--queue', queue_url,
840933
'--seed', 'foobar',
841934
'--build', build_id,
842935
'--worker', worker_id,
843936
'--timeout', '1',
844-
'--retry-selection', 'worker-history'
937+
'--retry-selection', retry_selection
845938
]
846939
args.concat(extra_args)
847940
args.push('-Itest', test_file)
848941

849942
capture_subprocess_io do
850943
system(
851-
{ 'BUILDKITE_RETRY_COUNT' => retry_count.to_s },
944+
{ 'BUILDKITE_RETRY_COUNT' => retry_count.to_s }.merge(environment),
852945
*args,
853946
chdir: 'test/fixtures/'
854947
)

0 commit comments

Comments
 (0)