2026-07-23 15:37:20 +00:00
|
|
|
using Heimdall.Agent.Services;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
|
|
|
|
|
|
public class RetryDelayedTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_ReturnsSpecifiedDelaysInOrder()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(
|
|
|
|
|
TimeSpan.FromSeconds(1),
|
|
|
|
|
TimeSpan.FromSeconds(5),
|
|
|
|
|
TimeSpan.FromSeconds(30));
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 0 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(1));
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 1 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(5));
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 2 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(30));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_AfterExhaustingDelays_ReturnsSixtySeconds()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromSeconds(1));
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 1 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_WithNoDelays_AlwaysReturnsSixtySeconds()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = i })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_WithSingleDelay_ReturnsFallbackAfterExhaustion()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromMilliseconds(500));
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 0 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromMilliseconds(500));
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 1 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 100 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_LargeRetryCount_DoesNotOverflow()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = int.MaxValue })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_AtExactBoundary_WorksCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromSeconds(1));
|
|
|
|
|
|
|
|
|
|
// PreviousRetryCount == _delays.Length → exhausted, returns 60s
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 1 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_ContextWithReason_StillReturnsCorrectDelay()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromSeconds(3));
|
|
|
|
|
|
|
|
|
|
var context = new RetryContext
|
|
|
|
|
{
|
|
|
|
|
PreviousRetryCount = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(context).ShouldBe(TimeSpan.FromSeconds(3));
|
|
|
|
|
}
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_NegativePreviousRetryCount_FallsBackToSixtySeconds()
|
|
|
|
|
{
|
|
|
|
|
var retry = new RetryDelayed(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5));
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = -1 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NextRetryDelay_LargeArrayOfDelays_WorksCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var delays = Enumerable.Range(1, 10).Select(s => TimeSpan.FromSeconds(s)).ToArray();
|
|
|
|
|
var retry = new RetryDelayed(delays);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = i })
|
|
|
|
|
.ShouldBe(delays[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retry.NextRetryDelay(new RetryContext { PreviousRetryCount = 10 })
|
|
|
|
|
.ShouldBe(TimeSpan.FromSeconds(60));
|
|
|
|
|
}
|
2026-07-23 15:37:20 +00:00
|
|
|
}
|