-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse slow test retries payload from library settings
- Loading branch information
1 parent
1c91543
commit 0798794
Showing
7 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module CI | ||
module Remote | ||
# Parses "slow_test_retries" payload for early flake detection settings | ||
# | ||
# Example payload: | ||
# { | ||
# "5s" => 10, | ||
# "10s" => 5, | ||
# "30s" => 3, | ||
# "5m" => 2 | ||
# } | ||
# | ||
# The payload above means that for tests that run less than 5 seconds, we should retry them 10 times, | ||
# for tests that run less than 10 seconds, we should retry them 5 times, and so on. | ||
class SlowTestRetries | ||
attr_reader :entries | ||
|
||
Entry = Struct.new(:duration, :max_attempts) | ||
|
||
DURATION_MEASURES = { | ||
"s" => 1, | ||
"m" => 60 | ||
}.freeze | ||
|
||
def initialize(payload) | ||
@entries = parse(payload) | ||
end | ||
|
||
def max_attempts_for_duration(duration) | ||
@entries.each do |entry| | ||
return entry.max_attempts if duration < entry.duration | ||
end | ||
|
||
1 | ||
end | ||
|
||
private | ||
|
||
def parse(payload) | ||
(payload || {}).keys.filter_map do |key| | ||
duration, measure = key.match(/(\d+)(\w+)/)&.captures | ||
next if duration.nil? || measure.nil? || !DURATION_MEASURES.key?(measure) | ||
|
||
Entry.new(duration.to_f * DURATION_MEASURES.fetch(measure, 1), payload[key].to_i) | ||
end.sort_by(&:duration) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Datadog | ||
module CI | ||
module Remote | ||
class SlowTestRetries | ||
attr_reader entries: Enumerable[Entry] | ||
|
||
class Entry | ||
attr_accessor duration: Float | ||
|
||
attr_accessor max_attempts: Integer | ||
|
||
def initialize: (Float duration, Integer max_attempts) -> void | ||
end | ||
|
||
DURATION_MEASURES: Hash[String, Integer] | ||
|
||
def initialize: (Hash[String, String | Integer] payload) -> void | ||
|
||
def max_attempts_for_duration: (Float duration) -> Integer | ||
|
||
private | ||
|
||
def parse: (Hash[String, String | Integer] payload) -> Enumerable[Entry] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../../../lib/datadog/ci/remote/slow_test_retries" | ||
|
||
RSpec.describe Datadog::CI::Remote::SlowTestRetries do | ||
let(:payload) do | ||
{ | ||
"5m" => 2, | ||
"5s" => 10, | ||
"30s" => 3, | ||
"10s" => 5, | ||
"ffdsfdsfds" => nil, | ||
"10m" => nil, | ||
"10h" => 12 | ||
} | ||
end | ||
|
||
subject(:slow_test_retries) { described_class.new(payload) } | ||
|
||
describe "#initialize" do | ||
subject { slow_test_retries.entries } | ||
|
||
it do | ||
is_expected.to eq([ | ||
described_class::Entry.new(5.0, 10), | ||
described_class::Entry.new(10.0, 5), | ||
described_class::Entry.new(30.0, 3), | ||
described_class::Entry.new(300.0, 2), | ||
described_class::Entry.new(600.0, 0) | ||
]) | ||
end | ||
end | ||
|
||
describe "#max_attempts_for_duration" do | ||
subject { slow_test_retries.max_attempts_for_duration(duration) } | ||
|
||
context "when the duration is less than 5 seconds" do | ||
let(:duration) { 4.9 } | ||
|
||
it { is_expected.to eq(10) } | ||
end | ||
|
||
context "when the duration is less than 10 seconds" do | ||
let(:duration) { 9.9 } | ||
|
||
it { is_expected.to eq(5) } | ||
end | ||
|
||
context "when the duration is less than 30 seconds" do | ||
let(:duration) { 29.9 } | ||
|
||
it { is_expected.to eq(3) } | ||
end | ||
|
||
context "when the duration is less than 5 minutes" do | ||
let(:duration) { 299.9 } | ||
|
||
it { is_expected.to eq(2) } | ||
end | ||
|
||
context "when the duration is less than 10 minutes" do | ||
let(:duration) { 599.9 } | ||
|
||
it { is_expected.to eq(0) } | ||
end | ||
|
||
context "when the duration is more than 10 minutes" do | ||
let(:duration) { 600.1 } | ||
|
||
it { is_expected.to eq(1) } | ||
end | ||
end | ||
end |