Skip to content

Commit 29ad54d

Browse files
authored
Merge pull request #337 from Shopify/shioyama/move_run_from_queue_to_queue_run
Move `Minitest.__run` patch internals to `Minitest::Queue.run`
2 parents a9277df + 5a3a595 commit 29ad54d

1 file changed

Lines changed: 74 additions & 55 deletions

File tree

ruby/lib/minitest/queue.rb

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module WithTimestamps
107107
end
108108

109109
module Queue
110-
include ::CI::Queue::OutputHelpers
110+
extend ::CI::Queue::OutputHelpers
111111
attr_writer :run_command_formatter, :project_root
112112

113113
def run_command_formatter
@@ -149,8 +149,79 @@ def self.relative_path(path, root: project_root)
149149
path
150150
end
151151

152+
class << self
153+
def queue
154+
Minitest.queue
155+
end
156+
157+
def run(reporter, *)
158+
rescue_run_errors do
159+
queue.poll do |example|
160+
result = queue.with_heartbeat(example.id) do
161+
example.run
162+
end
163+
164+
handle_test_result(reporter, example, result)
165+
end
166+
167+
queue.stop_heartbeat!
168+
end
169+
end
170+
171+
def handle_test_result(reporter, example, result)
172+
failed = !(result.passed? || result.skipped?)
173+
174+
if example.flaky?
175+
result.mark_as_flaked!
176+
failed = false
177+
end
178+
179+
if failed && queue.config.failing_test && queue.config.failing_test != example.id
180+
# When we do a bisect, we don't care about the result other than the test we're running the bisect on
181+
result.mark_as_flaked!
182+
failed = false
183+
elsif failed
184+
queue.report_failure!
185+
else
186+
queue.report_success!
187+
end
188+
189+
if failed && CI::Queue.requeueable?(result) && queue.requeue(example)
190+
result.requeue!
191+
end
192+
reporter.record(result)
193+
end
194+
195+
private
196+
197+
def rescue_run_errors(&block)
198+
block.call
199+
rescue Errno::EPIPE
200+
# This happens when the heartbeat process dies
201+
reopen_previous_step
202+
puts red("The heartbeat process died. This worker is exiting early.")
203+
exit!(41)
204+
rescue CI::Queue::Error => error
205+
reopen_previous_step
206+
puts red("#{error.class}: #{error.message}")
207+
error.backtrace.each do |frame|
208+
puts red(frame)
209+
end
210+
exit!(41)
211+
rescue => error
212+
reopen_previous_step
213+
Minitest.queue.report_worker_error(error)
214+
puts red("This worker exited because of an uncaught application error:")
215+
puts red("#{error.class}: #{error.message}")
216+
error.backtrace.each do |frame|
217+
puts red(frame)
218+
end
219+
exit!(42)
220+
end
221+
end
222+
152223
class SingleExample
153-
attr_reader :method_name
224+
attr_reader :runnable, :method_name
154225

155226
def initialize(runnable, method_name)
156227
@runnable = runnable
@@ -212,7 +283,7 @@ def loaded_tests
212283

213284
def __run(*args)
214285
if queue
215-
run_from_queue(*args)
286+
Queue.run(*args)
216287

217288
if queue.config.circuit_breakers.any?(&:open?)
218289
STDERR.puts queue.config.circuit_breakers.map(&:message).join(' ').strip
@@ -225,58 +296,6 @@ def __run(*args)
225296
super
226297
end
227298
end
228-
229-
def run_from_queue(reporter, *)
230-
queue.poll do |example|
231-
result = queue.with_heartbeat(example.id) do
232-
example.run
233-
end
234-
235-
failed = !(result.passed? || result.skipped?)
236-
237-
if example.flaky?
238-
result.mark_as_flaked!
239-
failed = false
240-
end
241-
242-
if failed && queue.config.failing_test && queue.config.failing_test != example.id
243-
# When we do a bisect, we don't care about the result other than the test we're running the bisect on
244-
result.mark_as_flaked!
245-
failed = false
246-
elsif failed
247-
queue.report_failure!
248-
else
249-
queue.report_success!
250-
end
251-
252-
if failed && CI::Queue.requeueable?(result) && queue.requeue(example)
253-
result.requeue!
254-
end
255-
reporter.record(result)
256-
end
257-
queue.stop_heartbeat!
258-
rescue Errno::EPIPE
259-
# This happens when the heartbeat process dies
260-
reopen_previous_step
261-
puts red("The heartbeat process died. This worker is exiting early.")
262-
exit!(41)
263-
rescue CI::Queue::Error => error
264-
reopen_previous_step
265-
puts red("#{error.class}: #{error.message}")
266-
error.backtrace.each do |frame|
267-
puts red(frame)
268-
end
269-
exit!(41)
270-
rescue => error
271-
reopen_previous_step
272-
queue.report_worker_error(error)
273-
puts red("This worker exited because of an uncaught application error:")
274-
puts red("#{error.class}: #{error.message}")
275-
error.backtrace.each do |frame|
276-
puts red(frame)
277-
end
278-
exit!(42)
279-
end
280299
end
281300
end
282301

0 commit comments

Comments
 (0)