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