Skip to content

Commit

Permalink
Add unit test for deleting CommandSender when response is pending (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
tehampson authored Nov 19, 2024
1 parent 649c19f commit 5cd0c0d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,43 @@ TEST_F(TestCommandInteraction, TestCommandSenderCommandAsyncSuccessResponseFlow)
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
}

TEST_F(TestCommandInteraction, CommandSenderDeletedWhenResponseIsPending)
{

mockCommandSenderDelegate.ResetCounter();
app::CommandSender * commandSender = Platform::New<app::CommandSender>(&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)
{

Expand Down

0 comments on commit 5cd0c0d

Please sign in to comment.