From 9566818fd60e92fbd5abae86ec4650bef41ef693 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 6 Oct 2026 21:11:27 +0000 Subject: [PATCH] test(sync): the server event test waits for the comparison the edit starts ServerEventsUpdateThisPcWithoutWaitingForTheNextComparison polled until the server's settings reached the local copy, then expected the row in sync; the link that records the agreement is stored after the local copy, so the row could still read LocalAhead (1 failure in 15 loaded runs). It now waits for SyncService.Changed, raised once the comparison stored every link and state. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .../Sync/SyncServerTests.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/Titano.Server.Tests/Sync/SyncServerTests.cs b/tests/Titano.Server.Tests/Sync/SyncServerTests.cs index c7d1c00b..daea9b86 100644 --- a/tests/Titano.Server.Tests/Sync/SyncServerTests.cs +++ b/tests/Titano.Server.Tests/Sync/SyncServerTests.cs @@ -22,6 +22,9 @@ public sealed class SyncServerTests(TestContext context) private const string Window41 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":41}}}"""; private const string Window33 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":33}}}"""; + /// How long any wait may take on a loaded runner; every wait ends as soon as what it waits for happened. + private static readonly TimeSpan Deadline = TimeSpan.FromSeconds(60); + [Test] public async Task ASequenceUploadedFromOnePcDownloadsIdenticallyOnAnother() { @@ -147,12 +150,16 @@ public sealed class SyncServerTests(TestContext context) var hub = server.Services.GetRequiredService(); await UntilAsync(() => Task.FromResult(agent.Snapshot is not null && hub.SubscriberCount > 0), "the agent's first comparison and event socket"); - // Someone edits the project on the server (web or another PC). + // Someone edits the project on the server (web or another PC). The comparison its event starts writes the + // settings on this PC before the link that records the agreement: wait for the comparison, not for the settings. + var compared = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + studio.Sync.Changed += _ => compared.TrySetResult(); using (var patch = await http.PatchAsJsonAsync($"/api/v1/sequences/{remoteId}", new UpdateSequenceRequest(null, null, null, null, Window33, null), ProtocolJson.Default.UpdateSequenceRequest)) await patch.ExpectAsync(HttpStatusCode.OK); - await UntilAsync(async () => ProjectSettings.FromJson((await studio.Db.Sequences.GetAsync(local.Id))!.SettingsJson ?? "{}").Exposure.DeflickerWindow.Manual == 33, + await SignalAsync(compared.Task, "the comparison of the edit"); + Assert.Equal(33, ProjectSettings.FromJson((await studio.Db.Sequences.GetAsync(local.Id))!.SettingsJson ?? "{}").Exposure.DeflickerWindow.Manual, "the edit reached this PC"); Assert.Equal(SyncStatus.InSync, (await studio.EntryAsync("Harbour sunset")).Status); } @@ -244,11 +251,23 @@ public sealed class SyncServerTests(TestContext context) private async Task UntilAsync(Func> condition, string what) { - var until = DateTime.UtcNow.AddSeconds(60); + var until = DateTime.UtcNow + Deadline; while (!await condition()) { if (DateTime.UtcNow > until) Assert.Fail("Timed out waiting for " + what); await Task.Delay(50, context.Cancellation); } } + + private async Task SignalAsync(Task signal, string what) + { + try + { + await signal.WaitAsync(Deadline, context.Cancellation); + } + catch (TimeoutException) + { + Assert.Fail("Timed out waiting for " + what); + } + } }