Files
scadalink-design/tests/ScadaLink.Host.Tests/StartupRetryTests.cs

66 lines
2.0 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
namespace ScadaLink.Host.Tests;
/// <summary>
/// Host-010: startup preconditions (database migration) must tolerate a database
/// that is briefly unavailable at boot — common when an app container and its DB
/// container start together — via a bounded retry with backoff.
/// </summary>
public class StartupRetryTests
{
[Fact]
public async Task ExecuteWithRetry_SucceedsFirstTry_RunsOnce()
{
var attempts = 0;
await StartupRetry.ExecuteWithRetryAsync(
"test-op",
() => { attempts++; return Task.CompletedTask; },
maxAttempts: 5,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance);
Assert.Equal(1, attempts);
}
[Fact]
public async Task ExecuteWithRetry_TransientFailures_RetriesUntilSuccess()
{
var attempts = 0;
await StartupRetry.ExecuteWithRetryAsync(
"test-op",
() =>
{
attempts++;
if (attempts < 3)
throw new InvalidOperationException("db not ready");
return Task.CompletedTask;
},
maxAttempts: 5,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance);
Assert.Equal(3, attempts);
}
[Fact]
public async Task ExecuteWithRetry_ExhaustsAttempts_RethrowsLastException()
{
var attempts = 0;
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() =>
StartupRetry.ExecuteWithRetryAsync(
"test-op",
() =>
{
attempts++;
throw new InvalidOperationException($"failure {attempts}");
},
maxAttempts: 3,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance));
Assert.Equal(3, attempts);
Assert.Equal("failure 3", ex.Message);
}
}