Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #include <sdbusplus/asio/object_server.hpp> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 2 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 3 | |
| 4 | #include <future> |
| 5 | #include <thread> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | |
| 9 | class DbusEnvironment : public ::testing::Environment |
| 10 | { |
| 11 | public: |
| 12 | ~DbusEnvironment(); |
| 13 | |
| 14 | void SetUp() override; |
| 15 | void TearDown() override; |
| 16 | void teardown(); |
| 17 | |
| 18 | static boost::asio::io_context& getIoc(); |
| 19 | static std::shared_ptr<sdbusplus::asio::connection> getBus(); |
| 20 | static std::shared_ptr<sdbusplus::asio::object_server> getObjServer(); |
| 21 | static const char* serviceName(); |
| 22 | static std::function<void()> setPromise(std::string_view name); |
| 23 | static void sleepFor(std::chrono::milliseconds); |
| 24 | static std::chrono::milliseconds measureTime(std::function<void()>); |
| 25 | |
| 26 | static void synchronizeIoc() |
| 27 | { |
| 28 | while (ioc.poll() > 0) |
| 29 | { |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | template <class Functor> |
| 34 | static void synchronizedPost(Functor&& functor) |
| 35 | { |
| 36 | boost::asio::post(ioc, std::forward<Functor>(functor)); |
| 37 | synchronizeIoc(); |
| 38 | } |
| 39 | |
| 40 | template <class T> |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 41 | static T waitForFuture( |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 42 | std::future<T> future, |
| 43 | std::chrono::milliseconds timeout = std::chrono::seconds(10)) |
| 44 | { |
| 45 | constexpr auto precission = std::chrono::milliseconds(10); |
| 46 | auto elapsed = std::chrono::milliseconds(0); |
| 47 | |
| 48 | while (future.valid() && elapsed < timeout) |
| 49 | { |
| 50 | synchronizeIoc(); |
| 51 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 52 | if (future.wait_for(precission) == std::future_status::ready) |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 53 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 54 | return future.get(); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 55 | } |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 56 | else |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 57 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 58 | elapsed += precission; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 62 | throw std::runtime_error("Timed out while waiting for future"); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static bool waitForFuture( |
| 66 | std::string_view name, |
| 67 | std::chrono::milliseconds timeout = std::chrono::seconds(10)); |
| 68 | |
| 69 | private: |
| 70 | static std::future<bool> getFuture(std::string_view name); |
| 71 | |
| 72 | static boost::asio::io_context ioc; |
| 73 | static std::shared_ptr<sdbusplus::asio::connection> bus; |
| 74 | static std::shared_ptr<sdbusplus::asio::object_server> objServer; |
| 75 | static std::map<std::string, std::vector<std::future<bool>>> futures; |
| 76 | static bool setUp; |
| 77 | }; |