test(render-node): read the node's last beat after its empty claim was answered
The heartbeat check slept 300 ms to get past the claim that finds the queue empty and then gave heartbeats 400 ms to move the node's last sign of life: a slow runner could fail it, or pass it on the claim's own touch. It now counts the claims the server answered, reads the last beat after the third one, waits for a later beat with a deadline and checks that no claim came since. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
1 file changed
+32
-5
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Counts the node's claims the server answered.</summary>
|
||||
private sealed class CountClaims(HttpMessageHandler inner) : DelegatingHandler(inner)
|
||||
{
|
||||
private int _answered;
|
||||
|
||||
public int Answered => Volatile.Read(ref _answered);
|
||||
|
||||
protected override async Task<HttpResponseMessage> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The first <c>cut</c> bytes, then a reset connection.</summary>
|
||||
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<Task<bool>> 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<JobDto> CreateRenderAsync(TestServer server, HttpClient editor, Guid sequenceId, ExportSettings? export = null)
|
||||
{
|
||||
var sequence = (await server.Database.Sequences.GetAsync(sequenceId))!;
|
||||
|
||||
Reference in new issue
Block a user