#include "dbus_environment.hpp" #include "helpers.hpp" #include #include DbusEnvironment::~DbusEnvironment() { if (setUp == true) { setUp = false; objServer = nullptr; bus = nullptr; } } void DbusEnvironment::SetUp() { if (setUp == false) { setUp = true; bus = std::make_shared(ioc); bus->request_name(serviceName()); objServer = std::make_unique(bus); } } void DbusEnvironment::TearDown() { ioc.poll(); futures.clear(); } boost::asio::io_context& DbusEnvironment::getIoc() { return ioc; } std::shared_ptr DbusEnvironment::getBus() { return bus; } std::shared_ptr DbusEnvironment::getObjServer() { return objServer; } const char* DbusEnvironment::serviceName() { return "telemetry.ut"; } std::function DbusEnvironment::setPromise(std::string_view name) { auto promise = std::make_shared>(); futures[std::string(name)].emplace_back(promise->get_future()); return [p = std::move(promise)]() { p->set_value(true); }; } bool DbusEnvironment::waitForFuture(std::string_view name, Milliseconds timeout) { return waitForFuture(getFuture(name), timeout); } bool DbusEnvironment::waitForFutures(std::string_view name, Milliseconds timeout) { auto& data = futures[std::string(name)]; auto ret = waitForFutures(std::move(data), true, [](auto sum, auto val) { return sum && val; }, timeout); data = std::vector>{}; return ret; } std::future DbusEnvironment::getFuture(std::string_view name) { auto& data = futures[std::string(name)]; auto it = data.begin(); if (it != data.end()) { auto result = std::move(*it); data.erase(it); return result; } return {}; } void DbusEnvironment::sleepFor(Milliseconds timeout) { auto end = std::chrono::high_resolution_clock::now() + timeout; while (std::chrono::high_resolution_clock::now() < end) { synchronizeIoc(); std::this_thread::yield(); } synchronizeIoc(); } Milliseconds DbusEnvironment::measureTime(std::function fun) { auto begin = std::chrono::high_resolution_clock::now(); fun(); auto end = std::chrono::high_resolution_clock::now(); return std::chrono::duration_cast(end - begin); } boost::asio::io_context DbusEnvironment::ioc; std::shared_ptr DbusEnvironment::bus; std::shared_ptr DbusEnvironment::objServer; std::map>> DbusEnvironment::futures; bool DbusEnvironment::setUp = false;