Module: Minitest::Queue

Extended by:
CI::Queue::OutputHelpers
Defined in:
lib/minitest/queue.rb,
lib/minitest/queue/runner.rb,
lib/minitest/queue/statsd.rb,
lib/minitest/queue/test_data.rb,
lib/minitest/queue/error_report.rb,
lib/minitest/queue/grind_recorder.rb,
lib/minitest/queue/grind_reporter.rb,
lib/minitest/queue/junit_reporter.rb,
lib/minitest/queue/failure_formatter.rb,
lib/minitest/queue/test_data_reporter.rb,
lib/minitest/queue/test_time_recorder.rb,
lib/minitest/queue/test_time_reporter.rb,
lib/minitest/queue/lazy_entry_resolver.rb,
lib/minitest/queue/lazy_test_discovery.rb,
lib/minitest/queue/build_status_recorder.rb,
lib/minitest/queue/build_status_reporter.rb,
lib/minitest/queue/local_requeue_reporter.rb,
lib/minitest/queue/worker_profile_reporter.rb,
lib/minitest/queue/queue_population_strategy.rb

Defined Under Namespace

Classes: BuildStatusRecorder, BuildStatusReporter, ErrorReport, FailureFormatter, GrindRecorder, GrindReporter, JUnitReporter, LazyEntryResolver, LazySingleExample, LazyTestDiscovery, LocalRequeueReporter, OrderReporter, QueuePopulationStrategy, Runner, SingleExample, Statsd, TestData, TestDataReporter, TestTimeRecorder, TestTimeReporter, WorkerProfileReporter

Constant Summary collapse

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.parallel_worker_idObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/minitest/queue.rb', line 176

def parallel_worker_id
  return @parallel_worker_id if @parallel_worker_id

  # Memoized per process: re-read after a fork, but not on every test.
  if @parallel_worker_id_env_pid != Process.pid
    @parallel_worker_id_env_pid = Process.pid
    @parallel_worker_id_from_env = parallel_worker_id_from_env
  end
  @parallel_worker_id_from_env
end

Instance Attribute Details

#project_root=(value) ⇒ Object (writeonly)

Sets the attribute project_root

Parameters:

  • value

    the value to set the attribute project_root to.



125
126
127
# File 'lib/minitest/queue.rb', line 125

def project_root=(value)
  @project_root = value
end

#queueObject

Returns the value of attribute queue.



629
630
631
# File 'lib/minitest/queue.rb', line 629

def queue
  @queue
end

#run_command_formatterObject



130
131
132
133
134
135
136
# File 'lib/minitest/queue.rb', line 130

def run_command_formatter
  @run_command_formatter ||= if defined?(Rails) && defined?(Rails::TestUnitRailtie)
    RAILS_RUN_COMMAND_FORMATTER
  else
    DEFAULT_RUN_COMMAND_FORMATTER
  end
end

Class Method Details

.handle_test_result(reporter, example, result) ⇒ Object



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

def handle_test_result(reporter, example, result)
  (result)

  if result.respond_to?(:queue_id=)
    result.queue_id = example.id
    result.queue_entry = example.queue_entry if result.respond_to?(:queue_entry=)
  end

  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
  end

  if failed && CI::Queue.requeueable?(result) && queue.requeue(example.queue_entry)
    queue.report_requeue!
    result.requeue!
    if CI::Queue.debug?
      $stderr.puts "[ci-queue][requeue] test_id=#{example.id} error_class=#{result.failures.first&.class} error=#{result.failures.first&.message&.lines&.first&.chomp}"
    end
  elsif failed
    queue.report_failure!
  else
    queue.report_success!
  end
  reporter.record(result)
end

.project_rootObject



159
160
161
# File 'lib/minitest/queue.rb', line 159

def self.project_root
  @project_root ||= Dir.pwd
end

.queueObject



224
225
226
# File 'lib/minitest/queue.rb', line 224

def queue
  Minitest.queue
end

.relative_path(path, root: project_root) ⇒ Object



163
164
165
166
167
# File 'lib/minitest/queue.rb', line 163

def self.relative_path(path, root: project_root)
  Pathname(path).relative_path_from(Pathname(root)).to_s
rescue ArgumentError, TypeError
  path
end

.run(reporter) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/minitest/queue.rb', line 228

def run(reporter, *)
  rescue_run_errors do
    begin
      queue.poll do |example|
        result = queue.with_heartbeat(example.queue_entry, lease: queue.lease_for(example.queue_entry)) do
          example.run
        end

        handle_test_result(reporter, example, result)
      end

      report_load_stats(queue)
    ensure
      store_worker_profile(queue)
      queue.stop_heartbeat!
    end
  end
end

.stamp_parallel_worker_metadata(result) ⇒ Object

Stamps per-process execution metadata on a result. Must be called in the process that ran the test so pid and per-process test index are captured worker-side.

First writer wins: results that were already stamped are left untouched. Embedding environments that run tests in forked workers and transport results to another process (e.g. over DRb) MUST call this in the worker before sending, otherwise the stamp applied during reporting would carry the reporting process's pid and an arrival-order index interleaved across workers.

The counter is mutex-guarded: results recorded from multiple threads (e.g. a DRb server dispatching each call on its own thread) get unique, gap-free indexes reflecting record order in this process. Returns the result when it was stamped, nil when it was skipped.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/minitest/queue.rb', line 202

def (result)
  return unless result.respond_to?(:parallel_worker_pid=)
  return if result.parallel_worker_pid # already stamped worker-side

  PARALLEL_WORKER_METADATA_MUTEX.synchronize do
    pid = Process.pid
    if @parallel_worker_metadata_pid != pid
      # Restart the per-process counter in forked workers so that
      # (parallel_worker_id, parallel_worker_pid, parallel_worker_test_index)
      # always reflects execution order within a single process.
      @parallel_worker_metadata_pid = pid
      @parallel_worker_next_test_index = 0
    end

    result.parallel_worker_id = parallel_worker_id
    result.parallel_worker_pid = pid
    result.parallel_worker_test_index = @parallel_worker_next_test_index
    @parallel_worker_next_test_index += 1
  end
  result
end

Instance Method Details

#loaded_testsObject



644
645
646
647
648
649
650
# File 'lib/minitest/queue.rb', line 644

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

#queue_reporters=(reporters) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/minitest/queue.rb', line 631

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

  # minitest 6 made plugin loading opt-in (no longer called from
  # Minitest.run). Ensure minitest-reporters' plugin is loaded so
  # that its DelegateReporter wires our reporters into the
  # CompositeReporter that Minitest.run creates.
  Minitest.load_plugins if Minitest.respond_to?(:load_plugins)
end

#run_command_for_runnable(runnable) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/minitest/queue.rb', line 150

def run_command_for_runnable(runnable)
  command = run_command_formatter.call(runnable)
  if command.is_a?(Array)
    Shellwords.join(command)
  else
    command
  end
end