From fbb4608d77dbcb52fe40bf35126cba76edb22178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?= <francois-simon.fauteux-chapleau@savoirfairelinux.com> Date: Mon, 15 Apr 2024 10:00:25 -0400 Subject: [PATCH] tests: don't sleep for 5 seconds in IceTest::setUp We do need to wait for the DHT's public address to become available after bootstrapping it before we can start the tests, but 5 seconds is much longer than typically necessary. Change-Id: I1c1094eafbe9c81767439009ba751b69cae24d08 --- tests/ice.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/ice.cpp b/tests/ice.cpp index 32928af..c2c1304 100644 --- a/tests/ice.cpp +++ b/tests/ice.cpp @@ -26,6 +26,7 @@ #include <asio/io_context.hpp> #include "opendht/dhtrunner.h" +#include "opendht/sockaddr.h" #include "opendht/thread_pool.h" #include "test_runner.h" #include "upnp/upnp_context.h" @@ -82,9 +83,22 @@ IceTest::setUp() dht_ = std::make_shared<dht::DhtRunner>(); dht::DhtRunner::Config config {}; dht::DhtRunner::Context context {}; + + std::mutex mtx; + std::unique_lock lk(mtx); + std::condition_variable cv; + context.publicAddressChangedCb = [&](std::vector<dht::SockAddr> addr) { + if (addr.size() != 0) + cv.notify_all(); + }; + dht_->run(0, config, std::move(context)); dht_->bootstrap("bootstrap.jami.net:4222"); - std::this_thread::sleep_for(std::chrono::seconds(5)); + // Wait for the DHT's public address to be available, otherwise the assertion that + // `addr4.size() != 0` at the beginning of several of the tests will fail. + cv.wait_for(lk, std::chrono::seconds(5), [&] { + return dht_->getPublicAddress().size() != 0; + }); } if (!turnV4_) { turnV4_ = std::make_unique<dhtnet::IpAddr>("turn.jami.net", AF_INET); -- GitLab