diff --git a/tests/Titano.Server.Tests/Jobs/RenderNodeClientTests.cs b/tests/Titano.Server.Tests/Jobs/RenderNodeClientTests.cs index ef445c21..c32fecf2 100644 --- a/tests/Titano.Server.Tests/Jobs/RenderNodeClientTests.cs +++ b/tests/Titano.Server.Tests/Jobs/RenderNodeClientTests.cs @@ -101,18 +101,20 @@ public sealed class RenderNodeClientTests(TestContext context) var job = await CreateRenderAsync(server, editor, sequenceId, Mjpeg with { Batch = ["gif-preview"] }); var gif = (await server.Database.Jobs.ListAsync(JobState.Queued, JobKind.Render)).Items.Single(j => j.Id != job.Id); - using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "studio-pc") { Token = token }; + var claims = new CountClaims(new SocketsHttpHandler()); + using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "studio-pc", claims) { Token = token }; var cache = context.CreateTempDirectory("node"); // Claims only when started and after each job: between them only heartbeats reach the server. var node = Node(client, cache, poll: TimeSpan.FromMinutes(5), heartbeat: TimeSpan.FromMilliseconds(100)); node.Start(); await JobApiTests.WaitForAsync(editor, job.Id, "done"); await JobApiTests.WaitForAsync(editor, gif.Id, "done"); - // Past the claim that found the queue empty, only heartbeats move the node's last sign of life. - await Task.Delay(300); + // Past the claim that found the queue empty (the server touched the node before answering it), only heartbeats + // move the node's last sign of life. + await UntilAsync(() => Task.FromResult(claims.Answered >= 3), "the claim that found the queue empty"); var beat = (await server.Database.RenderNodes.ListAsync()).Single().LastHeartbeat; - await Task.Delay(400); - Assert.Greater((await server.Database.RenderNodes.ListAsync()).Single().LastHeartbeat.Ticks, beat.Ticks); + await UntilAsync(async () => (await server.Database.RenderNodes.ListAsync()).Single().LastHeartbeat > beat, "a heartbeat"); + Assert.Equal(3, claims.Answered, "no claim since the queue was empty"); await node.DisposeAsync(); Assert.True(node.Completion.IsCompleted, "stopped"); @@ -312,6 +314,21 @@ public sealed class RenderNodeClientTests(TestContext context) } } + /// Counts the node's claims the server answered. + private sealed class CountClaims(HttpMessageHandler inner) : DelegatingHandler(inner) + { + private int _answered; + + public int Answered => Volatile.Read(ref _answered); + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var response = await base.SendAsync(request, cancellationToken); + if (request.RequestUri!.AbsolutePath.EndsWith("/claim", StringComparison.Ordinal)) Interlocked.Increment(ref _answered); + return response; + } + } + /// The first cut bytes, then a reset connection. private sealed class CutStream(byte[] data, int cut) : MemoryStream(data, 0, cut) { @@ -325,6 +342,16 @@ public sealed class RenderNodeClientTests(TestContext context) private static int Check(int read) => read > 0 ? read : throw new IOException("Connection reset by peer"); } + private static async Task UntilAsync(Func> condition, string what) + { + var until = DateTime.UtcNow.AddSeconds(30); + while (!await condition()) + { + if (DateTime.UtcNow > until) Assert.Fail("Timed out waiting for " + what); + await Task.Delay(25); + } + } + private static async Task CreateRenderAsync(TestServer server, HttpClient editor, Guid sequenceId, ExportSettings? export = null) { var sequence = (await server.Database.Sequences.GetAsync(sequenceId))!;