diff --git a/src/platform/tests/TestMdns.cpp b/src/platform/tests/TestMdns.cpp index e1d77e9d501335..906ffd7170b070 100644 --- a/src/platform/tests/TestMdns.cpp +++ b/src/platform/tests/TestMdns.cpp @@ -95,25 +95,57 @@ static const nlTest sTests[] = { NL_TEST_DEF("Test Mdns::PubSub", TestMdnsPubSub int TestMdns() { std::mutex mtx; - std::unique_lock lock(mtx); - std::condition_variable done; + + std::condition_variable readyCondition; + bool ready = false; + + std::condition_variable doneCondition; + bool done = false; + int retVal = EXIT_FAILURE; - std::thread t([&mtx, &done, &retVal]() { + std::thread t([&]() { { - std::lock_guard localLock(mtx); - nlTestSuite theSuite = { "CHIP DeviceLayer mdns tests", &sTests[0], nullptr, nullptr }; + std::lock_guard lock(mtx); + ready = true; + readyCondition.notify_one(); + } + + nlTestSuite theSuite = { "CHIP DeviceLayer mdns tests", &sTests[0], nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - retVal = nlTestRunnerStats(&theSuite); + nlTestRunner(&theSuite, nullptr); + retVal = nlTestRunnerStats(&theSuite); + + { + std::lock_guard lock(mtx); + done = true; + doneCondition.notify_all(); } - 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 lock(mtx); + readyCondition.wait(lock, [&] { return ready; }); + + doneCondition.wait_for(lock, std::chrono::seconds(5)); + if (!done) + { + fprintf(stderr, "mDNS test timeout, is avahi daemon running?\n"); + + chip::DeviceLayer::PlatformMgr().LockChipStack(); + chip::DeviceLayer::PlatformMgr().Shutdown(); + chip::DeviceLayer::SystemLayer.WakeSelect(); + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); + + // TODO: the above does not seem to actually reliably shut down the chip stack. + // Program will abort with core because chip thread will still run. + doneCondition.wait_for(lock, std::chrono::seconds(1)); + if (!done) + { + fprintf(stderr, "Orderly shutdown of the platform main loop failed as well.\n"); + } + retVal = EXIT_FAILURE; + } } return retVal; }