-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathDBusAwaitableMethodsTests.cpp
More file actions
247 lines (204 loc) · 8.04 KB
/
DBusAwaitableMethodsTests.cpp
File metadata and controls
247 lines (204 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* (C) 2016 - 2021 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
* (C) 2016 - 2026 Stanislav Angelovic <stanislav.angelovic@protonmail.com>
* (C) 2026 - Alex Cani <alexcani109@gmail.com>
*
* @file DBusAwaitableMethodsTests.cpp
*
* Created on: Mar 1, 2026
* Project: sdbus-c++
* Description: High-level D-Bus IPC C++ library based on sd-bus
*
* This file is part of sdbus-c++.
*
* sdbus-c++ is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* sdbus-c++ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
*/
#include <coroutine>
#include <cstdint>
#include <exception>
#include <future>
#include <map>
#include <string>
#include <utility>
#include <sdbus-c++/sdbus-c++.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "TestFixture.h"
#include "TestProxy.h"
using ::testing::Eq;
using namespace std::chrono_literals;
using namespace sdbus::test;
// Simple coroutine task type for testing purposes
// Uses a promise/future pair to communicate results and exceptions
// between the coroutine and the test code. Do not mistake this for
// the std::future-based async API of sdbus-c++.
template<typename T>
struct Task {
struct promise_type {
T value;
std::exception_ptr exception;
std::promise<T> completion;
std::future<T> future;
promise_type() : future(completion.get_future()) {}
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
// Lazy coroutine
std::suspend_always initial_suspend() noexcept { return {}; }
// final_suspend suspends so that the test code can retrieve the result
// or exception from the promise before the coroutine is destroyed
std::suspend_always final_suspend() noexcept
{
if (exception)
{
completion.set_exception(exception);
}
else
{
completion.set_value(std::move(value));
}
return {};
}
void return_value(T val) { value = std::move(val); }
void unhandled_exception() { exception = std::current_exception(); }
};
std::coroutine_handle<promise_type> handle;
// Ctor and rule of 5 for proper handle management
explicit Task(std::coroutine_handle<promise_type> hnd) : handle(hnd) {}
Task(Task&& other) noexcept : handle(std::exchange(other.handle, {})) {}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
if (handle) handle.destroy();
handle = std::exchange(other.handle, {});
}
return *this;
}
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
~Task() { if (handle) handle.destroy(); }
// "User API" for the test code, allows starting the task and retrieving the result or exception
void resume() { if (handle && !handle.done()) handle.resume(); }
T get() { return handle.promise().future.get(); }
};
// Specialization for void
template<>
struct Task<void> {
struct promise_type {
std::exception_ptr exception;
std::promise<void> completion;
std::future<void> future;
promise_type() : future(completion.get_future()) {}
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; } // NOLINT(readability-convert-member-functions-to-static)
std::suspend_always final_suspend() noexcept
{
if (exception)
{
completion.set_exception(exception);
}
else
{
completion.set_value();
}
return {};
}
void return_void() {}
void unhandled_exception() { exception = std::current_exception(); }
};
std::coroutine_handle<promise_type> handle;
explicit Task(std::coroutine_handle<promise_type> hnd) : handle(hnd) {}
Task(Task&& other) noexcept : handle(std::exchange(other.handle, {})) {}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
if (handle) handle.destroy();
handle = std::exchange(other.handle, {});
}
return *this;
}
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
~Task() { if (handle) handle.destroy(); }
void resume() { if (handle && !handle.done()) handle.resume(); } // NOLINT(readability-make-member-function-const)
void get() { handle.promise().future.get(); } // NOLINT(readability-make-member-function-const)
};
/*-------------------------------------*/
/* -- TEST CASES -- */
/*-------------------------------------*/
TYPED_TEST(AsyncSdbusTestObject, InvokesMethodAsynchronouslyOnClientSideWithAwaitable)
{
auto task = [](TestProxy* proxy) -> Task<uint32_t> {
co_return co_await proxy->doOperationClientSideAsync(100, sdbus::with_awaitable);
}(this->m_proxy.get());
task.resume();
ASSERT_THAT(task.get(), Eq(100));
}
TYPED_TEST(AsyncSdbusTestObject, InvokesMethodAsynchronouslyOnClientSideWithAwaitableOnBasicAPILevel)
{
auto task = [](TestProxy* proxy) -> Task<uint32_t> {
auto methodReply = co_await proxy->doOperationClientSideAsyncOnBasicAPILevel(100, sdbus::with_awaitable);
uint32_t returnValue{};
methodReply >> returnValue;
co_return returnValue;
}(this->m_proxy.get());
task.resume();
ASSERT_THAT(task.get(), Eq(100));
}
TYPED_TEST(AsyncSdbusTestObject, InvokesMethodWithLargeDataAsynchronouslyOnClientSideWithAwaitable)
{
std::map<int32_t, std::string> largeMap;
for (int32_t i = 0; i < 40'000; ++i)
largeMap.emplace(i, "This is string nr. " + std::to_string(i+1));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines) -- lambda closure has guaranteed lifetime
auto lambda = [&largeMap, this]() -> Task<std::map<int32_t, std::string>> {
co_return co_await this->m_proxy->doOperationWithLargeDataClientSideAsync(largeMap, sdbus::with_awaitable);
};
auto task = lambda();
task.resume();
ASSERT_THAT(task.get(), Eq(largeMap));
}
TYPED_TEST(AsyncSdbusTestObject, ThrowsErrorWhenClientSideAsynchronousMethodCallWithAwaitableFails)
{
auto task = [](TestProxy* proxy) -> Task<void> {
co_await proxy->doErroneousOperationClientSideAsync(sdbus::with_awaitable);
}(this->m_proxy.get());
task.resume();
ASSERT_THROW(task.get(), sdbus::Error);
}
TYPED_TEST(AsyncSdbusTestObject, AwaitableSupportsMultipleSequentialCalls)
{
auto task = [](TestProxy* proxy) -> Task<uint32_t> {
auto result1 = co_await proxy->doOperationClientSideAsync(10, sdbus::with_awaitable);
auto result2 = co_await proxy->doOperationClientSideAsync(20, sdbus::with_awaitable);
auto result3 = co_await proxy->doOperationClientSideAsync(30, sdbus::with_awaitable);
co_return result1 + result2 + result3;
}(this->m_proxy.get());
task.resume();
ASSERT_THAT(task.get(), Eq(60));
}
TYPED_TEST(AsyncSdbusTestObject, AwaitablePropagatesExceptionsCorrectly)
{
auto task = [](TestProxy* proxy) -> Task<std::string> {
try {
co_await proxy->doErroneousOperationClientSideAsync(sdbus::with_awaitable);
co_return "FAILED";
} catch (const sdbus::Error& e) {
// Verify we can inspect the exception
co_return std::string(e.getName());
}
}(this->m_proxy.get());
task.resume();
ASSERT_THAT(task.get(), ::testing::HasSubstr("Error"));
}