From 8dafff9ed71a3608a4c4b331c82364fdbcf08905 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 25 Apr 2023 16:28:17 +0200 Subject: [PATCH] Use Setup/Teardown for DNS-SD test suite --- src/platform/tests/TestDnssd.cpp | 129 ++++++++++--------------------- 1 file changed, 41 insertions(+), 88 deletions(-) diff --git a/src/platform/tests/TestDnssd.cpp b/src/platform/tests/TestDnssd.cpp index 1b2fe5078b58de..a5b3407e331b62 100644 --- a/src/platform/tests/TestDnssd.cpp +++ b/src/platform/tests/TestDnssd.cpp @@ -102,19 +102,26 @@ static void HandleBrowse(void * context, DnssdService * services, size_t service } } -static void HandlePublish(void * context, const char * type, const char * instanceName, CHIP_ERROR error) {} +static void DnssdErrorCallback(void * context, CHIP_ERROR error) +{ + auto * ctx = static_cast(context); + NL_TEST_ASSERT(ctx->mTestSuite, error == CHIP_NO_ERROR); +} -static void InitCallback(void * context, CHIP_ERROR error) +static void HandlePublish(void * context, const char * type, const char * instanceName, CHIP_ERROR error) { - auto * ctx = static_cast(context); - auto * suite = ctx->mTestSuite; + auto * ctx = static_cast(context); + NL_TEST_ASSERT(ctx->mTestSuite, error == CHIP_NO_ERROR); +} - DnssdService service; - TextEntry entry; - char key[] = "key"; - char val[] = "val"; +static void TestDnssdPubSub_DnssdInitCallback(void * context, CHIP_ERROR error) +{ + auto * ctx = static_cast(context); + NL_TEST_ASSERT(ctx->mTestSuite, error == CHIP_NO_ERROR); + auto * suite = ctx->mTestSuite; - NL_TEST_ASSERT(suite, error == CHIP_NO_ERROR); + DnssdService service{}; + TextEntry entry{ "key", reinterpret_cast("val"), 3 }; service.mInterface = chip::Inet::InterfaceId::Null(); service.mPort = 80; @@ -123,24 +130,17 @@ static void InitCallback(void * context, CHIP_ERROR error) strcpy(service.mType, "_mock"); service.mAddressType = chip::Inet::IPAddressType::kAny; service.mProtocol = DnssdServiceProtocol::kDnssdProtocolTcp; - entry.mKey = key; - entry.mData = reinterpret_cast(val); - entry.mDataSize = strlen(reinterpret_cast(entry.mData)); service.mTextEntries = &entry; service.mTextEntrySize = 1; service.mSubTypes = nullptr; service.mSubTypeSize = 0; - NL_TEST_ASSERT(suite, ChipDnssdPublishService(&service, HandlePublish) == CHIP_NO_ERROR); + NL_TEST_ASSERT(suite, ChipDnssdPublishService(&service, HandlePublish, context) == CHIP_NO_ERROR); intptr_t browseIdentifier; - ChipDnssdBrowse("_mock", DnssdServiceProtocol::kDnssdProtocolTcp, chip::Inet::IPAddressType::kAny, - chip::Inet::InterfaceId::Null(), HandleBrowse, context, &browseIdentifier); -} - -static void ErrorCallback(void * context, CHIP_ERROR error) -{ - VerifyOrDieWithMsg(error == CHIP_NO_ERROR, DeviceLayer, "Mdns error: %" CHIP_ERROR_FORMAT "\n", error.Format()); + NL_TEST_ASSERT(suite, + ChipDnssdBrowse("_mock", DnssdServiceProtocol::kDnssdProtocolTcp, chip::Inet::IPAddressType::kAny, + chip::Inet::InterfaceId::Null(), HandleBrowse, context, &browseIdentifier) == CHIP_NO_ERROR); } void TestDnssdPubSub(nlTestSuite * inSuite, void * inContext) @@ -148,86 +148,39 @@ void TestDnssdPubSub(nlTestSuite * inSuite, void * inContext) DnssdContext context; context.mTestSuite = inSuite; - chip::Platform::MemoryInit(); - chip::DeviceLayer::PlatformMgr().InitChipStack(); - NL_TEST_ASSERT(inSuite, chip::Dnssd::ChipDnssdInit(InitCallback, ErrorCallback, &context) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, + chip::Dnssd::ChipDnssdInit(TestDnssdPubSub_DnssdInitCallback, DnssdErrorCallback, &context) == CHIP_NO_ERROR); ChipLogProgress(DeviceLayer, "Start EventLoop"); chip::DeviceLayer::PlatformMgr().RunEventLoop(); ChipLogProgress(DeviceLayer, "End EventLoop"); + + chip::Dnssd::ChipDnssdShutdown(); } static const nlTest sTests[] = { NL_TEST_DEF("Test Dnssd::PubSub", TestDnssdPubSub), NL_TEST_SENTINEL() }; -int TestDnssd() +int TestDnssd_Setup(void * inContext) { - std::mutex mtx; - - std::condition_variable readyCondition; - bool ready = false; - - std::condition_variable doneCondition; - bool done = false; - bool shutdown = false; - - int retVal = EXIT_FAILURE; - - std::thread t([&]() { - { - 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); - - { - std::lock_guard lock(mtx); - done = true; - doneCondition.notify_all(); - } - }); - - { - 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"); - - // - // This will stop the event loop above, and wait till it has actually stopped - // (i.e exited RunEventLoop()). - // - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - chip::Dnssd::ChipDnssdShutdown(); - chip::DeviceLayer::PlatformMgr().Shutdown(); - shutdown = true; - - 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; - } - } - t.join(); + VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(chip::DeviceLayer::PlatformMgr().InitChipStack() == CHIP_NO_ERROR, FAILURE); + return SUCCESS; +} - if (!shutdown) - { - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - chip::Dnssd::ChipDnssdShutdown(); - chip::DeviceLayer::PlatformMgr().Shutdown(); - } +int TestDnssd_Teardown(void * inContext) +{ + chip::DeviceLayer::PlatformMgr().Shutdown(); chip::Platform::MemoryShutdown(); + return SUCCESS; +} + +int TestDnssd() +{ + nlTestSuite theSuite = { "CHIP DeviceLayer mDNS tests", &sTests[0], TestDnssd_Setup, TestDnssd_Teardown }; - return retVal; + // Run test suit against one context. + nlTestRunner(&theSuite, nullptr); + return nlTestRunnerStats(&theSuite); } CHIP_REGISTER_TEST_SUITE(TestDnssd);