using OpenWaf.Infrastructure.Persistence; namespace OpenWaf.Tests.Unit; /// /// The arithmetic of retrying (OPEN-60). Both ways a retry loop fails are /// arithmetic — a loop that never stops, and a loop that stops too early — and /// neither needs a running edge and a refusing endpoint to check. /// public sealed class PushRetryPolicyTests { [Theory] [InlineData(null, 6)] [InlineData("", 6)] [InlineData("1", 1)] [InlineData(" 9 ", 9)] [InlineData("20", 20)] public void Unset_means_six_and_a_whole_number_means_what_it_says(string? configured, int expected) => Assert.Equal(expected, PushRetryPolicy.ParseMaxAttempts(configured)); [Theory] [InlineData("0")] [InlineData("-1")] [InlineData("21")] [InlineData("abc")] [InlineData("6.5")] public void A_bound_that_could_not_mean_what_the_operator_wrote_refuses_startup(string configured) { var ex = Assert.Throws(() => PushRetryPolicy.ParseMaxAttempts(configured)); Assert.Contains("OpenWaf:Alerts:MaxPushAttempts", ex.Message); Assert.Contains(configured, ex.Message); } [Theory] [InlineData(null, 30)] [InlineData("1", 1)] [InlineData("3600", 3600)] public void The_backoff_base_is_parsed_the_same_way(string? configured, int expected) => Assert.Equal(expected, PushRetryPolicy.ParseRetrySeconds(configured)); [Theory] [InlineData("0")] [InlineData("3601")] [InlineData("-2")] public void A_zero_backoff_against_a_dead_endpoint_is_a_busy_loop_and_is_refused(string configured) { var ex = Assert.Throws(() => PushRetryPolicy.ParseRetrySeconds(configured)); Assert.Contains("OpenWaf:Alerts:PushRetrySeconds", ex.Message); } [Fact] public void Backoff_grows_and_stays_inside_its_cap() { Assert.Equal(TimeSpan.FromSeconds(30), PushRetryPolicy.NextDelay(1, 30)); Assert.Equal(TimeSpan.FromSeconds(60), PushRetryPolicy.NextDelay(2, 30)); Assert.Equal(TimeSpan.FromSeconds(120), PushRetryPolicy.NextDelay(3, 30)); Assert.Equal(TimeSpan.FromMinutes(4), PushRetryPolicy.NextDelay(4, 30)); Assert.Equal(TimeSpan.FromMinutes(8), PushRetryPolicy.NextDelay(5, 30)); // The cap is the point: exponential from a 30-second base reaches a day // in eight steps, and a day is not a retry interval for a page that was // meant to fire at 03:00. Assert.Equal(TimeSpan.FromMinutes(30), PushRetryPolicy.NextDelay(9, 30)); Assert.Equal(TimeSpan.FromMinutes(30), PushRetryPolicy.NextDelay(40, 30)); } [Fact] public void No_attempts_means_no_delay_and_a_zero_base_never_produces_a_zero_delay() { Assert.Equal(TimeSpan.Zero, PushRetryPolicy.NextDelay(0, 30)); // One failed attempt always waits at least the base, so the worker cannot // spin against an endpoint that is refusing. Assert.True(PushRetryPolicy.NextDelay(1, 1) >= TimeSpan.FromSeconds(1)); } [Fact] public void Giving_up_is_a_bound_an_operator_can_set_and_a_state_the_surface_can_name() { Assert.False(PushRetryPolicy.HasGivenUp(5, 6)); Assert.True(PushRetryPolicy.HasGivenUp(6, 6)); Assert.True(PushRetryPolicy.HasGivenUp(7, 6)); // The bound is configuration, so a test — and an operator who wants the // edge to stop asking sooner — can ask for a small one. Assert.True(PushRetryPolicy.HasGivenUp(1, 1)); } [Fact] public void A_claim_is_reclaimable_sooner_than_a_retry_interval_so_a_crash_delays_rather_than_loses() { Assert.True(PushRetryPolicy.ClaimReclaimWindow < PushRetryPolicy.MaxDelay); Assert.True(PushRetryPolicy.ClaimReclaimWindow > TimeSpan.FromSeconds(AlertNotifier.TimeoutSecondsCeiling)); } }