-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathqueue.rb
More file actions
329 lines (273 loc) · 7.72 KB
/
queue.rb
File metadata and controls
329 lines (273 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# frozen_string_literal: true
require 'shellwords'
require 'minitest'
require 'minitest/reporters'
require 'minitest/queue/failure_formatter'
require 'minitest/queue/error_report'
require 'minitest/queue/local_requeue_reporter'
require 'minitest/queue/build_status_recorder'
require 'minitest/queue/build_status_reporter'
require 'minitest/queue/order_reporter'
require 'minitest/queue/junit_reporter'
require 'minitest/queue/test_data_reporter'
require 'minitest/queue/grind_recorder'
require 'minitest/queue/grind_reporter'
require 'minitest/queue/test_time_recorder'
require 'minitest/queue/test_time_reporter'
module Minitest
class Requeue < Skip
attr_reader :failure
def initialize(failure)
super()
@failure = failure
end
def result_label
"Requeued"
end
def backtrace
failure.backtrace
end
def error
failure.error
end
def message
failure.message
end
end
class Flaked < Skip
attr_reader :failure
def initialize(failure)
super()
@failure = failure
end
def result_label
"Flaked"
end
def backtrace
failure.backtrace
end
def error
failure.error
end
def message
failure.message
end
end
module Requeueing
# Make requeues acts as skips for reporters not aware of the difference.
def skipped?
super || requeued?
end
def requeued?
Requeue === failure
end
def requeue!
self.failures.unshift(Requeue.new(self.failures.shift))
end
end
module Flakiness
# Make failed flaky tests acts as skips for reporters not aware of the difference.
def skipped?
super || flaked?
end
def flaked?
@flaky ||= false
!!((Flaked === failure) || @flaky)
end
def mark_as_flaked!
if passed?
@flaky = true
else
self.failures.unshift(Flaked.new(self.failures.shift))
end
end
end
module WithTimestamps
attr_accessor :start_timestamp, :finish_timestamp
end
module Queue
extend ::CI::Queue::OutputHelpers
attr_writer :run_command_formatter, :project_root
def run_command_formatter
@run_command_formatter ||= if defined?(Rails) && defined?(Rails::TestUnitRailtie)
RAILS_RUN_COMMAND_FORMATTER
else
DEFAULT_RUN_COMMAND_FORMATTER
end
end
DEFAULT_RUN_COMMAND_FORMATTER = lambda do |runnable|
filename = Minitest::Queue.relative_path(runnable.source_location[0])
identifier = "#{runnable.klass}##{runnable.name}"
['bundle', 'exec', 'ruby', '-Ilib:test', filename, '-n', identifier]
end
RAILS_RUN_COMMAND_FORMATTER = lambda do |runnable|
filename = Minitest::Queue.relative_path(runnable.source_location[0])
lineno = runnable.source_location[1]
['bin/rails', 'test', "#{filename}:#{lineno}"]
end
def run_command_for_runnable(runnable)
command = run_command_formatter.call(runnable)
if command.is_a?(Array)
Shellwords.join(command)
else
command
end
end
def self.project_root
@project_root ||= Dir.pwd
end
def self.relative_path(path, root: project_root)
Pathname(path).relative_path_from(Pathname(root)).to_s
rescue ArgumentError, TypeError
path
end
class << self
def queue
Minitest.queue
end
def run(reporter, *)
rescue_run_errors do
queue.poll do |example|
result = queue.with_heartbeat(example.id) do
example.run
end
handle_test_result(reporter, example, result)
end
queue.stop_heartbeat!
end
end
def handle_test_result(reporter, example, result)
failed = !(result.passed? || result.skipped?)
if example.flaky?
result.mark_as_flaked!
failed = false
end
if failed && queue.config.failing_test && queue.config.failing_test != example.id
# When we do a bisect, we don't care about the result other than the test we're running the bisect on
result.mark_as_flaked!
failed = false
elsif failed
queue.report_failure!
else
queue.report_success!
end
if failed && CI::Queue.requeueable?(result) && queue.requeue(example)
result.requeue!
end
reporter.record(result)
end
private
def rescue_run_errors(&block)
block.call
rescue Errno::EPIPE
# This happens when the heartbeat process dies
reopen_previous_step
puts red("The heartbeat process died. This worker is exiting early.")
exit!(41)
rescue CI::Queue::Error => error
reopen_previous_step
puts red("#{error.class}: #{error.message}")
error.backtrace.each do |frame|
puts red(frame)
end
exit!(41)
rescue => error
reopen_previous_step
Minitest.queue.report_worker_error(error)
puts red("This worker exited because of an uncaught application error:")
puts red("#{error.class}: #{error.message}")
error.backtrace.each do |frame|
puts red(frame)
end
exit!(42)
end
end
class SingleExample
attr_reader :runnable, :method_name
def initialize(runnable, method_name)
@runnable = runnable
@method_name = method_name
end
def id
@id ||= "#{@runnable}##{@method_name}".freeze
end
def <=>(other)
id <=> other.id
end
def with_timestamps
start_timestamp = current_timestamp
result = yield
result
ensure
if result
result.start_timestamp = start_timestamp
result.finish_timestamp = current_timestamp
end
end
def run
with_timestamps do
Minitest.run_one_method(@runnable, @method_name)
end
end
def flaky?
Minitest.queue.flaky?(self)
end
def source_location
@runnable.instance_method(@method_name).source_location
rescue NameError, NoMethodError
nil
end
private
def current_timestamp
CI::Queue.time_now.to_i
end
end
attr_accessor :queue
def queue_reporters=(reporters)
@queue_reporters ||= []
Reporters.use!(((Reporters.reporters || []) - @queue_reporters) + reporters)
Minitest.backtrace_filter.add_filter(%r{exe/minitest-queue|lib/ci/queue/})
@queue_reporters = reporters
end
def loaded_tests
Minitest::Test.runnables.flat_map do |runnable|
runnable.runnable_methods.map do |method_name|
SingleExample.new(runnable, method_name)
end
end
end
def __run(*args)
if queue
puts "------- Running tests #{queue.config.worker_id}"
Queue.run(*args)
if queue.config.circuit_breakers.any?(&:open?)
STDERR.puts queue.config.circuit_breakers.map(&:message).join(' ').strip
end
if queue.max_test_failed?
STDERR.puts 'This worker is exiting early because too many failed tests were encountered.'
end
else
super
end
end
end
end
Minitest.singleton_class.prepend(Minitest::Queue)
if defined? Minitest::Result
Minitest::Result.prepend(Minitest::Requeueing)
Minitest::Result.prepend(Minitest::Flakiness)
Minitest::Result.prepend(Minitest::WithTimestamps)
else
Minitest::Test.prepend(Minitest::Requeueing)
Minitest::Test.prepend(Minitest::Flakiness)
Minitest::Test.prepend(Minitest::WithTimestamps)
module MinitestBackwardCompatibility
def source_location
method(name).source_location
end
def klass
self.class.name
end
end
Minitest::Test.prepend(MinitestBackwardCompatibility)
end