Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
| 2 | |
| 3 | #include <future> |
| 4 | #include <thread> |
| 5 | |
| 6 | DbusEnvironment::~DbusEnvironment() |
| 7 | { |
| 8 | teardown(); |
| 9 | } |
| 10 | |
| 11 | void DbusEnvironment::SetUp() |
| 12 | { |
| 13 | if (setUp == false) |
| 14 | { |
| 15 | setUp = true; |
| 16 | |
| 17 | bus = std::make_shared<sdbusplus::asio::connection>(ioc); |
| 18 | bus->request_name(serviceName()); |
| 19 | |
| 20 | objServer = std::make_unique<sdbusplus::asio::object_server>(bus); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | void DbusEnvironment::TearDown() |
| 25 | { |
| 26 | ioc.poll(); |
| 27 | |
| 28 | futures.clear(); |
| 29 | } |
| 30 | |
| 31 | void DbusEnvironment::teardown() |
| 32 | { |
| 33 | if (setUp == true) |
| 34 | { |
| 35 | setUp = false; |
| 36 | |
| 37 | objServer = nullptr; |
| 38 | bus = nullptr; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | boost::asio::io_context& DbusEnvironment::getIoc() |
| 43 | { |
| 44 | return ioc; |
| 45 | } |
| 46 | |
| 47 | std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::getBus() |
| 48 | { |
| 49 | return bus; |
| 50 | } |
| 51 | |
| 52 | std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::getObjServer() |
| 53 | { |
| 54 | return objServer; |
| 55 | } |
| 56 | |
| 57 | const char* DbusEnvironment::serviceName() |
| 58 | { |
| 59 | return "telemetry.ut"; |
| 60 | } |
| 61 | |
| 62 | std::function<void()> DbusEnvironment::setPromise(std::string_view name) |
| 63 | { |
| 64 | auto promise = std::make_shared<std::promise<bool>>(); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 65 | futures[std::string(name)].emplace_back(promise->get_future()); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 66 | return [p = std::move(promise)]() { p->set_value(true); }; |
| 67 | } |
| 68 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 69 | bool DbusEnvironment::waitForFuture(std::string_view name, Milliseconds timeout) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 70 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 71 | return waitForFuture(getFuture(name), timeout); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 74 | bool DbusEnvironment::waitForFutures(std::string_view name, |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 75 | Milliseconds timeout) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 76 | { |
| 77 | auto& data = futures[std::string(name)]; |
| 78 | auto ret = waitForFutures( |
| 79 | std::move(data), true, [](auto sum, auto val) { return sum && val; }, |
| 80 | timeout); |
| 81 | data = std::vector<std::future<bool>>{}; |
| 82 | return ret; |
| 83 | } |
| 84 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 85 | std::future<bool> DbusEnvironment::getFuture(std::string_view name) |
| 86 | { |
| 87 | auto& data = futures[std::string(name)]; |
| 88 | auto it = data.begin(); |
| 89 | |
| 90 | if (it != data.end()) |
| 91 | { |
| 92 | auto result = std::move(*it); |
| 93 | data.erase(it); |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | return {}; |
| 98 | } |
| 99 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 100 | void DbusEnvironment::sleepFor(Milliseconds timeout) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 101 | { |
| 102 | auto end = std::chrono::high_resolution_clock::now() + timeout; |
| 103 | |
| 104 | while (std::chrono::high_resolution_clock::now() < end) |
| 105 | { |
| 106 | synchronizeIoc(); |
| 107 | std::this_thread::yield(); |
| 108 | } |
| 109 | |
| 110 | synchronizeIoc(); |
| 111 | } |
| 112 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 113 | Milliseconds DbusEnvironment::measureTime(std::function<void()> fun) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 114 | { |
| 115 | auto begin = std::chrono::high_resolution_clock::now(); |
| 116 | fun(); |
| 117 | auto end = std::chrono::high_resolution_clock::now(); |
| 118 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 119 | return std::chrono::duration_cast<Milliseconds>(end - begin); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | boost::asio::io_context DbusEnvironment::ioc; |
| 123 | std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::bus; |
| 124 | std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::objServer; |
| 125 | std::map<std::string, std::vector<std::future<bool>>> DbusEnvironment::futures; |
| 126 | bool DbusEnvironment::setUp = false; |