using NATS.Server.TestUtilities;

namespace NATS.Server.Clustering.Tests;

public class RouteHandshakeTests
{
    [Fact]
    public async Task Two_servers_establish_route_connection()
    {
        await using var a = await TestServerFactory.CreateClusterEnabledAsync();
        await using var b = await TestServerFactory.CreateClusterEnabledAsync(seed: a.ClusterListen);

        await a.WaitForReadyAsync();
        await b.WaitForReadyAsync();

        using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
        while (!timeout.IsCancellationRequested && (a.Stats.Routes == 0 || b.Stats.Routes == 0))
        {
            await Task.Delay(50, timeout.Token).ContinueWith(_ => { }, TaskScheduler.Default);
        }

        a.Stats.Routes.ShouldBeGreaterThan(0);
        b.Stats.Routes.ShouldBeGreaterThan(0);
    }
}
