Fix exchange manager tests to handle losing timeslice better. (#27872)
We could get into a situation where we lost the timeslice after the SendMessage
call and before we asserted the response timeout had not happened yet, which
would cause the test to fail.
The changes here are:
1) Move the assert that we are not timed out to _before_ SendMessage(). This
ensures that our state is correct up front, and generally nothing under
SendMessage proper or sending the message should trigger a timeout per se.
2) Use more slack when waiting for the timeout, just in case.
Fixes https://github.com/project-chip/connectedhomeip/issues/27479
diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp
index cd938ad..b3fbba2 100644
--- a/src/messaging/tests/TestExchangeMgr.cpp
+++ b/src/messaging/tests/TestExchangeMgr.cpp
@@ -179,16 +179,19 @@
ExpireSessionFromTimeoutDelegate sendDelegate;
ExchangeContext * ec1 = ctx.NewExchangeToBob(&sendDelegate);
- ec1->SetResponseTimeout(System::Clock::Timeout(100));
+ auto timeout = System::Clock::Timeout(100);
+ ec1->SetResponseTimeout(timeout);
+
+ NL_TEST_ASSERT(inSuite, !sendDelegate.IsOnResponseTimeoutCalled);
ec1->SendMessage(Protocols::BDX::Id, kMsgType_TEST1, System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize),
SendFlags(Messaging::SendMessageFlags::kExpectResponse).Set(Messaging::SendMessageFlags::kNoAutoRequestAck));
-
ctx.DrainAndServiceIO();
- NL_TEST_ASSERT(inSuite, !sendDelegate.IsOnResponseTimeoutCalled);
- // Wait for our timeout to elapse. Give it an extra 100ms.
- ctx.GetIOContext().DriveIOUntil(200_ms32, [&sendDelegate] { return sendDelegate.IsOnResponseTimeoutCalled; });
+ // Wait for our timeout to elapse. Give it an extra 1000ms of slack,
+ // because if we lose the timeslice for longer than the slack we could end
+ // up breaking out of the loop before the timeout timer has actually fired.
+ ctx.GetIOContext().DriveIOUntil(timeout + 1000_ms32, [&sendDelegate] { return sendDelegate.IsOnResponseTimeoutCalled; });
NL_TEST_ASSERT(inSuite, sendDelegate.IsOnResponseTimeoutCalled);