blob: 0ddf2416857ef181919aae45cb7d4195908ea4cf [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#pragma once
2
Krzysztof Grobelny80697712021-03-04 09:49:27 +00003#include "types/duration_type.hpp"
4
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02005#include <sdbusplus/asio/object_server.hpp>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02006#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02007
8#include <future>
9#include <thread>
10
11#include <gmock/gmock.h>
12
13class 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 Grobelny80697712021-03-04 09:49:27 +000027 static void sleepFor(Milliseconds);
28 static Milliseconds measureTime(std::function<void()>);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020029
30 static void synchronizeIoc()
31 {
32 while (ioc.poll() > 0)
Patrick Williams92cfff42021-02-22 17:15:39 -060033 {}
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020034 }
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 Dompkef763c9e2021-03-12 09:19:22 +010043 template <class T, class F>
Krzysztof Grobelny80697712021-03-04 09:49:27 +000044 static T waitForFutures(std::vector<std::future<T>> futures, T init,
45 F&& accumulator,
46 Milliseconds timeout = std::chrono::seconds(10))
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020047 {
Krzysztof Grobelny80697712021-03-04 09:49:27 +000048 constexpr auto precission = Milliseconds(10);
49 auto elapsed = Milliseconds(0);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020050
Szymon Dompkef763c9e2021-03-12 09:19:22 +010051 auto sum = init;
52 for (auto& future : futures)
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020053 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +010054 while (future.valid() && elapsed < timeout)
55 {
56 synchronizeIoc();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020057
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058 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 Grobelnyb5645942020-09-29 11:52:45 +020066 }
67 }
68
Szymon Dompkef763c9e2021-03-12 09:19:22 +010069 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 Grobelny80697712021-03-04 09:49:27 +000078 static T waitForFuture(std::future<T> future,
79 Milliseconds timeout = std::chrono::seconds(10))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010080 {
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 Grobelnyb5645942020-09-29 11:52:45 +020087 }
88
Krzysztof Grobelny80697712021-03-04 09:49:27 +000089 static bool waitForFuture(std::string_view name,
90 Milliseconds timeout = std::chrono::seconds(10));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020091
Krzysztof Grobelny80697712021-03-04 09:49:27 +000092 static bool waitForFutures(std::string_view name,
93 Milliseconds timeout = std::chrono::seconds(10));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010094
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020095 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};