Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 3 | #include "types/duration_types.hpp" |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 4 | #include "utils/set_exception.hpp" |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 5 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 6 | #include <sdbusplus/asio/object_server.hpp> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 7 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 8 | |
| 9 | #include <future> |
| 10 | #include <thread> |
| 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | |
| 14 | class DbusEnvironment : public ::testing::Environment |
| 15 | { |
| 16 | public: |
| 17 | ~DbusEnvironment(); |
| 18 | |
| 19 | void SetUp() override; |
| 20 | void TearDown() override; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 21 | |
| 22 | static boost::asio::io_context& getIoc(); |
| 23 | static std::shared_ptr<sdbusplus::asio::connection> getBus(); |
| 24 | static std::shared_ptr<sdbusplus::asio::object_server> getObjServer(); |
| 25 | static const char* serviceName(); |
| 26 | static std::function<void()> setPromise(std::string_view name); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 27 | static void sleepFor(Milliseconds); |
| 28 | static Milliseconds measureTime(std::function<void()>); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 29 | |
| 30 | static void synchronizeIoc() |
| 31 | { |
| 32 | while (ioc.poll() > 0) |
Patrick Williams | 92cfff4 | 2021-02-22 17:15:39 -0600 | [diff] [blame] | 33 | {} |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | template <class Functor> |
| 37 | static void synchronizedPost(Functor&& functor) |
| 38 | { |
| 39 | boost::asio::post(ioc, std::forward<Functor>(functor)); |
| 40 | synchronizeIoc(); |
| 41 | } |
| 42 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 43 | template <class T, class F> |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 44 | static T waitForFutures(std::vector<std::future<T>> futures, T init, |
| 45 | F&& accumulator, |
| 46 | Milliseconds timeout = std::chrono::seconds(10)) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 48 | constexpr auto precission = Milliseconds(10); |
| 49 | auto elapsed = Milliseconds(0); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 50 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 51 | auto sum = init; |
| 52 | for (auto& future : futures) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 53 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 54 | while (future.valid() && elapsed < timeout) |
| 55 | { |
| 56 | synchronizeIoc(); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 57 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 58 | if (future.wait_for(precission) == std::future_status::ready) |
| 59 | { |
| 60 | sum = accumulator(sum, future.get()); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | elapsed += precission; |
| 65 | } |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 69 | if (elapsed >= timeout) |
| 70 | { |
| 71 | throw std::runtime_error("Timed out while waiting for future"); |
| 72 | } |
| 73 | |
| 74 | return sum; |
| 75 | } |
| 76 | |
| 77 | template <class T> |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 78 | static T waitForFuture(std::future<T> future, |
| 79 | Milliseconds timeout = std::chrono::seconds(10)) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 80 | { |
| 81 | std::vector<std::future<T>> futures; |
| 82 | futures.emplace_back(std::move(future)); |
| 83 | |
Patrick Williams | f535cad | 2024-08-16 15:21:20 -0400 | [diff] [blame] | 84 | return waitForFutures(std::move(futures), T{}, |
| 85 | [](auto, const auto& value) { return value; }, |
| 86 | timeout); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 89 | static bool waitForFuture(std::string_view name, |
| 90 | Milliseconds timeout = std::chrono::seconds(10)); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 91 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 92 | static bool waitForFutures(std::string_view name, |
| 93 | Milliseconds timeout = std::chrono::seconds(10)); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 94 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 95 | template <class T> |
| 96 | static T getProperty(const std::string& path, |
| 97 | const std::string& interfaceName, |
| 98 | const std::string& property) |
| 99 | { |
| 100 | auto propertyPromise = std::promise<T>(); |
| 101 | auto propertyFuture = propertyPromise.get_future(); |
| 102 | sdbusplus::asio::getProperty<T>( |
| 103 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 104 | interfaceName, property, |
| 105 | [&propertyPromise](const boost::system::error_code& ec, T t) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 106 | if (ec) |
| 107 | { |
| 108 | utils::setException(propertyPromise, "GetProperty failed"); |
| 109 | return; |
| 110 | } |
| 111 | propertyPromise.set_value(t); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 112 | }); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 113 | return DbusEnvironment::waitForFuture(std::move(propertyFuture)); |
| 114 | } |
| 115 | |
| 116 | template <class T> |
| 117 | static boost::system::error_code |
| 118 | setProperty(const std::string& path, const std::string& interfaceName, |
| 119 | const std::string& property, const T& newValue) |
| 120 | { |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 121 | auto promise = std::promise<boost::system::error_code>(); |
| 122 | auto future = promise.get_future(); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 123 | sdbusplus::asio::setProperty( |
| 124 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 125 | interfaceName, property, std::move(newValue), |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 126 | [promise = std::move(promise)]( |
| 127 | boost::system::error_code ec) mutable { |
| 128 | promise.set_value(ec); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 129 | }); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 130 | return DbusEnvironment::waitForFuture(std::move(future)); |
| 131 | } |
| 132 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 133 | template <class... Args> |
| 134 | static boost::system::error_code |
| 135 | callMethod(const std::string& path, const std::string& interface, |
| 136 | const std::string& method, Args&&... args) |
| 137 | { |
| 138 | auto promise = std::promise<boost::system::error_code>(); |
| 139 | auto future = promise.get_future(); |
| 140 | DbusEnvironment::getBus()->async_method_call( |
| 141 | [promise = std::move(promise)]( |
| 142 | boost::system::error_code ec) mutable { |
| 143 | promise.set_value(ec); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 144 | }, |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 145 | DbusEnvironment::serviceName(), path, interface, method, |
| 146 | std::forward<Args>(args)...); |
| 147 | return DbusEnvironment::waitForFuture(std::move(future)); |
| 148 | } |
| 149 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 150 | private: |
| 151 | static std::future<bool> getFuture(std::string_view name); |
| 152 | |
| 153 | static boost::asio::io_context ioc; |
| 154 | static std::shared_ptr<sdbusplus::asio::connection> bus; |
| 155 | static std::shared_ptr<sdbusplus::asio::object_server> objServer; |
| 156 | static std::map<std::string, std::vector<std::future<bool>>> futures; |
| 157 | static bool setUp; |
| 158 | }; |