Class: TestQueue::Runner
- Inherits:
-
Object
- Object
- TestQueue::Runner
- Defined in:
- lib/test_queue/runner.rb,
lib/test_queue/runner/rspec.rb,
lib/test_queue/runner/example.rb,
lib/test_queue/runner/cucumber.rb,
lib/test_queue/runner/minitest.rb,
lib/test_queue/runner/testunit.rb,
lib/test_queue/runner/minitest5.rb,
lib/test_queue/runner/puppet_lint.rb
Defined Under Namespace
Classes: Cucumber, Example, Minitest, PuppetLint, RSpec, TestUnit
Constant Summary collapse
- TOKEN_REGEX =
/\ATOKEN=(\w+)/
- MiniTest =
For compatibility with test-queue 0.7.0 and earlier.
Minitest
Instance Attribute Summary collapse
-
#concurrency ⇒ Object
Returns the value of attribute concurrency.
-
#exit_when_done ⇒ Object
Returns the value of attribute exit_when_done.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
-
#abort(message) ⇒ Object
Stop the test run immediately.
-
#after_fork(num) ⇒ Object
Prepare a worker for executing jobs after a fork.
- #after_fork_internal(num, iterator) ⇒ Object
- #around_filter(_suite) ⇒ Object
- #awaiting_suites? ⇒ Boolean
- #cleanup_worker ⇒ Object
- #collect_worker_data(worker) ⇒ Object
- #connect_to_relay ⇒ Object
- #discover_suites ⇒ Object
- #distribute_queue ⇒ Object
- #enqueue_discovered_suite(suite_name, path) ⇒ Object
-
#execute ⇒ Object
Run the tests.
- #execute_internal ⇒ Object
-
#initialize(test_framework, concurrency = nil, socket = nil, relay = nil) ⇒ Runner
constructor
A new instance of Runner.
- #kill_subprocesses ⇒ Object
- #kill_suite_discovery_process(signal = 'KILL') ⇒ Object
- #kill_workers ⇒ Object
-
#prepare(concurrency) ⇒ Object
Run in the master before the fork.
-
#queue_status(start_time, queue_size, local_worker_count, remote_worker_count) ⇒ Object
Subclasses can override to monitor the status of the queue.
- #reap_suite_discovery_process(blocking = true) ⇒ Object
- #reap_workers(blocking = true) ⇒ Object
- #relay? ⇒ Boolean
- #relay_to_master(worker) ⇒ Object
-
#run_worker(iterator) ⇒ Object
Entry point for internal runner implementations.
- #spawn_workers ⇒ Object
- #start_master ⇒ Object
- #start_relay ⇒ Object
- #stats_file ⇒ Object
- #stop_master ⇒ Object
- #summarize ⇒ Object
- #summarize_internal ⇒ Object
- #summarize_worker(worker) ⇒ Object
- #worker_completed(worker) ⇒ Object
Constructor Details
#initialize(test_framework, concurrency = nil, socket = nil, relay = nil) ⇒ Runner
Returns a new instance of Runner.
38 39 40 41 42 43 44 45 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 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 |
# File 'lib/test_queue/runner.rb', line 38 def initialize(test_framework, concurrency = nil, socket = nil, relay = nil) @test_framework = test_framework @stats = Stats.new(stats_file) @early_failure_limit = nil if ENV['TEST_QUEUE_EARLY_FAILURE_LIMIT'] begin @early_failure_limit = Integer(ENV['TEST_QUEUE_EARLY_FAILURE_LIMIT']) rescue ArgumentError raise ArgumentError, 'TEST_QUEUE_EARLY_FAILURE_LIMIT could not be parsed as an integer' end end @procline = $0 @allowlist = if (forced = ENV['TEST_QUEUE_FORCE']) forced.split(/\s*,\s*/) else [] end @allowlist.freeze all_files = @test_framework.all_suite_files.to_set @queue = @stats.all_suites .select { |suite| all_files.include?(suite.path) } .sort_by { |suite| -suite.duration } .map { |suite| [suite.name, suite.path] } if @allowlist.any? @queue.select! { |suite_name, _path| @allowlist.include?(suite_name) } @queue.sort_by! { |suite_name, _path| @allowlist.index(suite_name) } end @awaited_suites = Set.new(@allowlist) @original_queue = Set.new(@queue).freeze @workers = {} @completed = [] @concurrency = concurrency || ENV['TEST_QUEUE_WORKERS']&.to_i || if File.exist?('/proc/cpuinfo') File.read('/proc/cpuinfo').split("\n").grep(/processor/).size elsif RUBY_PLATFORM.include?('darwin') `/usr/sbin/sysctl -n hw.activecpu`.to_i else 2 end unless @concurrency > 0 raise ArgumentError, "Worker count (#{@concurrency}) must be greater than 0" end @relay_connection_timeout = ENV['TEST_QUEUE_RELAY_TIMEOUT']&.to_i || 30 @run_token = ENV['TEST_QUEUE_RELAY_TOKEN'] || SecureRandom.hex(8) @socket = socket || ENV['TEST_QUEUE_SOCKET'] || "/tmp/test_queue_#{$$}_#{object_id}.sock" @relay = relay || ENV['TEST_QUEUE_RELAY'] @remote_master_message = ENV['TEST_QUEUE_REMOTE_MASTER_MESSAGE'] if ENV.key?('TEST_QUEUE_REMOTE_MASTER_MESSAGE') if @relay == @socket warn '*** Detected TEST_QUEUE_RELAY == TEST_QUEUE_SOCKET. Disabling relay mode.' @relay = nil elsif @relay @queue = [] end @discovered_suites = Set.new @assignments = {} @exit_when_done = true @aborting = false end |
Instance Attribute Details
#concurrency ⇒ Object
Returns the value of attribute concurrency.
33 34 35 |
# File 'lib/test_queue/runner.rb', line 33 def concurrency @concurrency end |
#exit_when_done ⇒ Object
Returns the value of attribute exit_when_done.
33 34 35 |
# File 'lib/test_queue/runner.rb', line 33 def exit_when_done @exit_when_done end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
34 35 36 |
# File 'lib/test_queue/runner.rb', line 34 def stats @stats end |
Instance Method Details
#abort(message) ⇒ Object
Stop the test run immediately.
message - String message to print to the console when exiting.
Doesn’t return.
599 600 601 602 603 |
# File 'lib/test_queue/runner.rb', line 599 def abort() @aborting = true kill_subprocesses Kernel.abort("Aborting: #{}") end |
#after_fork(num) ⇒ Object
Prepare a worker for executing jobs after a fork.
397 398 |
# File 'lib/test_queue/runner.rb', line 397 def after_fork(num) end |
#after_fork_internal(num, iterator) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/test_queue/runner.rb', line 369 def after_fork_internal(num, iterator) srand output = File.open("/tmp/test_queue_worker_#{$$}_output", 'w') $stdout.reopen(output) $stderr.reopen($stdout) $stdout.sync = $stderr.sync = true $0 = "test-queue worker [#{num}]" puts puts "==> Starting #{$0} (#{Process.pid} on #{Socket.gethostname}) - iterating over #{iterator.sock}" puts after_fork(num) end |
#around_filter(_suite) ⇒ Object
392 393 394 |
# File 'lib/test_queue/runner.rb', line 392 def around_filter(_suite) yield end |
#awaiting_suites? ⇒ Boolean
331 332 333 334 335 336 337 338 339 340 |
# File 'lib/test_queue/runner.rb', line 331 def awaiting_suites? # We're waiting to find all the allowlisted suites so we can run them in the correct order. # Or we don't have any suites yet, but we're working on it. if @awaited_suites.any? || @queue.empty? && !!@discovering_suites_pid true else # It's fine to run any queued suites now. false end end |
#cleanup_worker ⇒ Object
412 413 |
# File 'lib/test_queue/runner.rb', line 412 def cleanup_worker end |
#collect_worker_data(worker) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/test_queue/runner.rb', line 437 def collect_worker_data(worker) if File.exist?(file = "/tmp/test_queue_worker_#{worker.pid}_output") worker.output = File.binread(file) FileUtils.rm(file) end if File.exist?(file = "/tmp/test_queue_worker_#{worker.pid}_suites") worker.suites.replace(Marshal.load(File.binread(file))) FileUtils.rm(file) end end |
#connect_to_relay ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/test_queue/runner.rb', line 535 def connect_to_relay sock = nil start = Time.now puts "Attempting to connect for #{@relay_connection_timeout}s..." while sock.nil? begin sock = TCPSocket.new(*@relay.split(':')) rescue Errno::ECONNREFUSED => e raise e if Time.now - start > @relay_connection_timeout puts 'Master not yet available, sleeping...' sleep 0.5 end end sock end |
#discover_suites ⇒ Object
299 300 301 302 303 304 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 |
# File 'lib/test_queue/runner.rb', line 299 def discover_suites # Remote masters don't discover suites; the central master does and # distributes them to remote masters. return if relay? # No need to discover suites if all allowlisted suites are already # queued. return if @allowlist.any? && @awaited_suites.empty? @discovering_suites_pid = fork do terminate = false Signal.trap('INT') { terminate = true } $0 = 'test-queue suite discovery process' @test_framework.all_suite_files.each do |path| @test_framework.suites_from_file(path).each do |suite_name, _suite| Kernel.exit!(0) if terminate @server.connect_address.connect do |sock| sock.puts("TOKEN=#{@run_token}") data = Marshal.dump([suite_name, path]) sock.puts("NEW SUITE #{data.bytesize}") sock.write(data) end end end Kernel.exit! 0 end end |
#distribute_queue ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/test_queue/runner.rb', line 456 def distribute_queue return if relay? remote_workers = 0 until !awaiting_suites? && @queue.empty? && remote_workers == 0 queue_status(@start_time, @queue.size, @workers.size, remote_workers) if (status = reap_suite_discovery_process(false)) abort('Discovering suites failed.') unless status.success? abort("Failed to discover #{@awaited_suites.sort.join(', ')} specified in TEST_QUEUE_FORCE") if @awaited_suites.any? end if @server.wait_readable(0.1).nil? reap_workers(false) # check for worker deaths else sock = @server.accept token = sock.gets.strip cmd = sock.gets.strip token = token[TOKEN_REGEX, 1] # If we have a remote master from a different test run, respond with "WRONG RUN", and it will consider the test run done. if token != @run_token = token.nil? ? 'Worker sent no token to master' : "Worker from run #{token} connected to master" warn "*** #{} for run #{@run_token}; ignoring." sock.write("WRONG RUN\n") next end case cmd when /\APOP (\S+) (\d+)/ hostname = $1 pid = Integer($2) if awaiting_suites? sock.write(Marshal.dump('WAIT')) elsif (obj = @queue.shift) data = Marshal.dump(obj) sock.write(data) @assignments[obj] = [hostname, pid] end when /\AREMOTE MASTER (\d+) ([\w.-]+)(?: (.+))?/ num = $1.to_i remote_master = $2 = $3 sock.write("OK\n") remote_workers += num = "*** #{num} workers connected from #{remote_master} after #{Time.now - @start_time}s" += " #{}" if warn when /\AWORKER (\d+)/ data = sock.read($1.to_i) worker = Marshal.load(data) worker_completed(worker) remote_workers -= 1 when /\ANEW SUITE (\d+)/ data = sock.read($1.to_i) suite_name, path = Marshal.load(data) enqueue_discovered_suite(suite_name, path) when /\AKABOOM/ # worker reporting an abnormal number of test failures; # stop everything immediately and report the results. break else warn("Ignoring unrecognized command: \"#{cmd}\"") end sock.close end end ensure stop_master reap_workers end |
#enqueue_discovered_suite(suite_name, path) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/test_queue/runner.rb', line 342 def enqueue_discovered_suite(suite_name, path) if @allowlist.any? && !@allowlist.include?(suite_name) return end @discovered_suites << [suite_name, path] if @original_queue.include?([suite_name, path]) # This suite was already added to the queue some other way. @awaited_suites.delete(suite_name) return end # We don't know how long new suites will take to run, so we put them at # the front of the queue. It's better to run a fast suite early than to # run a slow suite late. @queue.unshift [suite_name, path] if @awaited_suites.delete?(suite_name) && @awaited_suites.empty? # We've found all the allowlisted suites. Sort the queue to match the # allowlist. @queue.sort_by! { |queued_suite_name, _path| @allowlist.index(queued_suite_name) } kill_suite_discovery_process('INT') end end |
#execute ⇒ Object
Run the tests.
If exit_when_done is true, exit! will be called before this method completes. If exit_when_done is false, this method will return an Integer number of failures.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/test_queue/runner.rb', line 115 def execute $stdout.sync = $stderr.sync = true @start_time = Time.now execute_internal exitstatus = summarize_internal if exit_when_done exit! exitstatus else exitstatus end end |
#execute_internal ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/test_queue/runner.rb', line 221 def execute_internal start_master prepare(@concurrency) @prepared_time = Time.now start_relay if relay? discover_suites spawn_workers distribute_queue ensure stop_master kill_subprocesses end |
#kill_subprocesses ⇒ Object
564 565 566 567 |
# File 'lib/test_queue/runner.rb', line 564 def kill_subprocesses kill_workers kill_suite_discovery_process end |
#kill_suite_discovery_process(signal = 'KILL') ⇒ Object
577 578 579 580 581 582 |
# File 'lib/test_queue/runner.rb', line 577 def kill_suite_discovery_process(signal = 'KILL') return unless @discovering_suites_pid Process.kill signal, @discovering_suites_pid reap_suite_discovery_process end |
#kill_workers ⇒ Object
569 570 571 572 573 574 575 |
# File 'lib/test_queue/runner.rb', line 569 def kill_workers @workers.each do |pid, _worker| Process.kill 'KILL', pid end reap_workers end |
#prepare(concurrency) ⇒ Object
Run in the master before the fork. Used to create concurrency copies of any databases required by the test workers.
389 390 |
# File 'lib/test_queue/runner.rb', line 389 def prepare(concurrency) end |
#queue_status(start_time, queue_size, local_worker_count, remote_worker_count) ⇒ Object
Subclasses can override to monitor the status of the queue.
For example, you may want to record metrics about how quickly remote workers connect, or abort the build if not enough connect.
This method is called very frequently during the test run, so don’t do anything expensive/blocking.
This method is not called on remote masters when using remote workers, only on the central master.
start_time - Time when the test run began queue_size - Integer number of suites left in the queue local_worker_count - Integer number of active local workers remote_worker_count - Integer number of active remote workers
Returns nothing.
622 623 |
# File 'lib/test_queue/runner.rb', line 622 def queue_status(start_time, queue_size, local_worker_count, remote_worker_count) end |
#reap_suite_discovery_process(blocking = true) ⇒ Object
584 585 586 587 588 589 590 591 592 |
# File 'lib/test_queue/runner.rb', line 584 def reap_suite_discovery_process(blocking = true) return unless @discovering_suites_pid _, status = Process.waitpid2(@discovering_suites_pid, blocking ? 0 : Process::WNOHANG) return unless status @discovering_suites_pid = nil status end |
#reap_workers(blocking = true) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/test_queue/runner.rb', line 420 def reap_workers(blocking = true) @workers.delete_if do |_, worker| if Process.waitpid(worker.pid, blocking ? 0 : Process::WNOHANG).nil? next false end worker.status = $? worker.end_time = Time.now collect_worker_data(worker) relay_to_master(worker) if relay? worker_completed(worker) true end end |
#relay? ⇒ Boolean
531 532 533 |
# File 'lib/test_queue/runner.rb', line 531 def relay? !!@relay end |
#relay_to_master(worker) ⇒ Object
552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/test_queue/runner.rb', line 552 def relay_to_master(worker) worker.host = Socket.gethostname data = Marshal.dump(worker) sock = connect_to_relay sock.puts("TOKEN=#{@run_token}") sock.puts("WORKER #{data.bytesize}") sock.write(data) ensure sock&.close end |
#run_worker(iterator) ⇒ Object
Entry point for internal runner implementations. The iterator will yield jobs from the shared queue on the master.
Returns an Integer number of failures.
404 405 406 407 408 409 410 |
# File 'lib/test_queue/runner.rb', line 404 def run_worker(iterator) iterator.each do |item| puts " #{item.inspect}" end 0 # exit status end |
#spawn_workers ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/test_queue/runner.rb', line 281 def spawn_workers @concurrency.times do |i| num = i + 1 pid = Process.fork do @server&.close iterator = Iterator.new(@test_framework, relay? ? @relay : @socket, method(:around_filter), early_failure_limit: @early_failure_limit, run_token: @run_token) after_fork_internal(num, iterator) ret = run_worker(iterator) || 0 cleanup_worker Kernel.exit! ret end @workers[pid] = Worker.new(pid, num) end end |
#start_master ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/test_queue/runner.rb', line 235 def start_master unless relay? if @socket =~ /\A(?:(.+):)?(\d+)\z/ address = $1 || '0.0.0.0' port = $2.to_i @socket = "#{$1}:#{$2}" @server = TCPServer.new(address, port) else FileUtils.rm_f(@socket) @server = UNIXServer.new(@socket) end end desc = "test-queue master (#{relay? ? "relaying to #{@relay}" : @socket})" puts "Starting #{desc}" $0 = "#{desc} - #{@procline}" end |
#start_relay ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/test_queue/runner.rb', line 253 def start_relay return unless relay? sock = connect_to_relay = @remote_master_message ? " #{@remote_master_message}" : '' = .gsub(/(\r|\n)/, '') # Our "protocol" is newline-separated sock.puts("TOKEN=#{@run_token}") sock.puts("REMOTE MASTER #{@concurrency} #{Socket.gethostname} #{}") response = sock.gets.strip unless response == 'OK' warn "*** Got non-OK response from master: #{response}" sock.close exit! 1 end sock.close rescue Errno::ECONNREFUSED warn "*** Unable to connect to relay #{@relay}. Aborting..." exit! 1 end |
#stats_file ⇒ Object
217 218 219 |
# File 'lib/test_queue/runner.rb', line 217 def stats_file ENV['TEST_QUEUE_STATS'] || '.test_queue_stats' end |
#stop_master ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/test_queue/runner.rb', line 273 def stop_master return if relay? FileUtils.rm_f(@socket) if @socket && @server.is_a?(UNIXServer) @server.close rescue nil if @server @socket = @server = nil end |
#summarize ⇒ Object
214 215 |
# File 'lib/test_queue/runner.rb', line 214 def summarize end |
#summarize_internal ⇒ Object
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 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/test_queue/runner.rb', line 129 def summarize_internal puts puts "==> Summary (#{@completed.size} workers in %.4fs)" % (Time.now - @start_time) puts estatus = 0 misrun_suites = [] unassigned_suites = [] @failures = '' @completed.each do |worker| estatus += (worker.status.exitstatus || 1) @stats.record_suites(worker.suites) worker.suites.each do |suite| assignment = @assignments.delete([suite.name, suite.path]) host = worker.host || Socket.gethostname if assignment.nil? unassigned_suites << [suite.name, suite.path] elsif assignment != [host, worker.pid] misrun_suites << [suite.name, suite.path] + assignment + [host, worker.pid] end @discovered_suites.delete([suite.name, suite.path]) end summarize_worker(worker) @failures += worker.failure_output if worker.failure_output puts ' [%2d] %60s %4d suites in %.4fs (%s %s)' % [ worker.num, worker.summary, worker.suites.size, worker.end_time - worker.start_time, worker.status.to_s, worker.host && " on #{worker.host.split('.').first}" ] end unless @failures.empty? puts puts '==> Failures' puts puts @failures end unless relay? unless @discovered_suites.empty? estatus += 1 puts puts 'The following suites were discovered but were not run:' puts @discovered_suites.sort.each do |suite_name, path| puts "#{suite_name} - #{path}" end end unless unassigned_suites.empty? estatus += 1 puts puts 'The following suites were not discovered but were run anyway:' puts unassigned_suites.sort.each do |suite_name, path| puts "#{suite_name} - #{path}" end end unless misrun_suites.empty? estatus += 1 puts puts 'The following suites were run on the wrong workers:' puts misrun_suites.each do |suite_name, path, target_host, target_pid, actual_host, actual_pid| puts "#{suite_name} - #{path}: #{actual_host} (#{actual_pid}) - assigned to #{target_host} (#{target_pid})" end end end puts @stats.save summarize estatus = @completed.inject(0) { |s, worker| s + (worker.status.exitstatus || 1) } [estatus, 255].min end |
#summarize_worker(worker) ⇒ Object
415 416 417 418 |
# File 'lib/test_queue/runner.rb', line 415 def summarize_worker(worker) worker.summary = '' worker.failure_output = '' end |
#worker_completed(worker) ⇒ Object
449 450 451 452 453 454 |
# File 'lib/test_queue/runner.rb', line 449 def worker_completed(worker) return if @aborting @completed << worker puts worker.output if ENV['TEST_QUEUE_VERBOSE'] || worker.status.exitstatus != 0 end |