// Copyright 2012-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0
namespace ZB.MOM.NatsNet.Server.IntegrationTests.Helpers;
///
/// Helper for polling-style checks in integration tests.
/// Corresponds to Go's checkFor(t, timeout, interval, func) pattern.
///
public static class CheckHelper
{
///
/// 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.
///
/// Maximum time to wait.
/// Polling interval.
/// Function returning null on success, or an error message on failure.
public static async Task CheckFor(TimeSpan timeout, TimeSpan interval, Func> 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}");
}
///
/// Polls the provided synchronous check function until it returns null (success) or the timeout elapses.
///
public static async Task CheckFor(TimeSpan timeout, TimeSpan interval, Func check)
{
await CheckFor(timeout, interval, () => Task.FromResult(check()));
}
}