#include #include #include #include #include class DbusEnvironment : public ::testing::Environment { public: ~DbusEnvironment(); void SetUp() override; void TearDown() override; void teardown(); static boost::asio::io_context& getIoc(); static std::shared_ptr getBus(); static std::shared_ptr getObjServer(); static const char* serviceName(); static std::function setPromise(std::string_view name); static void sleepFor(std::chrono::milliseconds); static std::chrono::milliseconds measureTime(std::function); static void synchronizeIoc() { while (ioc.poll() > 0) { } } template static void synchronizedPost(Functor&& functor) { boost::asio::post(ioc, std::forward(functor)); synchronizeIoc(); } template static std::optional waitForFuture( std::future future, std::chrono::milliseconds timeout = std::chrono::seconds(10)) { constexpr auto precission = std::chrono::milliseconds(10); auto elapsed = std::chrono::milliseconds(0); while (future.valid() && elapsed < timeout) { synchronizeIoc(); try { if (future.wait_for(precission) == std::future_status::ready) { return future.get(); } else { elapsed += precission; } } catch (const std::future_error& e) { std::cerr << e.what() << "\n"; return {}; } } return {}; } static bool waitForFuture( std::string_view name, std::chrono::milliseconds timeout = std::chrono::seconds(10)); private: static std::future getFuture(std::string_view name); static boost::asio::io_context ioc; static std::shared_ptr bus; static std::shared_ptr objServer; static std::map>> futures; static bool setUp; };