From c29f98ffe0455aa54977b5dec9bcc465dbd30f31 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 9 Nov 2026 13:33:13 -0500 Subject: [PATCH] test(ablegacy): make the LastSuccessfulRead-advanced assertions deterministic by waiting for the clock to pass the init stamp (lmxopcua#767) Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV --- .../NativePcccBridgeStatusHealthTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/NativePcccBridgeStatusHealthTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/NativePcccBridgeStatusHealthTests.cs index 58eecee7..ad39c4f9 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/NativePcccBridgeStatusHealthTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/NativePcccBridgeStatusHealthTests.cs @@ -44,6 +44,19 @@ public sealed class NativePcccBridgeStatusHealthTests return server; } + /// + /// InitializeAsync stamps LastSuccessfulRead with DateTime.UtcNow and the read stamps its own + /// UtcNow microseconds later; on a coarse/identical clock reading the two are equal, so an "advanced" + /// assertion (ShouldNotBe(before)) failed ~70% of runs (lmxopcua#767). Spin until the clock has strictly + /// passed the init stamp so any later stamp is provably newer. The assertion itself is unchanged. + /// + private static void WaitUntilClockPast(DateTime? stamp) + { + if (stamp is null) return; + var spin = new SpinWait(); + while (DateTime.UtcNow <= stamp.Value) spin.SpinOnce(); + } + #pragma warning disable OTOPCUA0001 // the driver is called directly to pin its OWN classification (no resilience wrap) private static async Task DriverAsync(FakeEipServer server) { @@ -86,6 +99,7 @@ public sealed class NativePcccBridgeStatusHealthTests await using var server = StsServer(0x60); var driver = await DriverAsync(server); var before = driver.GetHealth().LastSuccessfulRead; + WaitUntilClockPast(before); var result = (await driver.ReadAsync(["T"], ct)).Single(); @@ -116,6 +130,7 @@ public sealed class NativePcccBridgeStatusHealthTests await using var server = general == 0 ? StsServer(0x10) : CipEnvelopeServer(general); var driver = await DriverAsync(server); var before = driver.GetHealth().LastSuccessfulRead; + WaitUntilClockPast(before); var result = (await driver.ReadAsync(["T"], ct)).Single();