Skip to content

Commit

Permalink
Move IM standalone test run on a single event loop (#8174)
Browse files Browse the repository at this point in the history
* Move IM standalone test run on a single event loop

* Shutdown the platform after RunEventLoop is returned
  • Loading branch information
yufengwangca authored Jul 10, 2021
1 parent 97ab77f commit 5b690c7
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 85 deletions.
217 changes: 133 additions & 84 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <app/InteractionModelEngine.h>
#include <app/tests/integration/common.h>
#include <chrono>
#include <condition_variable>
#include <core/CHIPCore.h>
#include <mutex>
#include <platform/CHIPDeviceLayer.h>
Expand Down Expand Up @@ -86,7 +85,10 @@ enum class TestCommandResult : uint8_t

TestCommandResult gLastCommandResult = TestCommandResult::kUndefined;

std::condition_variable gCond;
void CommandRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error);
void BadCommandRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error);
void ReadRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error);
void WriteRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error);

CHIP_ERROR SendCommandRequest(chip::app::CommandSender * commandSender)
{
Expand Down Expand Up @@ -281,8 +283,6 @@ void HandleReadComplete()

printf("Read Response: %" PRIu64 "/%" PRIu64 "(%.2f%%) time=%.3fms\n", gReadRespCount, gReadCount,
static_cast<double>(gReadRespCount) * 100 / gReadCount, static_cast<double>(transitTime) / 1000);

gCond.notify_one();
}

void HandleWriteComplete()
Expand All @@ -294,8 +294,133 @@ void HandleWriteComplete()

printf("Write Response: %" PRIu64 "/%" PRIu64 "(%.2f%%) time=%.3fms\n", gWriteRespCount, gWriteCount,
static_cast<double>(gWriteRespCount) * 100 / gWriteCount, static_cast<double>(transitTime) / 1000);
}

void CommandRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error)
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (gCommandRespCount != gCommandCount)
{
printf("No response received\n");

// Set gCommandRespCount to gCommandCount to start next iteration if there is any.
gCommandRespCount = gCommandCount;
}

if (gCommandRespCount < kMaxCommandMessageCount)
{
chip::app::CommandSender * commandSender;
err = chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSender);
SuccessOrExit(err);

err = SendCommandRequest(commandSender);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to send command request with error: %s\n", chip::ErrorStr(err)));

err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, CommandRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));
}
else
{
err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, BadCommandRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));
}

exit:
if (err != CHIP_NO_ERROR)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
}
}

void BadCommandRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error)
{
// Test with invalid endpoint / cluster / command combination.
chip::app::CommandSender * commandSender;
CHIP_ERROR err = chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSender);
SuccessOrExit(err);

err = SendBadCommandRequest(commandSender);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to send bad command request with error: %s\n", chip::ErrorStr(err)));

err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, ReadRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));

exit:
if (err != CHIP_NO_ERROR)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
}
}

void ReadRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error)
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (gReadRespCount != gReadCount)
{
printf("No response received\n");

// Set gReadRespCount to gReadCount to start next iteration if there is any.
gReadRespCount = gReadCount;
}

if (gReadRespCount < kMaxReadMessageCount)
{
err = SendReadRequest();
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to send read request with error: %s\n", chip::ErrorStr(err)));

err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, ReadRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));
}
else
{
err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, WriteRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));
}

exit:
if (err != CHIP_NO_ERROR)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
}
}

void WriteRequestTimerHandler(chip::System::Layer * systemLayer, void * appState, CHIP_ERROR error)
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (gWriteRespCount != gWriteCount)
{
printf("No response received\n");

gCond.notify_one();
// Set gWriteRespCount to gWriteCount to start next iteration if there is any.
gWriteRespCount = gWriteCount;
}

if (gWriteRespCount < kMaxWriteMessageCount)
{
chip::app::WriteClient * writeClient;
err = chip::app::InteractionModelEngine::GetInstance()->NewWriteClient(&writeClient);
SuccessOrExit(err);

err = SendWriteRequest(writeClient);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to send write request with error: %s\n", chip::ErrorStr(err)));

err = chip::DeviceLayer::SystemLayer.StartTimer(gMessageIntervalSeconds * 1000, WriteRequestTimerHandler, NULL);
VerifyOrExit(err == CHIP_NO_ERROR, printf("Failed to schedule timer with error: %s\n", chip::ErrorStr(err)));
}
else
{
// Complete all tests.
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
}

exit:
if (err != CHIP_NO_ERROR)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
}
}

class MockInteractionModelApp : public chip::app::InteractionModelDelegate
Expand Down Expand Up @@ -346,7 +471,6 @@ class MockInteractionModelApp : public chip::app::InteractionModelDelegate

printf("Command Response: %" PRIu64 "/%" PRIu64 "(%.2f%%) time=%.3fms\n", gCommandRespCount, gCommandCount,
static_cast<double>(gCommandRespCount) * 100 / gCommandCount, static_cast<double>(transitTime) / 1000);
gCond.notify_one();
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -439,8 +563,6 @@ int main(int argc, char * argv[])

InitializeChip();

chip::DeviceLayer::PlatformMgr().StartEventLoopTask();

err = gTransportManager.Init(chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer)
.SetAddressType(chip::Inet::kIPAddressType_IPv4)
.SetListenPort(IM_CLIENT_PORT));
Expand All @@ -463,86 +585,13 @@ int main(int argc, char * argv[])
err = EstablishSecureSession();
SuccessOrExit(err);

// Connection has been established. Now send the CommandRequests.
for (unsigned int i = 0; i < kMaxCommandMessageCount; i++)
{
chip::app::CommandSender * commandSender;
err = chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSender);
SuccessOrExit(err);
err = SendCommandRequest(commandSender);
if (err != CHIP_NO_ERROR)
{
printf("Send command request failed: %s\n", chip::ErrorStr(err));
ExitNow();
}

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("Invoke Command: No response received\n");
}

VerifyOrExit(gLastCommandResult == TestCommandResult::kSuccess, err = CHIP_ERROR_INCORRECT_STATE);
}

// Test with invalid endpoint / cluster / command combination.
{
chip::app::CommandSender * commandSender;
err = chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSender);
SuccessOrExit(err);
err = SendBadCommandRequest(commandSender);
if (err != CHIP_NO_ERROR)
{
printf("Send command request failed: %s\n", chip::ErrorStr(err));
ExitNow();
}

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("Invoke Command: No response received\n");
}

VerifyOrExit(gLastCommandResult == TestCommandResult::kFailure, err = CHIP_ERROR_INCORRECT_STATE);
}

// Connection has been established. Now send the ReadRequests.
for (unsigned int i = 0; i < kMaxReadMessageCount; i++)
{
err = SendReadRequest();
if (err != CHIP_NO_ERROR)
{
printf("Send read request failed: %s\n", chip::ErrorStr(err));
goto exit;
}

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("read request: No response received\n");
}
}

// Connection has been established. Now send the ReadRequests.
for (unsigned int i = 0; i < kMaxWriteMessageCount; i++)
{
chip::app::WriteClient * writeClient;
err = chip::app::InteractionModelEngine::GetInstance()->NewWriteClient(&writeClient);
SuccessOrExit(err);
err = SendWriteRequest(writeClient);

if (err != CHIP_NO_ERROR)
{
printf("Send write request failed: %s\n", chip::ErrorStr(err));
goto exit;
}
err = chip::DeviceLayer::SystemLayer.StartTimer(0, CommandRequestTimerHandler, NULL);
SuccessOrExit(err);

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("write request: No response received\n");
}
}
chip::DeviceLayer::PlatformMgr().RunEventLoop();

chip::app::InteractionModelEngine::GetInstance()->Shutdown();
ShutdownChip();

exit:
if (err != CHIP_NO_ERROR || (gCommandRespCount != kMaxCommandMessageCount + kTotalFailureCommandMessageCount))
{
Expand Down
1 change: 0 additions & 1 deletion src/app/tests/integration/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void InitializeChip(void)

void ShutdownChip(void)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
chip::DeviceLayer::PlatformMgr().Shutdown();
gMessageCounterManager.Shutdown();
gExchangeManager.Shutdown();
Expand Down

0 comments on commit 5b690c7

Please sign in to comment.