diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 5d0179743cdff9..ac717f69d99016 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -95,6 +95,10 @@ class MockAppDelegate : public ExchangeDelegate System::PacketBufferHandle && buffer) override { IsOnMessageReceivedCalled = true; + if (mDropAckResponse) + { + ec->GetReliableMessageContext()->SetAckPending(false); + } ec->Close(); if (mTestSuite != nullptr) { @@ -106,8 +110,8 @@ class MockAppDelegate : public ExchangeDelegate void OnResponseTimeout(ExchangeContext * ec) override {} bool IsOnMessageReceivedCalled = false; - - nlTestSuite * mTestSuite = nullptr; + bool mDropAckResponse = false; + nlTestSuite * mTestSuite = nullptr; }; class MockSessionEstablishmentExchangeDispatch : public Messaging::ExchangeMessageDispatch @@ -487,6 +491,91 @@ void CheckResendApplicationMessageWithPeerExchange(nlTestSuite * inSuite, void * rm->ClearRetransTable(rc); } +void CheckResendApplicationMessageWithLostAcks(nlTestSuite * inSuite, void * inContext) +{ + TestContext & ctx = *reinterpret_cast<TestContext *>(inContext); + + ctx.GetInetLayer().SystemLayer()->Init(nullptr); + + chip::System::PacketBufferHandle buffer = chip::MessagePacketBuffer::NewWithData(PAYLOAD, sizeof(PAYLOAD)); + NL_TEST_ASSERT(inSuite, !buffer.IsNull()); + + CHIP_ERROR err = CHIP_NO_ERROR; + + MockAppDelegate mockReceiver; + err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Echo::MsgType::EchoRequest, &mockReceiver); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + mockReceiver.mTestSuite = inSuite; + + MockAppDelegate mockSender; + ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockSender); + NL_TEST_ASSERT(inSuite, exchange != nullptr); + + ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); + ReliableMessageContext * rc = exchange->GetReliableMessageContext(); + NL_TEST_ASSERT(inSuite, rm != nullptr); + NL_TEST_ASSERT(inSuite, rc != nullptr); + + rc->SetConfig({ + 1, // CHIP_CONFIG_RMP_DEFAULT_INITIAL_RETRY_INTERVAL + 1, // CHIP_CONFIG_RMP_DEFAULT_ACTIVE_RETRY_INTERVAL + }); + + mockReceiver.mDropAckResponse = true; + + // Let's not drop any messages. We'll drop acks for this test. + gLoopback.mSendMessageCount = 0; + gLoopback.mNumMessagesToDrop = 0; + gLoopback.mDroppedMessageCount = 0; + + // Ensure the retransmit table is empty right now + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); + + err = exchange->SendMessage(Echo::MsgType::EchoRequest, std::move(buffer)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + exchange->Close(); + + // Ensure the message was not dropped, and was added to retransmit table + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 1); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); + NL_TEST_ASSERT(inSuite, mockReceiver.IsOnMessageReceivedCalled); + + // 1 tick is 64 ms, sleep 65 ms to trigger first re-transmit + test_os_sleep_ms(65); + ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the retransmit message was also not dropped, and is still there in the retransmit table + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); + NL_TEST_ASSERT(inSuite, mockReceiver.IsOnMessageReceivedCalled); + + // Let's not drop the ack on the next retry + mockReceiver.mDropAckResponse = false; + + // 1 tick is 64 ms, sleep 65 ms to trigger first re-transmit + test_os_sleep_ms(65); + ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the message was retransmitted, and is no longer in the retransmit table + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 0); + + // TODO - Enable test for lost CRMP ack messages + // The following check is commented out because of https://github.com/project-chip/connectedhomeip/issues/7292 + // NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); + NL_TEST_ASSERT(inSuite, mockReceiver.IsOnMessageReceivedCalled); + + mockReceiver.mTestSuite = nullptr; + + err = ctx.GetExchangeManager().UnregisterUnsolicitedMessageHandlerForType(Echo::MsgType::EchoRequest); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + rm->ClearRetransTable(rc); +} + void CheckResendSessionEstablishmentMessageWithPeerExchange(nlTestSuite * inSuite, void * inContext) { // Making this static to reduce stack usage, as some platforms have limits on stack size. @@ -609,6 +698,7 @@ const nlTest sTests[] = NL_TEST_DEF("Test ReliableMessageMgr::CheckCloseExchangeAndResendApplicationMessage", CheckCloseExchangeAndResendApplicationMessage), NL_TEST_DEF("Test ReliableMessageMgr::CheckFailedMessageRetainOnSend", CheckFailedMessageRetainOnSend), NL_TEST_DEF("Test ReliableMessageMgr::CheckResendApplicationMessageWithPeerExchange", CheckResendApplicationMessageWithPeerExchange), + NL_TEST_DEF("Test ReliableMessageMgr::CheckResendApplicationMessageWithLostAcks", CheckResendApplicationMessageWithLostAcks), NL_TEST_DEF("Test ReliableMessageMgr::CheckResendSessionEstablishmentMessageWithPeerExchange", CheckResendSessionEstablishmentMessageWithPeerExchange), NL_TEST_DEF("Test ReliableMessageMgr::CheckSendStandaloneAckMessage", CheckSendStandaloneAckMessage),