From 8460f24274a878cff82ba7a7598d6a73c030e7f8 Mon Sep 17 00:00:00 2001 From: Sergii Gulyk Date: Wed, 14 Sep 2022 13:11:38 +0200 Subject: [PATCH] Adapter client initialized all its data items, setting them to UNAVAILABLE Lest creating race condition we should wait until all items are initialized in OnConnect (see ShdrClient.ListenForAdapter) The essence of race condition was that a data item might be sent via adapter EARLIER than adapter client initialized the same data item to UNAVAILABLE. As a result MTConnectAgent.FilterPeriod was discarding new value coming via adapter due to older timestamp assigned on sending. I have chosen ShdrClient.PingSent event, since ShdrClient.Listening was not called. --- .../ClientAgentCommunicationTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/IntegrationTests/ClientAgentCommunicationTests.cs b/tests/IntegrationTests/ClientAgentCommunicationTests.cs index 83185a24..589c9b0d 100644 --- a/tests/IntegrationTests/ClientAgentCommunicationTests.cs +++ b/tests/IntegrationTests/ClientAgentCommunicationTests.cs @@ -116,7 +116,22 @@ public ClientAgentCommunicationTests( { var adapterClient = new ShdrAdapterClient(adapterConfiguration, _agent, device); + // Adapter client initialized all its data items, setting them to UNAVAILABLE + // Lest creating race condition we should wait until all items are initialized in OnConnect (see ShdrClient.ListenForAdapter) + // The essence of race condition was that a data item might be sent via adapter EARLIER than adapter client initialized the same data item to UNAVAILABLE. + // As a result MTConnectAgent.FilterPeriod was discarding new value coming via adapter due to older timestamp assigned on sending. + // I have chosen ShdrClient.PingSent event, since ShdrClient.Listening was not called. + var waiter = new AutoResetEvent(false); + void DataItemsInitialized(object? sender, string msg) + { + adapterClient.PingSent -= DataItemsInitialized; + waiter.Set(); + } + + adapterClient.PingSent += DataItemsInitialized; adapterClient.Start(); + + Assert.True(waiter.WaitOne(10000)); } } }