Class: TestRunner::TestEvaluator

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/test-runner/test_evaluator.rb

Defined Under Namespace

Classes: ScriptState

Constant Summary collapse

SCRIPT_STATE_KEY =
:test_runner_script_state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, scripts, system: NixCli::DEFAULT_SYSTEM, test_config_path: nil, repo_root: nil, prepared_config: nil, **opts) ⇒ TestEvaluator

Returns a new instance of TestEvaluator.

Parameters:

Options Hash (**opts):

  • :default_timeout (Integer)
  • :destructive (Boolean)
  • :recreate_disks (Boolean)
  • :state_dir (String)
  • :sock_dir (String)


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

def initialize(test, scripts, system: NixCli::DEFAULT_SYSTEM, test_config_path: nil, repo_root: nil, prepared_config: nil, **opts)
  scripts.each do |s|
    next if s.test == test

    raise ArgumentError, "script #{s.name} is not of test #{test.path}"
  end

  @test = test
  @scripts = scripts
  @opts = opts
  @config = prepared_config || TestConfig.build(
    test, system:, test_config_path:, repo_root:,
          config_path: File.join(@opts.fetch(:state_dir), 'config.json')
  )
  if prepared_config
    raise ArgumentError, 'Prepared configuration belongs to another test' unless prepared_config.test == test

    prepared_config.validate!(scripts)
    FileUtils.mkdir_p(@opts.fetch(:state_dir))
    FileUtils.ln_sf(prepared_config.config_path, File.join(@opts.fetch(:state_dir), 'config.json'))
  end
  @machines = {}
  @default_timeout = opts.fetch(:default_timeout)
  @used_container_ids = []
  @used_container_ids_mutex = Mutex.new
  @log_mutex = Mutex.new

  @config['machines'].each do |name, cfg|
    var = :"@#{name}"

    machine_config = OsVm::MachineConfig.from_config(cfg)

    m = machine_class_for(machine_config).new(
      name,
      machine_config,
      opts[:state_dir],
      opts[:sock_dir],
      default_timeout: opts[:default_timeout],
      hash_base: test.path
    )

    m.destroy_disks if opts[:recreate_disks]

    instance_variable_set(var, m)

    define_singleton_method(name) do
      instance_variable_get(var)
    end

    machines[name] = m
  end
end

Instance Attribute Details

#machines ⇒ Hash<String, OsVm::Machine> (readonly)

Returns:

  • (Hash<String, OsVm::Machine>)


30
31
32
# File 'lib/test-runner/test_evaluator.rb', line 30

def machines
  @machines
end

Instance Method Details

#after(type, &block) ⇒ Object

Code block executed after suite, context or example

Parameters:

  • type (:suite, :context, :example)


204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/test-runner/test_evaluator.rb', line 204

def after(type, &block)
  state = current_script_state

  if type == :suite
    state.after << block
    return
  end

  raise 'Called outside of an example group, use from #describe block' if state.group_stack.empty?

  state.group_stack.last.add_after(type, block)
end

#before(type, &block) ⇒ Object

Code block executed before suite, context or example

Parameters:

  • type (:suite, :context, :example)


189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/test-runner/test_evaluator.rb', line 189

def before(type, &block)
  state = current_script_state

  if type == :suite
    state.before << block
    return
  end

  raise 'Called outside of an example group, use from #describe block' if state.group_stack.empty?

  state.group_stack.last.add_before(type, block)
end

#breakpoint ⇒ Object

Invoke interactive shell from within a test



148
149
150
# File 'lib/test-runner/test_evaluator.rb', line 148

def breakpoint
  binding.pry # rubocop:disable Lint/Debugger
end

#build_test_result(script_results, elapsed_time) ⇒ Object (protected)



577
578
579
580
581
582
583
584
585
586
587
# File 'lib/test-runner/test_evaluator.rb', line 577

def build_test_result(script_results, elapsed_time)
  test_success = script_results.all?(&:expected_result?)

  TestResult.new(
    @test,
    script_results,
    test_success,
    elapsed_time,
    @opts[:state_dir]
  )
end

#call_after_test_run_hook(test_result) ⇒ Object (protected)



551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/test-runner/test_evaluator.rb', line 551

def call_after_test_run_hook(test_result)
  Hook.call(
    :after_test_run,
    kwargs: {
      test: @test,
      test_result:,
      scripts: @scripts,
      machines:,
      state_dir: @opts[:state_dir]
    }
  )
end

#call_after_test_script_hook(script_result) ⇒ Object (protected)



564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/test-runner/test_evaluator.rb', line 564

def call_after_test_script_hook(script_result)
  Hook.call(
    :after_test_script_run,
    kwargs: {
      test: @test,
      script: script_result.test_script,
      script_result:,
      machines:,
      state_dir: @opts[:state_dir]
    }
  )
end

#configure_examples {|| ... } ⇒ Object

Configure default settings for example groups

Yield Parameters:



154
155
156
# File 'lib/test-runner/test_evaluator.rb', line 154

def configure_examples
  yield(current_script_state.example_config)
end

#container_apk(machine, container, operation, *arguments, name:, global_options: [], environment: {}, timeout: @default_timeout) ⇒ Array(Integer, String)

Run apk in a vpsAdminOS container with bounded transient retries

Parameters:

  • machine (OsVm::Machine) —

    vpsAdminOS host

  • container (String, Integer) —

    container id

  • operation (String) —

    apk operation

  • arguments (Array<String>) —

    operation arguments

  • name (String) —

    operation name for retry reporting

  • global_options (Array<String>) (defaults to: []) —

    apk options placed before OPERATION

  • environment (Hash<String, String>) (defaults to: {}) —

    command environment

  • timeout (Integer) (defaults to: @default_timeout) —

    command timeout

Returns:

  • (Array(Integer, String)) —

    command status and output

Raises:

  • (ArgumentError)


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/test-runner/test_evaluator.rb', line 413

def container_apk(
  machine,
  container,
  operation,
  *arguments,
  name:,
  global_options: [],
  environment: {},
  timeout: @default_timeout
)
  raise ArgumentError, 'apk operation cannot be empty' if operation.to_s.empty?

  command = ['osctl', 'ct', 'exec', container.to_s]
  command.push('env', *environment.map { |key, value| "#{key}=#{value}" }) unless environment.empty?
  command.push(
    'apk',
    '--no-interactive',
    '--timeout',
    '60',
    *global_options.map(&:to_s),
    operation.to_s,
    *arguments.map(&:to_s)
  )

  retry_operation(
    name:,
    attempts: 3,
    delay: ->(attempt, _) { attempt * 30 },
    retry_if: RetryClassifier.method(:apk)
  ) do
    machine.succeeds(Shellwords.join(command), timeout:)
  end
end

#container_apt_get(machine, container, *arguments, name:, environment: {}, timeout: @default_timeout) ⇒ Array(Integer, String)

Run apt-get in a vpsAdminOS container with bounded transient retries

Parameters:

  • machine (OsVm::Machine) —

    vpsAdminOS host

  • container (String, Integer) —

    container id

  • arguments (Array<String>) —

    apt-get arguments

  • name (String) —

    operation name for retry reporting

  • environment (Hash<String, String>) (defaults to: {}) —

    command environment

  • timeout (Integer) (defaults to: @default_timeout) —

    command timeout

Returns:

  • (Array(Integer, String)) —

    command status and output

Raises:

  • (ArgumentError)


388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/test-runner/test_evaluator.rb', line 388

def container_apt_get(machine, container, *arguments, name:, environment: {}, timeout: @default_timeout)
  raise ArgumentError, 'apt-get arguments cannot be empty' if arguments.empty?

  command = ['osctl', 'ct', 'exec', container.to_s]
  command.push('env', *environment.map { |key, value| "#{key}=#{value}" }) unless environment.empty?
  # Integration tests need package indexes, not translated descriptions.
  # Avoid optional Translation indexes racing an otherwise usable mirror.
  command.push('apt-get', '-o', 'Acquire::Retries=3', '-o', 'Acquire::Languages=none', *arguments.map(&:to_s))

  retry_apt_operation(name:) do
    machine.succeeds(Shellwords.join(command), timeout:)
  end
end

#current_script_state ⇒ Object (protected)



752
753
754
# File 'lib/test-runner/test_evaluator.rb', line 752

def current_script_state
  Thread.current[SCRIPT_STATE_KEY] || (raise 'No test script is running')
end

#describe(obj, order: nil) ⇒ Object Also known as: context

Create an example group

Example groups can be nested. Groups are evaluated in the configured order, which defaults to random. Groups contain examples which are defined within the yielded block using #it.

Parameters:

  • obj (#to_s)
  • order (nil, :defined, :rand, Random, Integer) (defaults to: nil) —

    order in which examples and subgroups are evaluated



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/test-runner/test_evaluator.rb', line 167

def describe(obj, order: nil, &)
  state = current_script_state
  grp = ExampleGroup.new(obj, parent: state.group_stack.last, order:, config: state.example_config, &)

  if state.group_stack.any?
    state.group_stack.last.add_group(grp)
  else
    state.example_groups << grp
  end

  state.group_stack << grp

  grp.load

  state.group_stack = state.group_stack[0..-2]
  nil
end

#do_run ⇒ Object (protected)



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/test-runner/test_evaluator.rb', line 764

def do_run
  yield

  machines.each_value do |m|
    m.stop if !m.kernel_failed? && m.running? && m.can_execute?
  end
ensure
  begin
    machines.each_value do |m|
      if m.kernel_failed?
        m.kill_after_kernel_failure
      else
        m.kill
      end
      m.destroy if @opts[:destructive]
      m.finalize
      m.cleanup
    end
  ensure
    raise_if_kernel_failed!
  end
end

#example(message, pending: false, skip: false, &block) ⇒ Object Also known as: it

Create a test example

#it must be called from within a #describe block. Examples within a group are evaluated in the group's configured order.

Parameters:

  • message (String)
  • pending (Boolean) (defaults to: false)
  • skip (Boolean) (defaults to: false)


225
226
227
228
229
230
231
232
# File 'lib/test-runner/test_evaluator.rb', line 225

def example(message, pending: false, skip: false, &block)
  state = current_script_state
  raise 'Called outside of an example group, use from #describe block' if state.group_stack.empty?

  grp = state.group_stack.last
  grp.add_example(Example.new(grp, message, pending:, skip: skip || block.nil?, &block))
  nil
end

#get_container_id(base = 'testct') ⇒ String

Generate container id that is unique to the test run

Returns:

  • (String)


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/test-runner/test_evaluator.rb', line 264

def get_container_id(base = 'testct')
  @used_container_ids_mutex.synchronize do
    10.times do
      new_id = "#{base}-#{SecureRandom.hex(2)}"

      if @used_container_ids.include?(new_id)
        sleep(0.05)
        next
      end

      @used_container_ids << new_id
      return new_id
    end
  end

  raise 'Unable to generate unique container id'
end

#get_example_count(groups: nil) ⇒ Object (protected)



735
736
737
738
739
740
741
742
743
744
# File 'lib/test-runner/test_evaluator.rb', line 735

def get_example_count(groups: nil)
  cnt = 0

  (groups || current_script_state.example_groups).each do |grp|
    cnt += grp.examples.count(&:evaluate?)
    cnt += get_example_count(groups: grp.groups)
  end

  cnt
end

#interactive ⇒ Object

Run interactive shell



135
136
137
138
139
# File 'lib/test-runner/test_evaluator.rb', line 135

def interactive
  do_run do
    binding.pry # rubocop:disable Lint/Debugger
  end
end

#kernel_failed? ⇒ Boolean (protected)

Returns:

  • (Boolean)


787
788
789
# File 'lib/test-runner/test_evaluator.rb', line 787

def kernel_failed?
  machines.each_value.any?(&:kernel_failed?)
end

#log(msg) ⇒ Object (protected)



746
747
748
749
750
# File 'lib/test-runner/test_evaluator.rb', line 746

def log(msg)
  @log_mutex.synchronize do
    warn "[#{Time.now}] #{msg}"
  end
end

#machine_class_for(config) ⇒ Object (protected)



795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/test-runner/test_evaluator.rb', line 795

def machine_class_for(config)
  klass = Hook.call(:machine_class_for, args: [config])
  return klass if klass

  case config.spin
  when 'vpsadminos'
    OsVm::VpsadminosMachine
  when 'nixos'
    OsVm::NixosMachine
  else
    raise ArgumentError, "Unknown machine spin #{config.spin.inspect}"
  end
end

#pending(message = '', skip: false, &block) ⇒ Object

Create a pending example or mark the currently evaluated example as pending

Parameters:

  • message (String) (defaults to: '')
  • skip (Boolean) (defaults to: false)


239
240
241
242
243
244
245
246
247
# File 'lib/test-runner/test_evaluator.rb', line 239

def pending(message = '', skip: false, &block)
  state = current_script_state

  if block || state.current_example.nil?
    it(message, pending: true, skip:, &block)
  else
    state.current_example.send(:set_pending)
  end
end

#raise_if_kernel_failed! ⇒ Object (protected)



791
792
793
# File 'lib/test-runner/test_evaluator.rb', line 791

def raise_if_kernel_failed!
  machines.each_value(&:raise_if_kernel_failed!)
end

#retry_apt_operation(name:) ⇒ any

Retry an APT operation using the framework's classified retry policy

This is used when test-runner cannot construct the apt-get command, such as when executing a byte-stable documentation fixture.

Parameters:

  • name (String) —

    operation name for retry reporting

Yield Returns:

  • (any)

Returns:

  • (any) —

    yielded value



455
456
457
458
459
460
461
462
463
# File 'lib/test-runner/test_evaluator.rb', line 455

def retry_apt_operation(name:, &)
  retry_operation(
    name:,
    attempts: 3,
    delay: ->(attempt, _) { attempt * 30 },
    retry_if: RetryClassifier.method(:apt),
    &
  )
end

#retry_operation(name:, attempts:, retry_if:, delay: 1) {|attempt| ... } ⇒ any

Retry a failed operation a bounded number of times

RETRY_IF receives the exception and must return false/nil for a terminal failure, true to retry with the exception summary as the reason, or a string describing the classified transient failure. DELAY may be a numeric value or a callable receiving the failed attempt number and exception.

Parameters:

  • name (String) —

    operation name for retry reporting

  • attempts (Integer) —

    maximum number of attempts, including the first

  • delay (Numeric, Proc) (defaults to: 1)
  • retry_if (Proc)

Yield Parameters:

  • attempt (Integer) —

    current attempt, starting at one

Returns:

  • (any) —

    yielded value

Raises:

  • (ArgumentError)


332
333
334
335
336
337
338
339
340
341
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
368
369
370
371
372
373
374
375
376
377
# File 'lib/test-runner/test_evaluator.rb', line 332

def retry_operation(name:, attempts:, retry_if:, delay: 1)
  unless attempts.is_a?(Integer) && attempts > 0
    raise ArgumentError, 'attempts must be a positive integer'
  end

  unless delay.respond_to?(:call) || (delay.is_a?(Numeric) && delay >= 0)
    raise ArgumentError, 'delay must be a non-negative number or callable'
  end

  raise ArgumentError, 'retry_if must be callable' unless retry_if.respond_to?(:call)

  attempts.times do |attempt|
    raise_if_kernel_failed!

    begin
      ret = yield(attempt + 1)
      raise_if_kernel_failed!
      return ret
    rescue StandardError => e
      raise_if_kernel_failed!
      raise if attempt + 1 >= attempts

      classification = retry_if.call(e)
      raise unless classification

      retry_delay = delay.respond_to?(:call) ? delay.call(attempt + 1, e) : delay

      unless retry_delay.is_a?(Numeric) && retry_delay >= 0
        raise ArgumentError, 'retry delay must be a non-negative number'
      end

      reason =
        if classification == true
          e.message.lines.first&.strip || e.class.to_s
        else
          classification.to_s
        end

      log(
        "Retrying #{name} after #{e.class}: #{reason}; " \
        "attempt #{attempt + 2}/#{attempts} in #{retry_delay}s"
      )
      sleep(retry_delay) if retry_delay > 0
    end
  end
end

#run {|one| ... } ⇒ Hash<String, TestScriptResult>

Run the test scripts

Yield Parameters:

Returns:



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
130
131
132
# File 'lib/test-runner/test_evaluator.rb', line 100

def run
  ret = {}
  script_results_by_name = {}
  test_started_at = Time.now
  event_mutex = Mutex.new
  result_mutex = Mutex.new

  do_run do
    run_script_workers do |script, worker_i|
      OsVm::Machine.with_shell(worker_i) do
        script_result = run_script(script) do |event|
          event_mutex.synchronize { yield(event) } if block_given?
        end

        call_after_test_script_hook(script_result)

        result_mutex.synchronize do
          ret[script.name] = script_result
          script_results_by_name[script.name] = script_result
        end

        event_mutex.synchronize { yield(script_result) } if block_given?
      end
    end
    raise_if_kernel_failed!

    script_results = @scripts.map { |script| script_results_by_name.fetch(script.name) }
    test_result = build_test_result(script_results, Time.now - test_started_at)
    call_after_test_run_hook(test_result)
  end

  ret
end

#run_examples ⇒ Object (protected)



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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/test-runner/test_evaluator.rb', line 668

def run_examples
  state = current_script_state
  example_count = get_example_count
  i = 1

  log 'Evaluating examples'

  raise_if_kernel_failed!
  state.before.each(&:call)
  raise_if_kernel_failed!

  results = ExampleOrdering.sort_by_order(state.example_groups, state.example_config.default_order).map do |grp|
    grp.evaluate do |type, example_or_result|
      if type == :before
        raise_if_kernel_failed!
        log "[#{i}/#{example_count}] Evaluating '#{example_or_result.full_message}'"
        state.current_example = example_or_result
      else
        result = example_or_result
        raise_if_kernel_failed!

        status =
          if result.success?
            if result.pending?
              'pending'
            elsif result.skip?
              'skipped'
            else
              'succeeded'
            end
          elsif result.pending?
            'unexpectedly succeeded'
          else
            'failed'
          end

        log "[#{i}/#{example_count}] '#{result.title}' #{status} in #{result.elapsed_time.round(2)}s"

        # No block is given in debug mode
        if block_given?
          yield({ type: :example, progress: i, total: example_count, result: result })
        end

        state.current_example = nil

        i += 1
      end
    end
  end.flatten

  state.after.each(&:call)
  raise_if_kernel_failed!

  failed = results.select(&:failure?)
  return if failed.empty?

  warn "\n#{failed.count} examples failed:"

  failed.each do |result|
    warn result.title
    warn result.error
    warn "\n\n"
  end

  raise 'One or more examples failed'
end

#run_script(script) ⇒ Object (protected)



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

def run_script(script)
  success = false
  t1 = Time.now

  begin
    log "Running script #{script.name}"

    script_context = clone
    script_context.send(:test_script, script.name) do |progress|
      next unless progress[:type] == :example

      example_result = progress[:result].to_h(
        script: script.name,
        progress: progress[:progress],
        total: progress[:total]
      )

      yield(example_result) if block_given?
    end
    raise_if_kernel_failed!

    t2 = Time.now
    log "Script #{script.name} finished in #{(t2 - t1).round(2)}s"
  rescue Exception => e # rubocop:disable Lint/RescueException
    t2 = Time.now
    log "Exception occurred while running script #{script.name} in #{(t2 - t1).round(2)}s"
    log e.full_message
  else
    success = true
  end

  TestScriptResult.new(script, success, t2 - t1)
end

#run_script_workers ⇒ Object (protected)



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

def run_script_workers
  worker_count = [@test.test_script_jobs, @scripts.length].min
  return if worker_count <= 0

  queue = Queue.new
  @scripts.each { |script| queue << script }

  workers = worker_count.times.map do |worker_i|
    Thread.new do
      loop do
        break if kernel_failed?

        script =
          begin
            queue.pop(true)
          rescue ThreadError
            break
          end

        yield(script, worker_i)
      end
    end
  end

  workers.each(&:join)
end

#skip(message = '', &block) ⇒ Object

Create a skipped example or mark the currently evaluated example as pending

Parameters:

  • message (String) (defaults to: '')


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

def skip(message = '', &block)
  state = current_script_state

  if block || state.current_example.nil?
    it(message, skip: true, &block)
  else
    state.current_example.send(:set_skip)
    throw :skip
  end
end

#start_all ⇒ Object

Start all machines



142
143
144
145
# File 'lib/test-runner/test_evaluator.rb', line 142

def start_all
  machines.each_value { |machine| machine.start(wait_for_boot: false) }
  machines.each_value(&:wait_for_boot)
end

#test_config ⇒ Object



93
94
95
# File 'lib/test-runner/test_evaluator.rb', line 93

def test_config
  @config.dig('framework', 'testConfig') || {}
end

#test_script(name) ⇒ Object (protected)



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/test-runner/test_evaluator.rb', line 650

def test_script(name, &)
  state = ScriptState.new(
    example_config: ExampleConfiguration.new,
    before: [],
    after: [],
    example_groups: [],
    group_stack: []
  )

  with_script_state(state) do
    binding.eval(@config['testScripts'][name]['script']) # rubocop:disable Security/Eval

    return if state.example_groups.empty?

    run_examples(&)
  end
end

#wait_for_block(name:, timeout: @default_timeout) ⇒ any

Wait for block to succeed

Yield until the code block returns a truthy value or a timeout is reached. RSpec expectation failures inside the block are treated as a falsey return value and retried until the timeout.

Parameters:

  • name (String) —

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any) —

    yielded value

Raises:

  • (OsVm::TimeoutError)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/test-runner/test_evaluator.rb', line 292

def wait_for_block(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    raise_if_kernel_failed!

    ret =
      begin
        yield
      rescue RSpec::Expectations::ExpectationNotMetError
        false
      end
    raise_if_kernel_failed!
    return ret if ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name}"
    end

    sleep(1)
  end
end

#wait_until_block_fails(name:, timeout: @default_timeout) ⇒ any

Wait for a block that eventually fails

Intended for polling code that is expected to start failing with OsVm::CommandFailed. OsVm::CommandSucceeded is treated as a retryable result, because the block may succeed before it starts failing. If no exception is raised, the method returns once the block yields a falsey value. RSpec expectations are not handled here because they cannot be reliably distinguished.

Parameters:

  • name (String) —

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any) —

    block return value or true when failure is signaled with OsVm::CommandFailed

Raises:

  • (OsVm::TimeoutError) —

    when the block does not fail in time



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/test-runner/test_evaluator.rb', line 520

def wait_until_block_fails(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    raise_if_kernel_failed!

    begin
      ret = yield
    rescue OsVm::CommandFailed
      return true
    rescue OsVm::CommandSucceeded
      ret = true
    end

    raise_if_kernel_failed!

    return ret unless ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name} to fail"
    end

    sleep(1)
  end
end

#wait_until_block_succeeds(name:, timeout: @default_timeout) ⇒ any

Wait for a block that eventually succeeds

Intended for polling code that can temporarily fail with OsVm::CommandFailed (treated as a retry) or signal success via OsVm::CommandSucceeded. RSpec expectation failures are also treated as retryable. The method returns as soon as the block yields a truthy value or raises OsVm::CommandSucceeded.

Parameters:

  • name (String) —

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any) —

    block return value or true when success is signaled with OsVm::CommandSucceeded

Raises:

  • (OsVm::TimeoutError) —

    when the block does not succeed in time



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

def wait_until_block_succeeds(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    raise_if_kernel_failed!

    ret =
      begin
        yield
      rescue OsVm::CommandFailed, RSpec::Expectations::ExpectationNotMetError
        false
      rescue OsVm::CommandSucceeded
        true
      end
    raise_if_kernel_failed!
    return ret if ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name} to succeed"
    end

    sleep(1)
  end
end

#with_script_state(state) ⇒ Object (protected)



756
757
758
759
760
761
762
# File 'lib/test-runner/test_evaluator.rb', line 756

def with_script_state(state)
  previous_state = Thread.current[SCRIPT_STATE_KEY]
  Thread.current[SCRIPT_STATE_KEY] = state
  yield
ensure
  Thread.current[SCRIPT_STATE_KEY] = previous_state
end