Skip to content

Commit

Permalink
Fix locking scope for MDNS test.
Browse files Browse the repository at this point in the history
std::condition_variable is supposed to be able to hold the lock mutex
when exiting after wait_for. If the lock on the mutex is held throughout
the 'RunMainLoop', the condition variable can never exit.

Updated the scope of the locks.
  • Loading branch information
andy31415 committed May 12, 2021
1 parent 8185019 commit fda459f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/platform/tests/TestMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,30 @@ static const nlTest sTests[] = { NL_TEST_DEF("Test Mdns::PubSub", TestMdnsPubSub
int TestMdns()
{
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
std::condition_variable done;
int retVal = EXIT_FAILURE;

std::thread t([&mtx, &done, &retVal]() {
{
std::lock_guard<std::mutex> localLock(mtx);
nlTestSuite theSuite = { "CHIP DeviceLayer mdns tests", &sTests[0], nullptr, nullptr };

nlTestRunner(&theSuite, nullptr);
retVal = nlTestRunnerStats(&theSuite);
}
done.notify_all();

{
std::lock_guard<std::mutex> localLock(mtx);
done.notify_all();
}
});

if (done.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout)
{
fprintf(stderr, "mDNS test timeout, is avahi daemon running?");
retVal = EXIT_FAILURE;
std::unique_lock<std::mutex> lock(mtx);
if (done.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout)
{
fprintf(stderr, "mDNS test timeout, is avahi daemon running?\n");
retVal = EXIT_FAILURE;
}
}
return retVal;
}
Expand Down

0 comments on commit fda459f

Please sign in to comment.