Class: RSpec::Rewind::RetryLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rewind/retry_loop.rb

Instance Method Summary collapse

Constructor Details

#initialize(example:, context:, retry_count_resolver:, attempt_runner:, retry_gate:, retry_transition:, flaky_transition:, clock: nil) ⇒ RetryLoop

Returns a new instance of RetryLoop.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec/rewind/retry_loop.rb', line 6

def initialize(
  example:,
  context:,
  retry_count_resolver:,
  attempt_runner:,
  retry_gate:,
  retry_transition:,
  flaky_transition:,
  clock: nil
)
  @example = example
  @context = context
  @retry_count_resolver = retry_count_resolver
  @attempt_runner = attempt_runner
  @retry_gate = retry_gate
  @retry_transition = retry_transition
  @flaky_transition = flaky_transition
  @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
end

Instance Method Details

#run(retries:, backoff:, wait:, retry_on:, skip_retry_on:, retry_if:) ⇒ Object



26
27
28
29
30
31
32
33
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
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
109
110
# File 'lib/rspec/rewind/retry_loop.rb', line 26

def run(retries:, backoff:, wait:, retry_on:, skip_retry_on:, retry_if:)
  resolved_retries = @retry_count_resolver.resolve(explicit_retries: retries)
  return @example.run if resolved_retries <= 0

  total_attempts = resolved_retries + 1
  attempt = 1
  first_exception = nil
  first_failure_duration = nil
  attempt_durations = []
  sleep_total = 0.0
  started_at = monotonic_time

  while attempt <= total_attempts
    exception, duration, raised = @attempt_runner.run(
      run_target: @example,
      exception_source: @context.source
    )
    attempt_durations << duration

    if exception.nil?
      if attempt > 1
        @flaky_transition.perform(
          attempt: attempt,
          retries: resolved_retries,
          duration: duration,
          exception: first_exception,
          total_duration: monotonic_time - started_at,
          attempt_durations: attempt_durations.dup,
          first_failure_duration: first_failure_duration,
          sleep_total: sleep_total
        )
      end
      return
    end

    first_exception ||= exception
    first_failure_duration ||= duration
    retry_number = attempt
    decision = @retry_gate.decision(
      exception: exception,
      retry_number: retry_number,
      resolved_retries: resolved_retries,
      retry_on: retry_on,
      skip_retry_on: skip_retry_on,
      retry_if: retry_if,
      example_id: @context.id,
      elapsed_time: monotonic_time - started_at,
      sleep_total: sleep_total
    )
    unless decision.allowed?
      @retry_transition.publish_not_retried(
        retry_number: retry_number,
        resolved_retries: resolved_retries,
        duration: duration,
        exception: exception,
        decision: decision,
        total_duration: monotonic_time - started_at,
        attempt_durations: attempt_durations.dup,
        sleep_total: sleep_total
      )
      raise exception if raised

      return
    end

    sleep_measurement = @retry_transition.perform(
      retry_number: retry_number,
      resolved_retries: resolved_retries,
      duration: duration,
      exception: exception,
      backoff: backoff,
      wait: wait,
      example_source: @context.source,
      total_duration: monotonic_time - started_at,
      attempt_durations: attempt_durations.dup,
      sleep_total: sleep_total,
      failure_fingerprint: FailureFingerprint.build(exception),
      budget_decision: decision.budget_decision,
      policy_decision: decision.policy_decision
    )
    sleep_total += sleep_measurement.actual

    attempt += 1
  end
end