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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



22
23
24
25
26
27
28
# File 'lib/minitest/queue/runner.rb', line 22

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 Method Details

.invoke(argv) ⇒ Object



18
19
20
# File 'lib/minitest/queue/runner.rb', line 18

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

Instance Method Details

#bisect_commandObject



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

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

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

  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', "")
    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)
    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', "")
    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', "")
    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"))
    exit! 0
  end
end

#grind_commandObject



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

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! }

  load_tests

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

  # Let minitest's at_exit hook trigger
end

#release_commandObject



138
139
140
141
# File 'lib/minitest/queue/runner.rb', line 138

def release_command
  require_worker_id!
  queue.release!
end

#report_commandObject



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

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 master was elected. Did all workers crash?", 40
    end

    unless supervisor.exhausted?
      reporter = BuildStatusReporter.new(build: supervisor.build)
      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

      msg = "#{supervisor.size} tests weren't run."

      if supervisor.max_test_failed?
        puts('Encountered too many failed tests. Test run was ended early.')
        abort!(msg)
      else
        abort!(msg)
      end
    end
  end

  reporter = BuildStatusReporter.new(build: supervisor.build)
  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
  reporter.report

  exit! reporter.success? ? 0 : 1
end

#report_grind_commandObject



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

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



44
45
46
47
48
# File 'lib/minitest/queue/runner.rb', line 44

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

#run!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minitest/queue/runner.rb', line 30

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



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

def run_command
  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
        load_tests
        populate_queue
      end
    else
      load_tests
      populate_queue
    end
  end

  at_exit {
    verify_reporters!(reporters)
  }
  # Let minitest's at_exit hook trigger
end

#verify_reporters!(reporters) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/minitest/queue/runner.rb', line 115

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