diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index 4535a51dff4b1e..363b43ccb4d5cf 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -1554,6 +1554,43 @@ TEST_F(TestCommandInteraction, TestCommandSenderCommandAsyncSuccessResponseFlow) EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u); } +TEST_F(TestCommandInteraction, CommandSenderDeletedWhenResponseIsPending) +{ + + mockCommandSenderDelegate.ResetCounter(); + app::CommandSender * commandSender = Platform::New(&mockCommandSenderDelegate, &GetExchangeManager()); + + AddInvokeRequestData(commandSender); + asyncCommand = true; + + EXPECT_EQ(commandSender->SendCommandRequest(GetSessionBobToAlice()), CHIP_NO_ERROR); + + DrainAndServiceIO(); + + EXPECT_EQ(mockCommandSenderDelegate.onResponseCalledTimes, 0); + EXPECT_EQ(mockCommandSenderDelegate.onFinalCalledTimes, 0); + EXPECT_EQ(mockCommandSenderDelegate.onErrorCalledTimes, 0); + EXPECT_EQ(GetNumActiveCommandResponderObjects(), 1u); + EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 2u); + + // This is NOT deleting CommandSender in one of the callbacks, so we are not violating + // the API contract. CommandSender is deleted when no message is being processed which + // is a time that deleting CommandSender is considered safe. + Platform::Delete(commandSender); + + // Decrease CommandHandler refcount and send response + asyncCommandHandle = nullptr; + + DrainAndServiceIO(); + + EXPECT_EQ(mockCommandSenderDelegate.onResponseCalledTimes, 0); + EXPECT_EQ(mockCommandSenderDelegate.onFinalCalledTimes, 0); + EXPECT_EQ(mockCommandSenderDelegate.onErrorCalledTimes, 0); + + EXPECT_EQ(GetNumActiveCommandResponderObjects(), 0u); + EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u); +} + TEST_F(TestCommandInteraction, TestCommandSenderCommandSpecificResponseFlow) {