// Copyright 2020-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0
namespace ZB.MOM.NatsNet.Server.IntegrationTests.Helpers;
///
/// Provides polling-based check utilities for integration tests,
/// mirroring the Go checkFor pattern.
///
public static class CheckHelper
{
///
/// Polls every until it returns
/// null (success) or elapses. Throws on timeout.
///
public static void CheckFor(TimeSpan timeout, TimeSpan interval, Func check)
{
var deadline = DateTime.UtcNow + timeout;
string? lastError = null;
while (DateTime.UtcNow < deadline)
{
lastError = check();
if (lastError is null)
return;
Thread.Sleep(interval);
}
throw new TimeoutException($"CheckFor timed out after {timeout}: {lastError}");
}
///
/// Async variant of .
///
public static async Task CheckForAsync(TimeSpan timeout, TimeSpan interval, Func> check)
{
var deadline = DateTime.UtcNow + timeout;
string? lastError = null;
while (DateTime.UtcNow < deadline)
{
lastError = await check();
if (lastError is null)
return;
await Task.Delay(interval);
}
throw new TimeoutException($"CheckFor timed out after {timeout}: {lastError}");
}
}