Skip to content

Commit 5cd0c0d

Browse files
authored
Add unit test for deleting CommandSender when response is pending (project-chip#36536)
1 parent 649c19f commit 5cd0c0d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/app/tests/TestCommandInteraction.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,43 @@ TEST_F(TestCommandInteraction, TestCommandSenderCommandAsyncSuccessResponseFlow)
15541554
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
15551555
}
15561556

1557+
TEST_F(TestCommandInteraction, CommandSenderDeletedWhenResponseIsPending)
1558+
{
1559+
1560+
mockCommandSenderDelegate.ResetCounter();
1561+
app::CommandSender * commandSender = Platform::New<app::CommandSender>(&mockCommandSenderDelegate, &GetExchangeManager());
1562+
1563+
AddInvokeRequestData(commandSender);
1564+
asyncCommand = true;
1565+
1566+
EXPECT_EQ(commandSender->SendCommandRequest(GetSessionBobToAlice()), CHIP_NO_ERROR);
1567+
1568+
DrainAndServiceIO();
1569+
1570+
EXPECT_EQ(mockCommandSenderDelegate.onResponseCalledTimes, 0);
1571+
EXPECT_EQ(mockCommandSenderDelegate.onFinalCalledTimes, 0);
1572+
EXPECT_EQ(mockCommandSenderDelegate.onErrorCalledTimes, 0);
1573+
EXPECT_EQ(GetNumActiveCommandResponderObjects(), 1u);
1574+
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 2u);
1575+
1576+
// This is NOT deleting CommandSender in one of the callbacks, so we are not violating
1577+
// the API contract. CommandSender is deleted when no message is being processed which
1578+
// is a time that deleting CommandSender is considered safe.
1579+
Platform::Delete(commandSender);
1580+
1581+
// Decrease CommandHandler refcount and send response
1582+
asyncCommandHandle = nullptr;
1583+
1584+
DrainAndServiceIO();
1585+
1586+
EXPECT_EQ(mockCommandSenderDelegate.onResponseCalledTimes, 0);
1587+
EXPECT_EQ(mockCommandSenderDelegate.onFinalCalledTimes, 0);
1588+
EXPECT_EQ(mockCommandSenderDelegate.onErrorCalledTimes, 0);
1589+
1590+
EXPECT_EQ(GetNumActiveCommandResponderObjects(), 0u);
1591+
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
1592+
}
1593+
15571594
TEST_F(TestCommandInteraction, TestCommandSenderCommandSpecificResponseFlow)
15581595
{
15591596

0 commit comments

Comments
 (0)