Files
natsnet/dotnet/tests/ZB.MOM.NatsNet.Server.IntegrationTests/Helpers/CheckHelper.cs
Joseph Doherty 6a0094524d test(batch55): port 75 NoRace integration tests
Ports 51 tests from norace_1_test.go and 24 tests from norace_2_test.go
as [SkippableFact] integration tests. Creates test harness infrastructure
(IntegrationTestBase, CheckHelper, NatsTestClient, TestServerHelper,
TestCluster) and tags all tests with [Trait("Category", "NoRace")].
Tests skip unless NATS_INTEGRATION_ENABLED=true is set.
2026-03-01 12:17:07 -05:00

40 lines
1.6 KiB
C#

// Copyright 2012-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0
namespace ZB.MOM.NatsNet.Server.IntegrationTests.Helpers;
/// <summary>
/// Helper for polling-style checks in integration tests.
/// Corresponds to Go's checkFor(t, timeout, interval, func) pattern.
/// </summary>
public static class CheckHelper
{
/// <summary>
/// Polls the provided check function until it returns null (success) or the timeout elapses.
/// Throws an exception with the last error message if the timeout is reached.
/// </summary>
/// <param name="timeout">Maximum time to wait.</param>
/// <param name="interval">Polling interval.</param>
/// <param name="check">Function returning null on success, or an error message on failure.</param>
public static async Task CheckFor(TimeSpan timeout, TimeSpan interval, Func<Task<string?>> check)
{
var deadline = DateTime.UtcNow + timeout;
string? lastError = null;
while (DateTime.UtcNow < deadline)
{
lastError = await check();
if (lastError == null) return;
await Task.Delay(interval);
}
throw new InvalidOperationException($"CheckFor timed out after {timeout}: {lastError}");
}
/// <summary>
/// Polls the provided synchronous check function until it returns null (success) or the timeout elapses.
/// </summary>
public static async Task CheckFor(TimeSpan timeout, TimeSpan interval, Func<string?> check)
{
await CheckFor(timeout, interval, () => Task.FromResult(check()));
}
}