Class: TestRunner::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/executor.rb

Constant Summary collapse

DEFAULT_RESOURCE_REFRESH_INTERVAL =
15
DEFAULT_STATUS_INTERVAL =
300
TEST_HEARTBEAT_INTERVAL =
300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_scripts, **opts) ⇒ Executor

Returns a new instance of Executor.

Parameters:

Options Hash (**opts):

  • :state_dir (String)
  • :jobs (Integer)
  • :jobs_auto (Boolean)
  • :default_timeout (Integer)
  • :stop_on_failure (Boolean)
  • :destructive (Boolean)
  • :recreate_disks (Boolean)
  • :resource_refresh_interval (Numeric)
  • :status_interval (Numeric)
  • :verbose (Boolean)
  • :repo_root (String)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/test-runner/executor.rb', line 34

def initialize(test_scripts, **opts)
  @test_scripts = test_scripts
  @opts = opts
  @workers = []
  @pending = []
  @results = []
  @running_tests = {}
  @stop_work = false
  @mutex = Mutex.new
  @scheduler_mutex = Mutex.new
  @scheduler_cv = ConditionVariable.new
  @resource_pool = ResourcePool.from_options(opts)
  @last_resource_wait_log_at = nil
  @resource_refresh_interval = parse_resource_refresh_interval(opts[:resource_refresh_interval])
  @resource_monitor_mutex = Mutex.new
  @resource_monitor_cv = ConditionVariable.new
  @resource_monitor_stop = false
  @resource_monitor = nil
  @status_interval = parse_status_interval(opts[:status_interval])
  @status_monitor_mutex = Mutex.new
  @status_monitor_cv = ConditionVariable.new
  @status_monitor_stop = false
  @status_monitor = nil

  fill_queue
end

Instance Attribute Details

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def mutex
  @mutex
end

#optsHash (readonly)

Returns:

  • (Hash)


16
17
18
# File 'lib/test-runner/executor.rb', line 16

def opts
  @opts
end

#pendingObject (readonly, protected)

Returns the value of attribute pending.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def pending
  @pending
end

#resource_monitor_cvObject (readonly, protected)

Returns the value of attribute resource_monitor_cv.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def resource_monitor_cv
  @resource_monitor_cv
end

#resource_monitor_mutexObject (readonly, protected)

Returns the value of attribute resource_monitor_mutex.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def resource_monitor_mutex
  @resource_monitor_mutex
end

#resource_poolObject (readonly, protected)

Returns the value of attribute resource_pool.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def resource_pool
  @resource_pool
end

#resource_refresh_intervalObject (readonly, protected)

Returns the value of attribute resource_refresh_interval.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def resource_refresh_interval
  @resource_refresh_interval
end

#resultsArray<TestResult> (readonly)

Returns:



19
20
21
# File 'lib/test-runner/executor.rb', line 19

def results
  @results
end

#running_testsObject (readonly, protected)

Returns the value of attribute running_tests.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def running_tests
  @running_tests
end

#scheduler_cvObject (readonly, protected)

Returns the value of attribute scheduler_cv.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def scheduler_cv
  @scheduler_cv
end

#scheduler_mutexObject (readonly, protected)

Returns the value of attribute scheduler_mutex.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def scheduler_mutex
  @scheduler_mutex
end

#status_intervalObject (readonly, protected)

Returns the value of attribute status_interval.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def status_interval
  @status_interval
end

#status_monitor_cvObject (readonly, protected)

Returns the value of attribute status_monitor_cv.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def status_monitor_cv
  @status_monitor_cv
end

#status_monitor_mutexObject (readonly, protected)

Returns the value of attribute status_monitor_mutex.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def status_monitor_mutex
  @status_monitor_mutex
end

#test_scriptsArray<TestScript> (readonly)

Returns:



13
14
15
# File 'lib/test-runner/executor.rb', line 13

def test_scripts
  @test_scripts
end

#workersObject (readonly, protected)

Returns the value of attribute workers.



133
134
135
# File 'lib/test-runner/executor.rb', line 133

def workers
  @workers
end

Instance Method Details

#build_accumulated_test_result(test, scripts, latest_script_results, elapsed_time, last_result) ⇒ Object (protected)



522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/test-runner/executor.rb', line 522

def build_accumulated_test_result(test, scripts, latest_script_results, elapsed_time, last_result)
  script_results = scripts.map do |script|
    latest_script_results.fetch(script) { TestScriptResult.new(script, false, -1) }
  end

  TestResult.new(
    test,
    script_results,
    last_result&.successful? || false,
    elapsed_time,
    last_result&.state_dir || test_state_dir(test)
  )
end

#classify_results(result_list) ⇒ Object (protected)



372
373
374
375
376
377
378
379
# File 'lib/test-runner/executor.rb', line 372

def classify_results(result_list)
  {
    expected_successful: result_list.select { |r| r.expected_to_succeed? && r.successful? },
    expected_failed: result_list.select { |r| r.expected_to_fail? && r.failed? },
    unexpected_failed: result_list.select { |r| r.expected_to_succeed? && r.failed? },
    unexpected_successful: result_list.select { |r| r.expected_to_fail? && r.successful? }
  }
end

#fill_queueObject (protected)



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/test-runner/executor.rb', line 147

def fill_queue
  tests = {}

  test_scripts.each do |ts|
    tests[ts.test] ||= []
    tests[ts.test] << ts
  end

  tests.to_a.shuffle!.each_with_index do |(test, scripts), i|
    @pending << [i, test, scripts.shuffle!]
  end

  @test_count = tests.length
end

#last_nonempty_line(path, max_bytes: 8192) ⇒ Object (protected)



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/test-runner/executor.rb', line 726

def last_nonempty_line(path, max_bytes: 8192)
  return nil unless File.file?(path)

  size = File.size(path)
  return nil if size <= 0

  offset = [size - max_bytes, 0].max
  data = File.open(path, 'rb') do |f|
    f.seek(offset)
    f.read
  end

  data.lines.reverse_each do |line|
    stripped = line.strip
    return stripped unless stripped.empty?
  end

  nil
rescue Errno::ENOENT, Errno::EACCES
  nil
end

#log(msg = '') ⇒ Object (protected)



748
749
750
# File 'lib/test-runner/executor.rb', line 748

def log(msg = '')
  mutex.synchronize { puts "[#{Time.now}] #{msg}" }
end

#log_reserved_resources(test, resources) ⇒ Object (protected)



431
432
433
434
435
436
# File 'lib/test-runner/executor.rb', line 431

def log_reserved_resources(test, resources)
  log(
    "Reserved resources for '#{test.path}': #{resources.summary}; " \
    "pool: #{resource_pool.status}"
  )
end

#log_resource_waitObject (protected)



438
439
440
441
442
443
444
445
446
447
448
# File 'lib/test-runner/executor.rb', line 438

def log_resource_wait
  now = Time.now
  return if @last_resource_wait_log_at && now - @last_resource_wait_log_at < 60

  @last_resource_wait_log_at = now
  waiting = pending.first(3).map do |_test_i, test, _scripts|
    "#{test.path} (#{test.resources.summary})"
  end.join(', ')

  log("Waiting for resources: pool: #{resource_pool.status}; pending: #{waiting}")
end

#log_statusObject (protected)



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/test-runner/executor.rb', line 401

def log_status
  snapshot = status_snapshot

  log(
    "Status: #{snapshot.fetch(:status)}; " \
    "#{snapshot.fetch(:expected_successful)} succeeded as expected, " \
    "#{snapshot.fetch(:expected_failed)} failed as expected, " \
    "#{snapshot.fetch(:unexpected_failed)} unexpectedly failed, " \
    "#{snapshot.fetch(:unexpected_successful)} unexpectedly succeeded; " \
    "#{snapshot.fetch(:running)} running, #{snapshot.fetch(:remaining)} remaining"
  )

  unless snapshot.fetch(:unexpected_failed_paths).empty?
    log("Unexpectedly failed test scripts: #{snapshot.fetch(:unexpected_failed_paths).join(', ')}")
  end

  return if snapshot.fetch(:unexpected_successful_paths).empty?

  log("Unexpectedly successful test scripts: #{snapshot.fetch(:unexpected_successful_paths).join(', ')}")
end

#mark_test_finished(test, result) ⇒ Object (protected)



365
366
367
368
369
370
# File 'lib/test-runner/executor.rb', line 365

def mark_test_finished(test, result)
  mutex.synchronize do
    running_tests.delete(test.path)
    results << result unless result.nil?
  end
end

#mark_test_running(i, test) ⇒ Object (protected)



355
356
357
358
359
360
361
362
363
# File 'lib/test-runner/executor.rb', line 355

def mark_test_running(i, test)
  mutex.synchronize do
    running_tests[test.path] = {
      index: i,
      test:,
      started_at: Time.now
    }
  end
end

#parse_resource_refresh_interval(value) ⇒ Object (protected)

Raises:

  • (ArgumentError)


329
330
331
332
333
334
335
336
# File 'lib/test-runner/executor.rb', line 329

def parse_resource_refresh_interval(value)
  value = DEFAULT_RESOURCE_REFRESH_INTERVAL if value.nil? || value.to_s == ''

  ret = Float(value)
  raise ArgumentError, 'resource refresh interval must be positive' if ret <= 0

  ret
end

#parse_status_interval(value) ⇒ Object (protected)

Raises:

  • (ArgumentError)


338
339
340
341
342
343
344
345
# File 'lib/test-runner/executor.rb', line 338

def parse_status_interval(value)
  value = DEFAULT_STATUS_INTERVAL if value.nil? || value.to_s == ''

  ret = Float(value)
  raise ArgumentError, 'status interval must be non-negative' if ret < 0

  ret
end

#refresh_resource_capacityObject (protected)



245
246
247
248
249
# File 'lib/test-runner/executor.rb', line 245

def refresh_resource_capacity
  scheduler_mutex.synchronize do
    refresh_resource_capacity_locked
  end
end

#refresh_resource_capacity_lockedObject (protected)



251
252
253
254
255
256
257
258
259
# File 'lib/test-runner/executor.rb', line 251

def refresh_resource_capacity_locked
  previous_status = resource_pool.status
  return unless resource_pool.refresh_capacity

  current_status = resource_pool.status
  scheduler_cv.broadcast

  log("Resource limits updated: #{current_status}") if previous_status != current_status
end

#release_test_resources(resources) ⇒ Object (protected)



238
239
240
241
242
243
# File 'lib/test-runner/executor.rb', line 238

def release_test_resources(resources)
  scheduler_mutex.synchronize do
    resource_pool.release(resources)
    scheduler_cv.broadcast
  end
end

#reserve_next_testObject (protected)



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/test-runner/executor.rb', line 190

def reserve_next_test
  scheduler_mutex.synchronize do
    loop do
      return nil if stop_work?
      return nil if pending.empty?

      i = schedulable_test_index

      if i
        item = pending.delete_at(i)
        _test_i, test, = item
        resources = test.resources

        resource_pool.reserve(resources)
        log_reserved_resources(test, resources)

        return [*item, resources]
      end

      log_resource_wait
      scheduler_cv.wait(scheduler_mutex)
    end
  end
end

#runArray<TestResult>

Returns:



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/test-runner/executor.rb', line 62

def run
  log(
    "Running #{test_scripts.length} scripts of #{@test_count} tests, " \
    "at most #{opts[:jobs]} tests at a time#{opts[:jobs_auto] ? ' (auto)' : ''}"
  )
  log("Resource detection: #{resource_pool.capacity_status}")
  log("Resource limits: #{resource_pool.status}")
  log("State directory is #{state_dir}")
  t1 = Time.now

  begin
    start_resource_monitor
    start_status_monitor

    opts[:jobs].times do |i|
      start_worker(i)
    end

    wait_for_workers
  ensure
    stop_status_monitor
    stop_resource_monitor
  end

  log("Run #{results.inject(0) { |acc, r| acc + r.script_results.length }} test scripts of #{@test_count} tests in #{(Time.now - t1).round(2)} seconds")

  result_groups = classify_results(results)
  expected_successful = result_groups.fetch(:expected_successful)
  expected_failed = result_groups.fetch(:expected_failed)
  unexpected_failed = result_groups.fetch(:unexpected_failed)
  unexpected_successful = result_groups.fetch(:unexpected_successful)
  unexpected_failed_paths = unexpected_script_paths(results, successful: false)
  unexpected_successful_paths = unexpected_script_paths(results, successful: true)

  if expected_successful.any?
    log("#{expected_successful.length} tests successful")
  end

  if expected_failed.any?
    log("#{expected_failed.length} tests failed as expected")
  end

  if unexpected_failed.any?
    log("#{unexpected_failed.length} tests should have succeeded, but failed")
  end

  if unexpected_successful.any?
    log("#{unexpected_successful.length} tests should have failed, but succeeded")
  end

  if unexpected_failed_paths.any?
    log('Unexpectedly failed test scripts:')

    unexpected_failed_paths.each { |path| log("  #{path}") }

    puts
  end

  if unexpected_successful_paths.any?
    log('Unexpectedly successful test scripts:')

    unexpected_successful_paths.each { |path| log("  #{path}") }

    puts
  end

  results
end

#run_resource_monitorObject (protected)



314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/test-runner/executor.rb', line 314

def run_resource_monitor
  loop do
    resource_monitor_mutex.synchronize do
      return if @resource_monitor_stop

      resource_monitor_cv.wait(resource_monitor_mutex, resource_refresh_interval)
      return if @resource_monitor_stop
    end

    refresh_resource_capacity
  rescue StandardError => e
    log("Resource monitor failed: #{e.class}: #{e.message}")
  end
end

#run_status_monitorObject (protected)



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/test-runner/executor.rb', line 299

def run_status_monitor
  loop do
    status_monitor_mutex.synchronize do
      return if @status_monitor_stop

      status_monitor_cv.wait(status_monitor_mutex, status_interval)
      return if @status_monitor_stop
    end

    log_status
  rescue StandardError => e
    log("Status monitor failed: #{e.class}: #{e.message}")
  end
end

#run_test(test, scripts, prefix:) ⇒ Object (protected)



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/test-runner/executor.rb', line 536

def run_test(test, scripts, prefix:)
  t1 = Time.now
  dir = test_state_dir(test)
  r, w = IO.pipe

  # 4 ports for use with boot.qemu.networks.[i].socket.mcast.port
  mcast_ports = OsVm::PortReservation.get_ports(key: "test:#{test.path}", size: 4)

  pid = Process.fork do
    r.close
    FileUtils.mkdir_p(dir)

    out = File.open(File.join(dir, 'test-runner.log'), 'w')
    $stdout.reopen(out)
    $stderr.reopen(out)
    $stdin.close

    OsVm::PortReservation.reset_to_ports(mcast_ports)

    ev = TestRunner::TestEvaluator.new(
      test,
      scripts,
      system: opts[:system],
      test_config_path: opts[:test_config_path],
      repo_root: opts[:repo_root],
      state_dir: dir,
      sock_dir: test_sock_dir,
      default_timeout: opts[:default_timeout],
      destructive: opts[:destructive],
      recreate_disks: opts[:recreate_disks]
    )

    ev.run do |result_hash|
      w.puts(result_hash.to_json)
    end
  end

  w.close

  script_results = []
  test_runner_log = File.join(dir, 'test-runner.log')
  heartbeat_interval = TEST_HEARTBEAT_INTERVAL
  next_heartbeat_at = Time.now + heartbeat_interval

  begin
    loop do
      ready =
        if verbose?
          timeout = [next_heartbeat_at - Time.now, 0].max
          r.wait_readable(timeout)
        else
          r.wait_readable
        end

      if ready.nil?
        elapsed = (Time.now - t1).round(2)
        msg = "#{prefix} Test '#{test.path}' still running after #{elapsed} seconds, log: #{test_runner_log}"
        last_line = last_nonempty_line(test_runner_log)
        msg += ", last output: #{last_line}" if last_line
        log(msg)
        next_heartbeat_at = Time.now + heartbeat_interval
        next
      end

      line = r.gets
      break if line.nil?

      next_heartbeat_at = Time.now + heartbeat_interval if verbose?

      begin
        result_hash = JSON.parse(line)
      rescue JSON::ParserError
        warn "Unable to parse test script result json: #{line.inspect}"
        next
      end

      case result_hash['type']
      when 'script'
        test_script = test.test_scripts[result_hash['script']]
        script_result = TestScriptResult.from_h(test_script, result_hash)
        script_results << script_result

        next if test_script.singleton?

        secs = script_result.elapsed_time.round(2)

        if script_result.expected_result?
          if script_result.successful?
            log("#{prefix} Script '#{test_script.path}' successful in #{secs} seconds")
          else
            log("#{prefix} Script '#{test_script.path}' failed as expected in #{secs} seconds")
          end
        else # unexpected result
          if script_result.successful?
            log("#{prefix} Script '#{test_script.path}' unexpectedly succeeded in #{secs} seconds")
          else
            log("#{prefix} Script '#{test_script.path}' failed after #{secs} seconds")
          end

          stop_work! if opts[:stop_on_failure]
        end
      when 'example'
        status =
          if result_hash['success']
            if result_hash['pending']
              'pending'
            elsif result_hash['skip']
              'skipped'
            else
              'succeeded'
            end
          elsif result_hash['pending']
            'unexpectedly succeeded'
          else
            'failed'
          end

        log("#{prefix} Example [#{result_hash['progress']}/#{result_hash['total']}] '#{result_hash['example']}' #{status} in #{result_hash['elapsed_time'].round(2)} seconds")
      end
    end
  rescue EOFError
    # pass
  end

  Process.wait(pid)

  OsVm::PortReservation.release_ports(key: "test:#{test.path}")

  # Complement script results if some are missing
  scripts.each do |script|
    script_result = script_results.detect { |sr| sr.test_script == script }
    next if script_result

    script_results << TestScriptResult.new(script, false, -1)
  end

  result = TestResult.new(
    test,
    script_results,
    $?.exitstatus == 0,
    Time.now - t1,
    dir
  )

  File.open(File.join(dir, 'test-result.txt'), 'w') do |f|
    str =
      if result.expected_result?
        if result.successful?
          'expected_success'
        else
          'expected_failure'
        end
      elsif result.successful?
        'unexpected_success'
      else
        'unexpected_failure'
      end

    f.puts(str)
  end

  result
end

#run_test_attempt(i, test, scripts, attempt) ⇒ Object (protected)



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
# File 'lib/test-runner/executor.rb', line 488

def run_test_attempt(i, test, scripts, attempt)
  prefix = "[#{i + 1}/#{@test_count}]"
  script_list = scripts.map { |v| "##{v.name}" }.join(', ')
  max_attempts = scripts.map(&:attempts).max

  if attempt > 0
    log("#{prefix} Retrying test '#{test.path}' (#{script_list}) (attempt #{attempt + 1}/#{max_attempts})")
  else
    log("#{prefix} Running test '#{test.path}' (#{script_list})")
  end

  result = run_test(test, scripts, prefix:)

  secs = result.elapsed_time.round(2)

  if result.expected_result?
    if result.successful?
      log("#{prefix} Test '#{test.path}' successful in #{secs} seconds")
    else
      log("#{prefix} Test '#{test.path}' failed as expected in #{secs} seconds")
    end
  else # unexpected result
    if result.successful?
      log("#{prefix} Test '#{test.path}' unexpectedly succeeded in #{secs} seconds, see #{result.state_dir}")
    else
      log("#{prefix} Test '#{test.path}' failed after #{secs} seconds, see #{result.state_dir}")
    end

    stop_work! if opts[:stop_on_failure]
  end

  result
end

#run_test_with_retries(i, test, scripts) ⇒ Object (protected)



450
451
452
453
454
455
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
# File 'lib/test-runner/executor.rb', line 450

def run_test_with_retries(i, test, scripts)
  latest_script_results = {}
  remaining_scripts = scripts
  elapsed_time = 0
  last_result = nil
  attempt = 0

  loop do
    last_result = run_test_attempt(i, test, remaining_scripts, attempt)
    elapsed_time += last_result.elapsed_time

    last_result.script_results.each do |script_result|
      latest_script_results[script_result.test_script] = script_result
    end

    break if stop_work?

    remaining_scripts = remaining_scripts.select do |script|
      script_result = latest_script_results.fetch(script)

      script_result.unexpected_result? && attempt + 1 < script.attempts
    end

    break if remaining_scripts.empty?

    sleep(5)
    attempt += 1
  end

  build_accumulated_test_result(
    test,
    scripts,
    latest_script_results,
    elapsed_time,
    last_result
  )
end

#run_worker(_w_i) ⇒ Object (protected)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/test-runner/executor.rb', line 170

def run_worker(_w_i)
  loop do
    return if stop_work?

    reserved_test = reserve_next_test
    return if reserved_test.nil?

    i, test, scripts, resources = reserved_test
    result = nil

    begin
      mark_test_running(i, test)
      result = run_test_with_retries(i, test, scripts)
    ensure
      release_test_resources(resources)
      mark_test_finished(test, result)
    end
  end
end

#schedulable_test_indexObject (protected)



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/test-runner/executor.rb', line 215

def schedulable_test_index
  i = pending.index do |_test_i, test, _scripts|
    resource_pool.can_reserve?(test.resources)
  end

  return i unless i.nil?

  # Never deadlock the suite just because one test is larger than the
  # detected capacity. Run it alone and let QEMU or the host enforce the
  # real limit.
  if resource_pool.running == 0
    _test_i, test, = pending.first
    log(
      "WARNING: Test #{test.path} requests resources beyond the scheduler " \
      "limits (requested: #{test.resources.summary}; available: #{resource_pool.status}); " \
      'running it alone may exhaust the host'
    )
    return 0
  end

  nil
end

#start_resource_monitorObject (protected)



261
262
263
264
# File 'lib/test-runner/executor.rb', line 261

def start_resource_monitor
  @resource_monitor_stop = false
  @resource_monitor = Thread.new { run_resource_monitor }
end

#start_status_monitorObject (protected)



279
280
281
282
283
284
# File 'lib/test-runner/executor.rb', line 279

def start_status_monitor
  return unless status_monitor_enabled?

  @status_monitor_stop = false
  @status_monitor = Thread.new { run_status_monitor }
end

#start_worker(i) ⇒ Object (protected)



162
163
164
# File 'lib/test-runner/executor.rb', line 162

def start_worker(i)
  workers << Thread.new { run_worker(i) }
end

#state_dirObject (protected)



717
718
719
# File 'lib/test-runner/executor.rb', line 717

def state_dir
  opts[:state_dir]
end

#status_monitor_enabled?Boolean (protected)

Returns:

  • (Boolean)


347
348
349
# File 'lib/test-runner/executor.rb', line 347

def status_monitor_enabled?
  status_interval > 0
end

#status_snapshotObject (protected)



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/test-runner/executor.rb', line 381

def status_snapshot
  pending_count = scheduler_mutex.synchronize { pending.length }
  finished_results, running_count = mutex.synchronize { [results.dup, running_tests.length] }
  result_groups = classify_results(finished_results)
  unexpected_failed = result_groups.fetch(:unexpected_failed).length
  unexpected_successful = result_groups.fetch(:unexpected_successful).length

  {
    status: unexpected_failed > 0 || unexpected_successful > 0 ? 'failed' : 'passing',
    expected_successful: result_groups.fetch(:expected_successful).length,
    expected_failed: result_groups.fetch(:expected_failed).length,
    unexpected_failed:,
    unexpected_successful:,
    unexpected_failed_paths: unexpected_script_paths(finished_results, successful: false),
    unexpected_successful_paths: unexpected_script_paths(finished_results, successful: true),
    running: running_count,
    remaining: pending_count
  }
end

#stop_resource_monitorObject (protected)



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/test-runner/executor.rb', line 266

def stop_resource_monitor
  thread = @resource_monitor
  return if thread.nil?

  resource_monitor_mutex.synchronize do
    @resource_monitor_stop = true
    resource_monitor_cv.signal
  end

  thread.join
  @resource_monitor = nil
end

#stop_status_monitorObject (protected)



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/test-runner/executor.rb', line 286

def stop_status_monitor
  thread = @status_monitor
  return if thread.nil?

  status_monitor_mutex.synchronize do
    @status_monitor_stop = true
    status_monitor_cv.signal
  end

  thread.join
  @status_monitor = nil
end

#stop_work!Object (protected)



700
701
702
703
# File 'lib/test-runner/executor.rb', line 700

def stop_work!
  @stop_work = true
  scheduler_mutex.synchronize { scheduler_cv.broadcast }
end

#stop_work?Boolean (protected)

Returns:

  • (Boolean)


705
706
707
# File 'lib/test-runner/executor.rb', line 705

def stop_work?
  @stop_work
end

#test_sock_dirObject (protected)



713
714
715
# File 'lib/test-runner/executor.rb', line 713

def test_sock_dir
  File.join(state_dir, 'socks')
end

#test_state_dir(test) ⇒ Object (protected)



709
710
711
# File 'lib/test-runner/executor.rb', line 709

def test_state_dir(test)
  File.join(state_dir, "os-test-#{test_state_key(test)}")
end

#test_state_key(test) ⇒ Object (protected)



721
722
723
724
# File 'lib/test-runner/executor.rb', line 721

def test_state_key(test)
  slug = test.path.gsub(/[^A-Za-z0-9_.-]+/, '__')
  "#{slug}-#{Digest::SHA256.hexdigest(test.path)[0, 8]}"
end

#unexpected_script_paths(test_results, successful:) ⇒ Object (protected)



422
423
424
425
426
427
428
429
# File 'lib/test-runner/executor.rb', line 422

def unexpected_script_paths(test_results, successful:)
  test_results.flat_map(&:script_results)
              .select(&:unexpected_result?)
              .select { |script_result| successful ? script_result.successful? : script_result.failed? }
              .map { |script_result| script_result.test_script.path }
              .uniq
              .sort
end

#verbose?Boolean (protected)

Returns:

  • (Boolean)


351
352
353
# File 'lib/test-runner/executor.rb', line 351

def verbose?
  opts[:verbose]
end

#wait_for_workersObject (protected)



166
167
168
# File 'lib/test-runner/executor.rb', line 166

def wait_for_workers
  workers.each(&:join)
end