Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions score/datarouter/test/ut/ut_logging/test_unix_domain_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,27 @@ UnixDomainSockAddr MakeTempAddrAbstractFalse(std::string& name)
return UnixDomainSockAddr(name, /*isAbstract=*/false);
}

/// Block until the filesystem socket at \p path exists (i.e. bind() completed).
/// Block until the server at \p path is accepting connections (i.e. listen() completed).
/// A successful non-blocking connect() proves the server passed both bind() and listen().
/// Returns true on success, false on timeout.
bool WaitForSocketFile(const std::string& path, std::chrono::milliseconds timeout = std::chrono::milliseconds(5000))
{
constexpr auto kPollInterval = std::chrono::milliseconds(10);
auto deadline = std::chrono::steady_clock::now() + timeout;
while (std::chrono::steady_clock::now() < deadline)
{
struct stat st{};
if (::stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFSOCK) != 0)
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (fd >= 0)
{
return true;
sockaddr_un su{};
su.sun_family = AF_UNIX;
std::strncpy(su.sun_path, path.c_str(), sizeof(su.sun_path) - 1);
if (::connect(fd, reinterpret_cast<sockaddr*>(&su), sizeof(su)) == 0)
{
::close(fd);
return true;
}
::close(fd);
}
std::this_thread::sleep_for(kPollInterval);
}
Expand Down
Loading