tests: fix flakiness in MctpUtil tests - Replaced static sleep(3) in SetUp with dynamic system() wait loop for mock mctpd to claim name. - Added 100ms delay after setupMctpEndpointListener to allow D-Bus match rules to register before triggering events. - This resolves flakiness in MctpReactorDevice removal and destruction notification tests. Google-Bug-Id: 509971428 TAG=agy CONV=20b74d17-8c22-4637-865e-fef8e20edda2 Change-Id: I56b3c191277774ee75ade8a65bc97873938de5b9
diff --git a/tests/test_MctpUtil.cpp b/tests/test_MctpUtil.cpp index cf2bfe3..0854d5f 100644 --- a/tests/test_MctpUtil.cpp +++ b/tests/test_MctpUtil.cpp
@@ -93,8 +93,36 @@ // clang-format on exit(1); } - // Give mock some time to start and claim names! - sleep(3); // NOLINT + // Wait for mock to claim names + bool ready = false; + for (int i = 0; i < 50; i++) + { + std::string cmd = + "busctl call org.freedesktop.DBus /org/freedesktop/DBus " + "org.freedesktop.DBus GetNameOwner s " + "au.com.codeconstruct.MCTP1 > /dev/null 2>&1"; + const char* dbusAddr = std::getenv("DBUS_SESSION_BUS_ADDRESS"); + if (dbusAddr != nullptr) + { + size_t pos = cmd.find("busctl "); + if (pos != std::string::npos) + { + cmd.replace(pos, 7, + "busctl --address=" + std::string(dbusAddr) + + " "); + } + } + int rc = system(cmd.c_str()); // NOLINT(cert-env33-c) + if (rc != -1 && WIFEXITED(rc) && WEXITSTATUS(rc) == 0) + { + ready = true; + break; + } + usleep(100000); // 100ms + } + + ASSERT_TRUE(ready) + << "Mock mctpd service au.com.codeconstruct.MCTP1 not ready!"; } void TearDown() override @@ -1150,6 +1178,7 @@ "/xyz/openbmc_project/inventory/system/board/MockBoard/mctp_device"; setupMctpEndpointListener(conn, MctpMessageType::NVME_MI); + usleep(100000); // 100ms for match rules to register // Trigger Add event FIRST! std::string addCmd = @@ -1276,6 +1305,7 @@ "/xyz/openbmc_project/inventory/system/board/MockBoard/mctp_device"; setupMctpEndpointListener(conn, MctpMessageType::NVME_MI); + usleep(100000); // 100ms for match rules to register // Trigger Add event FIRST! std::string addCmd =