blob: a0f454e45cddcd568859e26bf0928d7ab7877fb4 [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#include "dbus_environment.hpp"
2
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02003#include "helpers.hpp"
4
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02005#include <future>
6#include <thread>
7
8DbusEnvironment::~DbusEnvironment()
9{
10 teardown();
11}
12
13void 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
26void DbusEnvironment::TearDown()
27{
28 ioc.poll();
29
30 futures.clear();
31}
32
33void DbusEnvironment::teardown()
34{
35 if (setUp == true)
36 {
37 setUp = false;
38
39 objServer = nullptr;
40 bus = nullptr;
41 }
42}
43
44boost::asio::io_context& DbusEnvironment::getIoc()
45{
46 return ioc;
47}
48
49std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::getBus()
50{
51 return bus;
52}
53
54std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::getObjServer()
55{
56 return objServer;
57}
58
59const char* DbusEnvironment::serviceName()
60{
61 return "telemetry.ut";
62}
63
64std::function<void()> DbusEnvironment::setPromise(std::string_view name)
65{
66 auto promise = std::make_shared<std::promise<bool>>();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +020067 futures[std::string(name)].emplace_back(promise->get_future());
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020068 return [p = std::move(promise)]() { p->set_value(true); };
69}
70
Krzysztof Grobelny80697712021-03-04 09:49:27 +000071bool DbusEnvironment::waitForFuture(std::string_view name, Milliseconds timeout)
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020072{
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010073 return waitForFuture(getFuture(name), timeout);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020074}
75
Szymon Dompkef763c9e2021-03-12 09:19:22 +010076bool DbusEnvironment::waitForFutures(std::string_view name,
Krzysztof Grobelny80697712021-03-04 09:49:27 +000077 Milliseconds timeout)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010078{
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 Grobelnyb5645942020-09-29 11:52:45 +020087std::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 Grobelny80697712021-03-04 09:49:27 +0000102void DbusEnvironment::sleepFor(Milliseconds timeout)
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200103{
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 Grobelny80697712021-03-04 09:49:27 +0000115Milliseconds DbusEnvironment::measureTime(std::function<void()> fun)
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200116{
117 auto begin = std::chrono::high_resolution_clock::now();
118 fun();
119 auto end = std::chrono::high_resolution_clock::now();
120
Krzysztof Grobelny80697712021-03-04 09:49:27 +0000121 return std::chrono::duration_cast<Milliseconds>(end - begin);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200122}
123
124boost::asio::io_context DbusEnvironment::ioc;
125std::shared_ptr<sdbusplus::asio::connection> DbusEnvironment::bus;
126std::shared_ptr<sdbusplus::asio::object_server> DbusEnvironment::objServer;
127std::map<std::string, std::vector<std::future<bool>>> DbusEnvironment::futures;
128bool DbusEnvironment::setUp = false;