Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 3 | #include "types/duration_type.hpp" |
| 4 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 5 | #include <sdbusplus/asio/object_server.hpp> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 6 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 7 | |
| 8 | #include <future> |
| 9 | #include <thread> |
| 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | |
| 13 | class DbusEnvironment : public ::testing::Environment |
| 14 | { |
| 15 | public: |
| 16 | ~DbusEnvironment(); |
| 17 | |
| 18 | void SetUp() override; |
| 19 | void TearDown() override; |
| 20 | void teardown(); |
| 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 | |
| 84 | return waitForFutures( |
| 85 | std::move(futures), T{}, |
| 86 | [](auto, const auto& value) { return value; }, 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 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 95 | private: |
| 96 | static std::future<bool> getFuture(std::string_view name); |
| 97 | |
| 98 | static boost::asio::io_context ioc; |
| 99 | static std::shared_ptr<sdbusplus::asio::connection> bus; |
| 100 | static std::shared_ptr<sdbusplus::asio::object_server> objServer; |
| 101 | static std::map<std::string, std::vector<std::future<bool>>> futures; |
| 102 | static bool setUp; |
| 103 | }; |