Class: Minitest::Queue::Runner

Inherits:
Object
  • Object
show all
Includes:
CI::Queue::OutputHelpers
Defined in:
lib/minitest/queue/runner.rb

Constant Summary collapse

Error =
Class.new(StandardError)
MissingParameter =
Class.new(Error)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



29
30
31
32
33
34
35
# File 'lib/minitest/queue/runner.rb', line 29

def initialize(argv)
  @queue_config = CI::Queue::Configuration.from_env(ENV)
  @command, @argv = parse(argv)
  if Minitest.respond_to?(:seed=)
    Minitest.seed = @queue_config.seed.to_i
  end
end

Class Attribute Details

.load_tests_durationObject

Returns the value of attribute load_tests_duration.



22
23
24
# File 'lib/minitest/queue/runner.rb', line 22

def load_tests_duration
  @load_tests_duration
end

.run_startObject

Returns the value of attribute run_start.



22
23
24
# File 'lib/minitest/queue/runner.rb', line 22

def run_start
  @run_start
end

.total_filesObject

Returns the value of attribute total_files.



22
23
24
# File 'lib/minitest/queue/runner.rb', line 22

def total_files
  @total_files
end

Class Method Details

.invoke(argv) ⇒ Object



25
26
27
# File 'lib/minitest/queue/runner.rb', line 25

def self.invoke(argv)
  new(argv).run!
end

Instance Method Details

#bisect_commandObject



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
# File 'lib/minitest/queue/runner.rb', line 197

def bisect_command
  invalid_usage! "Missing the FAILING_TEST argument." unless queue_config.failing_test

  set_load_path
  @queue = CI::Queue::Bisect.new(queue_url, queue_config)
  Minitest.queue = queue
  prepare_queue_for_execution

  step("Testing the failing test in isolation")
  unless queue.failing_test_present?
    puts reopen_previous_step
    puts red("The failing test does not exist.")
    File.write('log/test_order.log', "")
    File.write('log/bisect_test_details.log', "")
    exit! 1
  end

  unless run_tests_in_fork(queue.failing_test)
    puts reopen_previous_step
    puts red("The test fail when ran alone, no need to bisect.")
    File.write('log/test_order.log', queue_config.failing_test)
    File.write('log/bisect_test_details.log', "")
    exit! 0
  end

  run_index = 0
  while queue.suspects_left > 1
    run_index += 1
    step("Run ##{run_index}, #{queue.suspects_left} suspects left")
    if run_tests_in_fork(queue.candidates)
      queue.succeeded!
    else
      queue.failed!
    end
    puts
  end

  if queue.suspects_left == 0
    step(yellow("The failing test was the first test in the test order so there is nothing to bisect."))
    File.write('log/test_order.log', "")
    File.write('log/bisect_test_details.log', "")
    exit! 1
  end

  failing_order = queue.candidates
  step("Final validation")
  if run_tests_in_fork(failing_order)
    step(yellow("The bisection was inconclusive, there might not be any leaky test here."))
    File.write('log/test_order.log', "")
    File.write('log/bisect_test_details.log', "")
    exit! 1
  else
    step(green('The following command should reproduce the leak on your machine:'), collapsed: false)
    command = %w(bundle exec minitest-queue --queue - run)
    command << "-I#{load_paths}" if load_paths
    command += argv

    puts
    puts "cat <<'EOF' |\n#{failing_order.to_a.map(&:id).join("\n")}\nEOF\n#{command.join(' ')}"
    puts

    File.write('log/test_order.log', failing_order.to_a.map(&:id).join("\n"))

    bisect_test_details = failing_order.to_a.map do |test|
      source_location = test.source_location
      file_path = source_location&.first || 'unknown'
      line_number = source_location&.last || -1
      "#{test.id} #{file_path}:#{line_number}"
    end

    File.write('log/bisect_test_details.log', bisect_test_details.join("\n"))

    exit! 0
  end
end

#grind_commandObject



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
# File 'lib/minitest/queue/runner.rb', line 160

def grind_command
  invalid_usage!('No list to grind provided') if grind_list.nil?
  invalid_usage!('No grind count provided') if grind_count.nil?

  set_load_path

  queue_config.build_id = queue_config.build_id + '-grind'
  queue_config.grind_count = grind_count

  reporter_queue = CI::Queue::Redis::Grind.new(queue_url, queue_config)

  Minitest.queue = queue
  reporters = [
    GrindRecorder.new(build: reporter_queue.build),
    TestDataReporter.new(namespace: queue_config&.namespace),
  ]

  if queue_config.track_test_duration
    test_time_record = CI::Queue::Redis::TestTimeRecord.new(queue_url, queue_config)
    reporters << TestTimeRecorder.new(build: test_time_record)
  end

  if queue_config.statsd_endpoint
    reporters << Minitest::Reporters::StatsdReporter.new(statsd_endpoint: queue_config.statsd_endpoint)
  end
  Minitest.queue_reporters = reporters

  trap('TERM') { Minitest.queue.shutdown! }
  trap('INT') { Minitest.queue.shutdown! }

  @queue = CI::Queue::Grind.new(grind_list, queue_config)
  Minitest.queue = queue
  prepare_queue_for_execution

  # Let minitest's at_exit hook trigger
end

#release_commandObject



155
156
157
158
# File 'lib/minitest/queue/runner.rb', line 155

def release_command
  require_worker_id!
  queue.release!
end

#report_commandObject



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
# File 'lib/minitest/queue/runner.rb', line 273

def report_command
  supervisor = begin
    queue.supervisor
  rescue NotImplementedError => error
    abort! error.message
  end

  step("Waiting for workers to complete")

  unless supervisor.wait_for_workers { display_warnings(supervisor.build) }
    unless supervisor.queue_initialized?
      abort! "No leader was elected. This typically means no worker was able to start. Were there any errors during application boot?", 40
    end

    unless supervisor.exhausted?
      reporter = BuildStatusReporter.new(supervisor: supervisor)
      exit_code = reporter.report
      reporter.write_failure_file(queue_config.failure_file) if queue_config.failure_file
      reporter.write_flaky_tests_file(queue_config.export_flaky_tests_file) if queue_config.export_flaky_tests_file

      abort!("#{supervisor.size} tests weren't run.", exit_code)
    end
  end

  reporter = BuildStatusReporter.new(supervisor: supervisor)
  reporter.write_failure_file(queue_config.failure_file) if queue_config.failure_file
  reporter.write_flaky_tests_file(queue_config.export_flaky_tests_file) if queue_config.export_flaky_tests_file
  exit_code = reporter.report
  print_worker_profiles(supervisor)
  exit! exit_code
end

#report_grind_commandObject



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
330
331
332
333
# File 'lib/minitest/queue/runner.rb', line 305

def report_grind_command
  queue_config.build_id = queue_config.build_id + '-grind'
  @queue = CI::Queue::Redis::Grind.new(queue_url, queue_config)

  supervisor = begin
    queue.supervisor
  rescue NotImplementedError => error
    abort! error.message
  end

  grind_reporter = GrindReporter.new(build: supervisor.build)
  grind_reporter.report

  test_time_reporter_success = if queue_config.track_test_duration
    test_time_record = CI::Queue::Redis::TestTimeRecord.new(queue_url, queue_config)
    test_time_reporter = Minitest::Queue::TestTimeReporter.new(
      build: test_time_record,
      limit: queue_config.max_test_duration,
      percentile: queue_config.max_test_duration_percentile,
    )
    test_time_reporter.report

    test_time_reporter.success?
  else
    true
  end

  exit! grind_reporter.success? && test_time_reporter_success ? 0 : 1
end

#retry_commandObject



51
52
53
54
55
# File 'lib/minitest/queue/runner.rb', line 51

def retry_command
  require_worker_id!
  STDERR.puts "Warning: the retry subcommand is deprecated."
  run_command # aliased for backward compatibility purpose
end

#run!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/minitest/queue/runner.rb', line 37

def run!
  invalid_usage!("No command given") if command.nil?
  invalid_usage!('Missing queue URL') unless queue_url

  @queue = CI::Queue.from_uri(queue_url, queue_config)

  method = "#{command}_command"
  if respond_to?(method)
    public_send(method)
  else
    invalid_usage!("Unknown command: #{command}")
  end
end

#run_commandObject



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
# File 'lib/minitest/queue/runner.rb', line 57

def run_command
  Runner.run_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  require_worker_id!
  # if it's an automatic job retry we should process the main queue
  if manual_retry?
    if queue.expired?
      abort! "The test run is too old and can't be retried"
    end
    reset_counters
    retry_queue = queue.retry_queue
    if retry_queue.exhausted?
      puts "The retry queue does not contain any failure, we'll process the main queue instead."
    else
      puts "Retrying failed tests."
      self.queue = retry_queue
    end
  end

  queue.rescue_connection_errors { queue.created_at = CI::Queue.time_now.to_f }
  queue.boot_heartbeat_process!

  set_load_path
  Minitest.queue = queue
  reporters = [
    LocalRequeueReporter.new(verbose: verbose),
    BuildStatusRecorder.new(build: queue.build),
    JUnitReporter.new,
    TestDataReporter.new(namespace: queue_config&.namespace),
    OrderReporter.new(path: 'log/test_order.log'),
  ]
  if queue_config.statsd_endpoint
    reporters << Minitest::Reporters::StatsdReporter.new(statsd_endpoint: queue_config.statsd_endpoint)
  end
  Minitest.queue_reporters = reporters

  trap('TERM') { Minitest.queue.shutdown! }
  trap('INT') { Minitest.queue.shutdown! }

  if queue.rescue_connection_errors { queue.exhausted? }
    puts green('All tests were ran already')
  else
    # If the job gets (automatically) retried and there are still workers running but not many tests left
    # in the queue, we assume by the time the application is booted the queue is empty and it's faster to no-op.
    if retry? && queue.rescue_connection_errors { queue.queue_initialized? }
      remaining = queue.rescue_connection_errors { queue.remaining }.to_i
      running = queue.rescue_connection_errors { queue.running }.to_i

      puts "#{remaining} tests left and #{running} workers running."
      if remaining <= running
        puts green("Queue almost empty, exiting early...")
      else
        prepare_queue_for_execution
      end
    else
      prepare_queue_for_execution
    end
  end

  if queue_config.lazy_load
    # In lazy-load mode, run minitest explicitly instead of relying on
    # minitest/autorun's at_exit hook, which may not be registered since
    # test files haven't been loaded yet. exit! prevents double-execution
    # if minitest/autorun was loaded by the leader during streaming.
    passed = Minitest.run []
    verify_reporters!(reporters)
    exit!(passed ? 0 : 1)
  else
    at_exit {
      verify_reporters!(reporters)
    }
    # Let minitest's at_exit hook trigger
  end
end

#verify_reporters!(reporters) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/minitest/queue/runner.rb', line 132

def verify_reporters!(reporters)
  return unless reporters.any? { |r| !Minitest::Reporters.reporters.include?(r) }

  warn <<~WARNING
    WARNING!

    ci-queue requires several custom minitest reporters.
    Please do not overwrite them.
    If you have a statement in your test suite like this

      Minitest::Reporters.use!(SomeReporter.new)

    you should only run it when other reporters have not been configured
    to avoid breaking ci-queue's functionality and getting false test summaries.

    Use something like this:

      if Minitest::Reporters.reporters.nil?
        Minitest::Reporters.use!(SomeReporter.new)
      end
  WARNING
end