From e192d50dc516dc3278a6ee29336eb57c62dab053 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Sat, 21 May 2022 04:51:35 -0400 Subject: [PATCH] Fail unit tests that leave messages sitting around undrained. Tests might be accidentally forgetting to drain their messages, and hence think messages got delivered when they did not. Fail the tests if that happens. Fixes https://github.com/project-chip/connectedhomeip/issues/17624 --- src/app/tests/TestReadInteraction.cpp | 24 ++++++++++++++++---- src/app/tests/TestReportingEngine.cpp | 3 +++ src/transport/raw/tests/NetworkTestHelpers.h | 7 +++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index f44d7972293559..e45d04869f0709 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -306,7 +306,7 @@ class TestReadInteraction static void TestSubscribeEarlyShutdown(nlTestSuite * apSuite, void * apContext); static void TestSubscribeInvalidAttributePathRoundtrip(nlTestSuite * apSuite, void * apContext); static void TestReadInvalidAttributePathRoundtrip(nlTestSuite * apSuite, void * apContext); - static void TestSubscribeInvalidIterval(nlTestSuite * apSuite, void * apContext); + static void TestSubscribeInvalidInterval(nlTestSuite * apSuite, void * apContext); static void TestReadShutdown(nlTestSuite * apSuite, void * apContext); static void TestResubscribeRoundtrip(nlTestSuite * apSuite, void * apContext); static void TestSubscribeRoundtripStatusReportTimeout(nlTestSuite * apSuite, void * apContext); @@ -411,6 +411,12 @@ void TestReadInteraction::TestReadClient(nlTestSuite * apSuite, void * apContext err = readClient.SendRequest(readPrepareParams); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + // We don't actually want to deliver that message, because we want to + // synthesize the read response. But we don't want it hanging around + // forever either. + ctx.GetLoopback().mNumMessagesToDrop = 1; + ctx.DrainAndServiceIO(); + GenerateReportData(apSuite, apContext, buf, false /*aNeedInvalidReport*/, true /* aSuppressResponse*/); err = readClient.ProcessReportData(std::move(buf)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); @@ -545,6 +551,12 @@ void TestReadInteraction::TestReadClientInvalidReport(nlTestSuite * apSuite, voi err = readClient.SendRequest(readPrepareParams); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + // We don't actually want to deliver that message, because we want to + // synthesize the read response. But we don't want it hanging around + // forever either. + ctx.GetLoopback().mNumMessagesToDrop = 1; + ctx.DrainAndServiceIO(); + GenerateReportData(apSuite, apContext, buf, true /*aNeedInvalidReport*/, true /* aSuppressResponse*/); err = readClient.ProcessReportData(std::move(buf)); @@ -1992,7 +2004,7 @@ void TestReadInteraction::TestReadShutdown(nlTestSuite * apSuite, void * apConte Platform::Delete(pClients[2]); } -void TestReadInteraction::TestSubscribeInvalidIterval(nlTestSuite * apSuite, void * apContext) +void TestReadInteraction::TestSubscribeInvalidInterval(nlTestSuite * apSuite, void * apContext) { TestContext & ctx = *static_cast(apContext); CHIP_ERROR err = CHIP_NO_ERROR; @@ -2553,6 +2565,11 @@ void TestReadInteraction::TestPostSubscribeRoundtripChunkReportTimeout(nlTestSui NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadClients() == 0); engine->Shutdown(); NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); + + // Engine shutdown seems to trigger some messages to try to be sent. Make + // sure those get flushed out. + ctx.DrainAndServiceIO(); + ctx.CreateSessionBobToAlice(); } } // namespace app @@ -2593,10 +2610,9 @@ const nlTest sTests[] = NL_TEST_DEF("TestSubscribeEarlyShutdown", chip::app::TestReadInteraction::TestSubscribeEarlyShutdown), NL_TEST_DEF("TestSubscribeInvalidAttributePathRoundtrip", chip::app::TestReadInteraction::TestSubscribeInvalidAttributePathRoundtrip), NL_TEST_DEF("TestReadInvalidAttributePathRoundtrip", chip::app::TestReadInteraction::TestReadInvalidAttributePathRoundtrip), - NL_TEST_DEF("TestSubscribeInvalidIterval", chip::app::TestReadInteraction::TestSubscribeInvalidIterval), + NL_TEST_DEF("TestSubscribeInvalidInterval", chip::app::TestReadInteraction::TestSubscribeInvalidInterval), NL_TEST_DEF("TestSubscribeRoundtripStatusReportTimeout", chip::app::TestReadInteraction::TestSubscribeRoundtripStatusReportTimeout), NL_TEST_DEF("TestPostSubscribeRoundtripStatusReportTimeout", chip::app::TestReadInteraction::TestPostSubscribeRoundtripStatusReportTimeout), - //#if 0 NL_TEST_DEF("TestReadChunkingStatusReportTimeout", chip::app::TestReadInteraction::TestReadChunkingStatusReportTimeout), NL_TEST_DEF("TestSubscribeRoundtripChunkStatusReportTimeout", chip::app::TestReadInteraction::TestSubscribeRoundtripChunkStatusReportTimeout), NL_TEST_DEF("TestPostSubscribeRoundtripChunkStatusReportTimeout", chip::app::TestReadInteraction::TestPostSubscribeRoundtripChunkStatusReportTimeout), diff --git a/src/app/tests/TestReportingEngine.cpp b/src/app/tests/TestReportingEngine.cpp index 6e66d15879a6de..f2395bea4c9f62 100644 --- a/src/app/tests/TestReportingEngine.cpp +++ b/src/app/tests/TestReportingEngine.cpp @@ -158,6 +158,9 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite app::ReadHandler readHandler(dummy, exchangeCtx, chip::app::ReadHandler::InteractionType::Read); readHandler.OnInitialRequest(std::move(readRequestbuf)); err = InteractionModelEngine::GetInstance()->GetReportingEngine().BuildAndSendSingleReportData(&readHandler); + + ctx.DrainAndServiceIO(); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); } diff --git a/src/transport/raw/tests/NetworkTestHelpers.h b/src/transport/raw/tests/NetworkTestHelpers.h index 61779bdc497f45..a0cb29643b3eeb 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.h +++ b/src/transport/raw/tests/NetworkTestHelpers.h @@ -74,10 +74,9 @@ class LoopbackTransport : public Transport::Base void InitLoopbackTransport(System::Layer * systemLayer) { mSystemLayer = systemLayer; } void ShutdownLoopbackTransport() { - // TODO: remove these after #17624 (Ensure tests drain all message in loopback transport) being fixed - // Packets are allocated from platform memory, we should release them before Platform::MemoryShutdown - while (!mPendingMessageQueue.empty()) - mPendingMessageQueue.pop(); + // Make sure no one left packets hanging out that they thought got + // delivered but actually didn't. + VerifyOrDie(mPendingMessageQueue.empty()); } /// Transports are required to have a constructor that takes exactly one argument