The push queue was a channel in one process: three attempts inside one request of work, then the row was simply no longer owed to anybody. An endpoint down for ten minutes swallowed every alert raised in that window, and a restart swallowed everything still waiting. The alert table stayed correct the whole time, which is exactly what made the hole invisible from the surface. What is owed is now a row in alert_pushes: denormalised payload (retention prunes edge_alerts, and a delivery still owed must not go with the row it was copied from), attempts, next-attempt time, last error, and a claim so two edges over one database send each row once. Attempts are configuration (OpenWaf:Alerts:MaxPushAttempts, PushRetrySeconds), validated at startup like the other bounds; when they run out the row records that and the surface says how many were given up, because a counter that cannot tell 'still retrying' from 'stopped retrying' is the blindness this item removes. Surfaces: /admin-api/state answers alertPushPending/Delivered/GivenUp/OldestPendingAt/LastError from the table, /metrics publishes the same as gauges from a cache no older than 5s (the metrics endpoint's rule is that it never leans on the database per scrape). Retention prunes only rows that are finished. What the run caught, in order: (1) SchemaUpgradeTests hardcoded the migration count as a literal, so the new migration turned a working upgrade into a red test - the count is now read from the migrations assembly. (2) Two of the new queue tests asserted 'pending == 0' after waiting only for 'delivered >= 1'; one scene raises more than one alert, so the read landed between two rows and failed on a correct product - the wait now requires the queue to have emptied. (3) The whole integration suite answered 9 failures of 251, every one 'Timed out waiting for the entry point to build the IHost after 00:05:00', while the identical build minutes later answered 251/251 in 34s: a test host that cannot bind a loopback socket fails as a timeout, and the README now says so next to the scheduling note that used to be the only explanation on file. Gates on this tree: build 0 warnings / 0 errors, unit 277/277, integration 251/251 in 34s, smoke 68 passed / 0 failed (3 new), image 261 MB, compose-smoke 232 passed / 0 failed with 0 containers left behind (2 new, including the push-queue table being added by the edge to a volume that predates it), console 90 assertions / 0 failed. Refs OPEN-60.
93 lines
3.8 KiB
C#
93 lines
3.8 KiB
C#
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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<InvalidOperationException>(() => 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<InvalidOperationException>(() => 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));
|
|
}
|
|
}
|