From 9c6ac0cdbc47d01c0967f6c7f1b37d26f220962c Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 10 Nov 2026 11:02:22 -0500 Subject: [PATCH] test(integration): live Write2 round-trip with timestamp (#212, IntegrationTests-033) Opt-in [LiveMxAccessFact] mirroring the Write round-trip: Register/AddItem/ Advise, then Write2 with a VT_DATE timestamp value; asserts Ok + Write2 kind and a matching OnWriteComplete. Skips without the live MXAccess env. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV --- .../WorkerLiveMxAccessSmokeTests.cs | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs b/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs index 0c06d60d..e2b880a3 100644 --- a/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs +++ b/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs @@ -233,6 +233,123 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output) } } + /// + /// Verifies that a Write2 command (the B8 extended write carrying a timestamp + /// value as its second context parameter) round-trips through live MXAccess against an + /// advised item and raises a matching event. + /// A regression that short-circuits Write2 to InvalidRequest (or drops the + /// timestamp) fails here, which no other live test catches (IntegrationTests-033, #212). + /// + /// A task that represents the asynchronous operation. + [LiveMxAccessFact] + public async Task GatewaySession_WithLiveWorker_Write2ValueWithTimestampToAdvisedItem() + { + string workerExecutablePath = IntegrationTestEnvironment.ResolveLiveMxAccessWorkerExecutablePath(); + Assert.True( + File.Exists(workerExecutablePath), + $"Live MXAccess worker executable was not found at {workerExecutablePath}. Build the worker or set {IntegrationTestEnvironment.LiveMxAccessWorkerExecutableVariableName}."); + + TestWorkerProcessFactory processFactory = new(output); + await using GatewayServiceFixture fixture = new(workerExecutablePath, processFactory, output); + using RecordingServerStreamWriter eventWriter = new(); + + string? sessionId = null; + Task? streamTask = null; + using CancellationTokenSource streamCancellation = new(); + + try + { + OpenSessionReply openReply = await fixture.Service.OpenSession( + new OpenSessionRequest + { + ClientSessionName = "live-mxaccess-write2", + ClientCorrelationId = "live-open-write2", + CommandTimeout = Duration.FromTimeSpan(CommandTimeout), + }, + new TestServerCallContext()).ConfigureAwait(false); + + sessionId = openReply.SessionId; + Assert.Equal(ProtocolStatusCode.Ok, openReply.ProtocolStatus.Code); + + streamTask = fixture.Service.StreamEvents( + new StreamEventsRequest { SessionId = sessionId }, + eventWriter, + new TestServerCallContext(streamCancellation.Token)); + + MxCommandReply registerReply = await fixture.Service.Invoke( + CreateRegisterRequest(sessionId), + new TestServerCallContext()).ConfigureAwait(false); + LogReply("Register", registerReply); + Assert.Equal(ProtocolStatusCode.Ok, registerReply.ProtocolStatus.Code); + + MxCommandReply addItemReply = await fixture.Service.Invoke( + CreateAddItemRequest(sessionId, registerReply.Register.ServerHandle), + new TestServerCallContext()).ConfigureAwait(false); + LogReply("AddItem", addItemReply); + Assert.Equal(ProtocolStatusCode.Ok, addItemReply.ProtocolStatus.Code); + Assert.True(addItemReply.AddItem.ItemHandle > 0); + + MxCommandReply adviseReply = await fixture.Service.Invoke( + CreateAdviseRequest( + sessionId, + registerReply.Register.ServerHandle, + addItemReply.AddItem.ItemHandle), + new TestServerCallContext()).ConfigureAwait(false); + LogReply("Advise", adviseReply); + Assert.Equal(ProtocolStatusCode.Ok, adviseReply.ProtocolStatus.Code); + + MxCommandReply writeReply = await fixture.Service.Invoke( + CreateWrite2Request( + sessionId, + registerReply.Register.ServerHandle, + addItemReply.AddItem.ItemHandle), + new TestServerCallContext()).ConfigureAwait(false); + LogReply("Write2", writeReply); + + // Happy-path Write2: the worker reached the COM Write2 call and it + // succeeded. InvalidRequest here means the executor rejected the + // payload (missing value/timestamp) before reaching MXAccess. + Assert.Equal(ProtocolStatusCode.Ok, writeReply.ProtocolStatus.Code); + Assert.Equal(MxCommandKind.Write2, writeReply.Kind); + + // Proof of round-trip: MXAccess fires OnWriteComplete (event id 2) + // after the underlying provider acknowledges the write — that is + // the event the cross-language client e2e runner asserts on. We + // scan the recorded stream (so an interleaving OnDataChange does + // not preempt the match) for an OnWriteComplete carrying the same + // server/item handles the Write command targeted. + MxEvent writeComplete = await eventWriter + .WaitForMessageAsync( + candidate => candidate.Family == MxEventFamily.OnWriteComplete + && candidate.ServerHandle == registerReply.Register.ServerHandle + && candidate.ItemHandle == addItemReply.AddItem.ItemHandle, + IntegrationTestEnvironment.LiveMxAccessEventTimeout, + streamCancellation.Token) + .ConfigureAwait(false); + LogEvent(writeComplete); + + Assert.Equal(MxEventFamily.OnWriteComplete, writeComplete.Family); + Assert.Equal(sessionId, writeComplete.SessionId); + Assert.Equal(registerReply.Register.ServerHandle, writeComplete.ServerHandle); + Assert.Equal(addItemReply.AddItem.ItemHandle, writeComplete.ItemHandle); + + // The stream task must not be in a faulted state. ShutDownAsync's + // broad catch would otherwise swallow the fault and silently let + // this Write2-parity coverage pass against a broken event pipeline. + Assert.False( + streamTask.IsFaulted, + streamTask.Exception?.ToString() ?? "Event stream task faulted without an exception."); + } + finally + { + // Cancel the stream call before draining so StreamEvents observes + // cancellation rather than blocking on the channel. Any unhandled + // stream-task fault is rethrown from ShutDownAsync into the test. + streamCancellation.Cancel(); + await ShutDownAsync(fixture, processFactory, sessionId, streamTask, propagateStreamFaults: true).ConfigureAwait(false); + } + } + /// /// Verifies that an AddItem against an invalid server handle surfaces the MXAccess failure /// without faulting the gateway transport, exercising the invalid-handle parity path. @@ -1444,6 +1561,38 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output) }; } + private static MxCommandRequest CreateWrite2Request( + string sessionId, + int serverHandle, + int itemHandle) + { + return new MxCommandRequest + { + SessionId = sessionId, + ClientCorrelationId = "live-write2", + Command = new MxCommand + { + Kind = MxCommandKind.Write2, + Write2 = new Write2Command + { + ServerHandle = serverHandle, + ItemHandle = itemHandle, + Value = new MxValue + { + DataType = MxDataType.Integer, + Int32Value = 2, + }, + TimestampValue = new MxValue + { + DataType = MxDataType.Time, + VariantType = "VT_DATE", + TimestampValue = Timestamp.FromDateTime(DateTime.UtcNow), + }, + }, + }, + }; + } + private static MxCommandRequest CreateUnAdviseRequest( string sessionId, int serverHandle,