Skip to content

Commit c5b78f8

Browse files
committed
ruby: Initialize test runs before checking retry expiry
1 parent 0370167 commit c5b78f8

4 files changed

Lines changed: 123 additions & 14 deletions

File tree

‎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: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ def test_retry_success
221221
end
222222

223223
def test_worker_history_retry_replays_all_reserved_tests
224-
run_worker_history_worker(retry_count: 0)
224+
run_retry_worker(retry_count: 0)
225225
assert_predicate $?, :success?
226226

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

229229
assert_predicate $?, :success?
230230
assert_includes out, 'Replaying 100 tests from worker history.'
@@ -233,22 +233,111 @@ def test_worker_history_retry_replays_all_reserved_tests
233233
end
234234

235235
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')
236+
run_retry_worker(retry_count: 0, build_id: 'missing-history', worker_id: '1')
237237

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

240240
assert_predicate $?, :success?
241241
assert_includes out, "The retry queue does not contain any failure, we'll process the main queue instead."
242242
assert_includes out, 'All tests were ran already'
243243
end
244244

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

828917
private
829918

830-
def run_worker_history_worker(
919+
def run_retry_worker(
831920
retry_count:,
832921
build_id: 'worker-history',
833922
worker_id: '1',
834923
test_file: 'test/passing_test.rb',
835-
extra_args: []
924+
extra_args: [],
925+
retry_selection: 'worker-history',
926+
environment: {},
927+
queue_url: @redis_url
836928
)
837929
args = [
838930
@exe, 'run',
839-
'--queue', @redis_url,
931+
'--queue', queue_url,
840932
'--seed', 'foobar',
841933
'--build', build_id,
842934
'--worker', worker_id,
843935
'--timeout', '1',
844-
'--retry-selection', 'worker-history'
936+
'--retry-selection', retry_selection
845937
]
846938
args.concat(extra_args)
847939
args.push('-Itest', test_file)
848940

849941
capture_subprocess_io do
850942
system(
851-
{ 'BUILDKITE_RETRY_COUNT' => retry_count.to_s },
943+
{ 'BUILDKITE_RETRY_COUNT' => retry_count.to_s }.merge(environment),
852944
*args,
853945
chdir: 'test/fixtures/'
854946
)

0 commit comments

Comments
 (0)