Skip to content

Commit

Permalink
unit tests for RetryNew strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 5, 2024
1 parent 2cfdeb6 commit ec3c099
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_retries/strategy/retry_new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def record_retry(test_span)
def record_duration(duration)
@max_attempts = @duration_thresholds.max_attempts_for_duration(duration)

Datadog.logger.debug { "Recorded test duration of [#{@duration}], new Max Attempts value is [#{@max_attempts}]" }
Datadog.logger.debug { "Recorded test duration of [#{duration}], new Max Attempts value is [#{@max_attempts}]" }
end
end
end
Expand Down
38 changes: 32 additions & 6 deletions spec/datadog/ci/test_retries/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
let(:retry_failed_tests_total_limit) { 12 }
let(:retry_new_tests_enabled) { true }
let(:retry_new_tests_percentage_limit) { 30 }
let(:retry_new_tests_max_attempts) { 5 }

let(:remote_flaky_test_retries_enabled) { false }
let(:remote_early_flake_detection_enabled) { false }
Expand All @@ -36,7 +37,7 @@
let(:slow_test_retries) do
instance_double(
Datadog::CI::Remote::SlowTestRetries,
max_attempts_for_duration: 10
max_attempts_for_duration: retry_new_tests_max_attempts
)
end

Expand Down Expand Up @@ -106,7 +107,7 @@
subject

expect(component.retry_new_tests_enabled).to be true
expect(component.retry_new_tests_duration_thresholds.max_attempts_for_duration(1.2)).to eq(10)
expect(component.retry_new_tests_duration_thresholds.max_attempts_for_duration(1.2)).to eq(retry_new_tests_max_attempts)
expect(component.retry_new_tests_percentage_limit).to eq(retry_new_tests_percentage_limit)
end

Expand Down Expand Up @@ -218,12 +219,18 @@
let(:flaky_test_retries_enabled) { true }
end

let(:test_failed) { false }
let(:component) do
Datadog.send(:components).test_retries
end

let(:tracer_span) do
instance_double(Datadog::Tracing::SpanOperation, duration: 1.2)
end
let(:test_span) do
instance_double(
Datadog::CI::Test,
failed?: test_failed,
passed?: false,
passed?: !test_failed,
set_tag: true,
get_tag: true,
skipped?: false,
Expand All @@ -232,14 +239,16 @@
test_suite_name: "mysuite"
)
end
let(:test_failed) { false }

subject(:runs_count) do
runs_count = 0
component.with_retries do
runs_count += 1

# run callback manually
# run callbacks manually
Datadog.send(:components).test_visibility.send(:on_test_finished, test_span)
Datadog.send(:components).test_visibility.send(:on_after_test_span_finished, tracer_span)
end

runs_count
Expand All @@ -253,7 +262,7 @@
it { is_expected.to eq(1) }
end

context "when retried failed tests strategy is used" do
context "when retry failed tests strategy is used" do
let(:remote_flaky_test_retries_enabled) { true }

context "when test span is failed" do
Expand All @@ -269,5 +278,22 @@
it { is_expected.to eq(1) }
end
end

context "when retry new test strategy is used" do
let(:remote_early_flake_detection_enabled) { true }
let(:unique_tests_set) { Set.new(["mysuite.mytest."]) }

it { is_expected.to eq(11) }

context "when test duration increases" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
before do
allow(tracer_span).to receive(:duration).and_return(5.1, 10.1, 30.1, 600.1)
end

# 5.1s (5 retries) -> 10.1s (3 retries) -> 30.1s (2 retries) -> done => 3 executions in total
it { is_expected.to eq(3) }
end
end
end
end
39 changes: 39 additions & 0 deletions spec/datadog/ci/test_retries/strategy/retry_new_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "../../../../../lib/datadog/ci/test_retries/strategy/retry_new"

RSpec.describe Datadog::CI::TestRetries::Strategy::RetryNew do
let(:max_attempts) { 10 }
let(:duration_thresholds) {
Datadog::CI::Remote::SlowTestRetries.new({
"5s" => 10,
"10s" => 5,
"30s" => 3,
"10m" => 2
})
}
subject(:strategy) { described_class.new(duration_thresholds: duration_thresholds) }

describe "#should_retry?" do
subject { strategy.should_retry? }
let(:test_span) { double(:test_span, set_tag: true) }

context "when max attempts haven't been reached yet" do
it { is_expected.to be true }
end

context "when the max attempts have been reached" do
before { max_attempts.times { strategy.record_retry(test_span) } }

it { is_expected.to be false }
end
end

describe "#record_duration" do
subject { strategy.record_duration(duration) }

let(:duration) { 5 }

it "updates the max attempts based on the duration" do
expect { subject }.to change { strategy.instance_variable_get(:@max_attempts) }.from(10).to(5)
end
end
end

0 comments on commit ec3c099

Please sign in to comment.