From eaa43cf42f1359aa14fa58e1c28059968f21d19d Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 10:07:29 -0500 Subject: [PATCH] test(worker): net48-compatible timeout helper in the dispatcher gate test (WIP-010) --- .../Sta/StaCommandDispatcherTests.cs | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/Sta/StaCommandDispatcherTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/Sta/StaCommandDispatcherTests.cs index 92d333b4..cdd6c6c0 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/Sta/StaCommandDispatcherTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/Sta/StaCommandDispatcherTests.cs @@ -276,17 +276,19 @@ public sealed class StaCommandDispatcherTests try { - Task first = await Task.Run(() => dispatcher.DispatchAsync(CreateCommand("correlation-1", MxCommandKind.Register))) - .WaitAsync(TimeSpan.FromSeconds(10)); + // Task.Factory.StartNew (not Task.Run) so the returned reply task is not unwrapped: the + // dispatch call itself must return promptly even though the reply cannot complete yet. + Task first = await WithTimeoutAsync( + Task.Factory.StartNew(() => dispatcher.DispatchAsync(CreateCommand("correlation-1", MxCommandKind.Register)))); Assert.True(entered.Wait(TimeSpan.FromSeconds(10)), "The command logger was never called."); // The first command's log line is now blocked. Everything that takes the gate must still return. - Task second = await Task.Run(() => dispatcher.DispatchAsync(CreateCommand("correlation-2", MxCommandKind.AddItem))) - .WaitAsync(TimeSpan.FromSeconds(10)); - bool canceled = await Task.Run(() => dispatcher.CancelQueuedCommand("correlation-2")) - .WaitAsync(TimeSpan.FromSeconds(10)); - int pending = await Task.Run(() => dispatcher.PendingCommandCount) - .WaitAsync(TimeSpan.FromSeconds(10)); + Task second = await WithTimeoutAsync( + Task.Factory.StartNew(() => dispatcher.DispatchAsync(CreateCommand("correlation-2", MxCommandKind.AddItem)))); + bool canceled = await WithTimeoutAsync( + Task.Run(() => dispatcher.CancelQueuedCommand("correlation-2"))); + int pending = await WithTimeoutAsync( + Task.Run(() => dispatcher.PendingCommandCount)); Assert.True(canceled); Assert.Equal(0, pending); @@ -297,7 +299,7 @@ public sealed class StaCommandDispatcherTests release.Set(); } - Assert.Equal(ProtocolStatusCode.Ok, (await Task.Run(() => dispatcher.DispatchAsync(CreateCommand("correlation-3", MxCommandKind.Register)))).ProtocolStatus.Code); + Assert.Equal(ProtocolStatusCode.Ok, (await dispatcher.DispatchAsync(CreateCommand("correlation-3", MxCommandKind.Register))).ProtocolStatus.Code); } /// Verifies the opt-in environment value is parsed strictly. @@ -347,6 +349,18 @@ public sealed class StaCommandDispatcherTests /// /// Test executor that records executed command correlations and thread IDs. /// + // net48 has no Task.WaitAsync; a task that does not finish in time fails the test instead of hanging it. + private static async Task WithTimeoutAsync(Task task) + { + Task finished = await Task.WhenAny(task, Task.Delay(TimeSpan.FromSeconds(10))); + if (!ReferenceEquals(finished, task)) + { + throw new TimeoutException("The operation did not complete while the command logger was blocked."); + } + + return await task; + } + private sealed class BlockingWorkerLogger : IWorkerLogger { private readonly ManualResetEventSlim entered;