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

def initialize(argv)
  @queue_config = CI::Queue::Configuration.from_env(ENV)
  @command, @argv = parse(argv)
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



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

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

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

  step("Testing the failing test in isolation")
  unless run_tests_in_fork(queue.failing_test)
    puts reopen_previous_step
    puts red("The test fail when ran alone, no need to bisect.")
    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

  failing_order = queue.candidates
  step("Final validation")
  status = if run_tests_in_fork(failing_order)
    step(yellow("The bisection was inconclusive, there might not be any leaky test here."))
    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
    exit! 0
  end
end

#grind_commandObject



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

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

#report_commandObject



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

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?"
    end

    unless supervisor.exhausted?
      msg = "#{supervisor.size} tests weren't run."
      if supervisor.max_test_failed?
        puts('Encountered too many failed tests. Test run was ended early.')
        puts(msg)
      else
        abort!(msg)
      end
    end
  end

  reporter = BuildStatusReporter.new(build: supervisor.build)

  if queue_config.failure_file
    failures = reporter.error_reports.map(&:to_h).to_json
    File.write(queue_config.failure_file, failures)
  end

  reporter.report
  exit! reporter.success? ? 0 : 1
end

#report_grind_commandObject



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

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



41
42
43
44
# File 'lib/minitest/queue/runner.rb', line 41

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

#run!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/minitest/queue/runner.rb', line 27

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



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

def run_command
  if queue.retrying?
    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

  set_load_path
  Minitest.queue = queue
  reporters = [
    LocalRequeueReporter.new,
    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
    load_tests
    populate_queue
  end
  # Let minitest's at_exit hook trigger
end