From 1933202333574fa44f5baceb0d24ebada80483a2 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Fri, 21 May 2021 23:57:03 -0700 Subject: [PATCH] Update CRMP test to wait for ack and check retransmit table (#7008) * Update CRMP test to wait for ack and check retransmit table * address review comments --- src/messaging/ReliableMessageMgr.cpp | 6 +-- .../tests/TestReliableMessageProtocol.cpp | 48 ++++++++++++++----- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/messaging/ReliableMessageMgr.cpp b/src/messaging/ReliableMessageMgr.cpp index f60c3797f909e6..686d2bc60111b7 100644 --- a/src/messaging/ReliableMessageMgr.cpp +++ b/src/messaging/ReliableMessageMgr.cpp @@ -134,13 +134,14 @@ void ReliableMessageMgr::ExecuteActions() continue; uint8_t sendCount = entry.sendCount; + uint32_t msgId = entry.retainedBuf.GetMsgId(); if (sendCount == CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS) { err = CHIP_ERROR_MESSAGE_NOT_ACKNOWLEDGED; ChipLogError(ExchangeManager, "Failed to Send CHIP MsgId:%08" PRIX32 " sendCount: %" PRIu8 " max retries: %" PRIu8, - entry.retainedBuf.GetMsgId(), sendCount, CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS); + msgId, sendCount, CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS); // Remove from Table ClearRetransTable(entry); @@ -155,8 +156,7 @@ void ReliableMessageMgr::ExecuteActions() // If the retransmission was successful, update the passive timer entry.nextRetransTimeTick = static_cast(rc->GetActiveRetransmitTimeoutTick()); #if !defined(NDEBUG) - ChipLogDetail(ExchangeManager, "Retransmit MsgId:%08" PRIX32 " Send Cnt %d", entry.retainedBuf.GetMsgId(), - entry.sendCount); + ChipLogDetail(ExchangeManager, "Retransmit MsgId:%08" PRIX32 " Send Cnt %d", msgId, entry.sendCount); #endif } } diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 52747e8bdab4c2..b45dfd2c8173aa 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -57,25 +57,36 @@ TestContext sContext; const char PAYLOAD[] = "Hello!"; -int gSendMessageCount = 0; - class OutgoingTransport : public Transport::Base { public: - /// Transports are required to have a constructor that takes exactly one argument - CHIP_ERROR Init(const char * unused) { return CHIP_NO_ERROR; } - CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) override { - gSendMessageCount++; + mSendMessageCount++; + + if (mNumMessagesToDrop == 0) + { + System::PacketBufferHandle receivedMessage = msgBuf.CloneData(); + HandleMessageReceived(address, std::move(receivedMessage)); + } + else + { + mNumMessagesToDrop--; + mDroppedMessageCount++; + } return CHIP_NO_ERROR; } bool CanSendToPeer(const PeerAddress & address) override { return true; } + + uint32_t mNumMessagesToDrop = 0; + uint32_t mDroppedMessageCount = 0; + uint32_t mSendMessageCount = 0; }; -TransportMgr gTransportMgr; +TransportMgrBase gTransportMgr; +OutgoingTransport gLoopback; class MockAppDelegate : public ExchangeDelegate { @@ -179,20 +190,32 @@ void CheckResendMessage(nlTestSuite * inSuite, void * inContext) 1, // CHIP_CONFIG_RMP_DEFAULT_ACTIVE_RETRY_INTERVAL }); - gSendMessageCount = 0; + gLoopback.mSendMessageCount = 0; + gLoopback.mNumMessagesToDrop = 2; err = exchange->SendMessage(Echo::MsgType::EchoRequest, std::move(buffer)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 1); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 1); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); // 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); - NL_TEST_ASSERT(inSuite, gSendMessageCount == 2); + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2); + NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); // sleep another 65 ms to trigger second re-transmit test_os_sleep_ms(65); ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); - NL_TEST_ASSERT(inSuite, gSendMessageCount == 3); + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); + + ChipLogProgress(ExchangeManager, "Checking that retransmit table is empty now"); + test_os_sleep_ms(65); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); rm->ClearRetransTable(rc); exchange->Close(); @@ -257,9 +280,7 @@ int Initialize(void * aContext) if (err != CHIP_NO_ERROR) return FAILURE; - err = gTransportMgr.Init("LOOPBACK"); - if (err != CHIP_NO_ERROR) - return FAILURE; + gTransportMgr.Init(&gLoopback); auto * ctx = reinterpret_cast(aContext); err = ctx->Init(&sSuite, &gTransportMgr); @@ -268,6 +289,7 @@ int Initialize(void * aContext) return FAILURE; } + gTransportMgr.SetSecureSessionMgr(&ctx->GetSecureSessionManager()); return SUCCESS; }