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.



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

def initialize(argv)
  @queue_config = CI::Queue::Configuration.from_env(ENV)
  @command, @argv = parse(argv)
end

Class Method Details

.invoke(argv) ⇒ Object



16
17
18
# File 'lib/minitest/queue/runner.rb', line 16

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

Instance Method Details

#bisect_command ⇒ Object



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

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

#report_command ⇒ Object



114
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 114

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
    unless supervisor.queue_initialized?
      abort! "No master was elected. Did all workers crash?"
    end

    unless supervisor.exhausted?
      abort! "#{supervisor.size} tests weren't run."
    end
  end

  reporter = BuildStatusReporter.new(build: supervisor.build)
  reporter.report
  exit! reporter.success? ? 0 : 1
end

#retry_command ⇒ Object



39
40
41
42
# File 'lib/minitest/queue/runner.rb', line 39

def retry_command
  self.queue = queue.retry_queue
  run_command
end

#run! ⇒ Object



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

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_command ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/minitest/queue/runner.rb', line 44

def run_command
  set_load_path
  Minitest.queue = queue
  reporters = [
    LocalRequeueReporter.new,
    BuildStatusRecorder.new(build: queue.build),
    JUnitReporter.new,
    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! }

  unless queue.rescue_connection_errors { queue.exhausted? }
    load_tests
    populate_queue
  end
  # Let minitest's at_exit hook trigger
end