Skip to content

Commit

Permalink
[chiptool.py] Ensure async report that came in before the test got a …
Browse files Browse the repository at this point in the history
…chance to listen for a report are dispatched properly
  • Loading branch information
vivien-apple committed Sep 22, 2023
1 parent 6612760 commit 1276275
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct InteractiveServerResult
bool mIsAsyncReport = false;
uint16_t mTimeout = 0;
int mStatus = EXIT_SUCCESS;
std::vector<std::string> mPendingResults;
std::vector<std::string> mResults;
std::vector<InteractiveServerResultLog> mLogs;

Expand Down Expand Up @@ -100,9 +101,20 @@ struct InteractiveServerResult
mIsAsyncReport = isAsyncReport;
mTimeout = timeout;

if (mIsAsyncReport && mTimeout)
if (mIsAsyncReport)
{
chip::DeviceLayer::PlatformMgr().ScheduleWork(StartAsyncTimeout, reinterpret_cast<intptr_t>(this));
if (mTimeout)
{
chip::DeviceLayer::PlatformMgr().ScheduleWork(StartAsyncTimeout, reinterpret_cast<intptr_t>(this));
}

if (!mPendingResults.empty())
{
mResults = std::move(mPendingResults);
}

// Reset the pending results.
mPendingResults.clear();
}
}

Expand All @@ -119,6 +131,7 @@ struct InteractiveServerResult
mIsAsyncReport = false;
mTimeout = 0;
mStatus = EXIT_SUCCESS;
mPendingResults.clear();
mResults.clear();
mLogs.clear();
}
Expand Down Expand Up @@ -158,11 +171,26 @@ struct InteractiveServerResult
void MaybeAddResult(const char * result)
{
auto lock = ScopedLock(mMutex);
VerifyOrReturn(mEnabled);
// If we are not waiting for a result it could be that a report came in *before* the command to wait for
// it has happened. The result is stored until the next command. If the next command is an async
// command the pending result would be promote to a valid result so it could be dispatched properly.
if (!mEnabled)
{
mPendingResults.push_back(result);
return;
}

mResults.push_back(result);
}

bool HasResults()
{
auto lock = ScopedLock(mMutex);
VerifyOrReturnValue(mEnabled, false);

return !mResults.empty();
}

std::string AsJsonString()
{
auto lock = ScopedLock(mMutex);
Expand Down Expand Up @@ -308,7 +336,16 @@ bool InteractiveServerCommand::OnWebSocketMessageReceived(char * msg)
}

gInteractiveServerResult.Setup(isAsyncReport, timeout);
VerifyOrReturnValue(!isAsyncReport, true);

if (isAsyncReport)
{
if (gInteractiveServerResult.HasResults())
{
mWebSocketServer.Send(gInteractiveServerResult.AsJsonString().c_str());
gInteractiveServerResult.Reset();
}
return true;
}

auto shouldStop = ParseCommand(msg, &gInteractiveServerResult.mStatus);
mWebSocketServer.Send(gInteractiveServerResult.AsJsonString().c_str());
Expand Down

0 comments on commit 1276275

Please sign in to comment.