89 lines
2.8 KiB
C#
89 lines
2.8 KiB
C#
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));
|
||
|
|
}
|
||
|
|
}
|