-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__test.cpp
353 lines (292 loc) · 13.1 KB
/
__test.cpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* Copyright 2024 The Kingsoft's ks-async Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "ks_future.h"
#include "ks_promise.h"
#include "ks_async_task.h"
#include "ks_notification_center.h"
#include <iostream>
#include <sstream>
#include <string>
namespace {
template <class T>
std::string _result_to_str(const ks_result<T>& result) {
if (result.is_value())
return (std::stringstream() << result.to_value()).str();
else if (result.is_error())
return (std::stringstream() << "(Err:" << result.to_error().get_code() << ")").str();
else
return "(!Fin)";
}
template <>
std::string _result_to_str(const ks_result<void>& result) {
if (result.is_value())
return "VOID";
else if (result.is_error())
return (std::stringstream() << "(Err:" << result.to_error().get_code() << ")").str();
else
return "(!Fin)";
}
template <class T>
void _output_result(const char* title, const ks_result<T>& result) {
if (result.is_value())
std::cout << title << _result_to_str(result) << " (succ)\n";
else if (result.is_error())
std::cout << title << "{ code: " << result.to_error().get_code() << " } (error)\n";
else
std::cout << title << "-- (not-completed)\n";
}
} //namespace
ks_latch g_exit_latch(0);
void test_promise() {
g_exit_latch.add(1);
std::cout << "test promise ... ";
auto promise = ks_promise<std::string>::create();
promise.get_future()
.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
std::thread([promise]() {
promise.resolve("pass");
}).join();
g_exit_latch.wait();
}
void test_post() {
g_exit_latch.add(1);
std::cout << "test post ... ";
auto future = ks_future<std::string>::post(ks_apartment::default_mta(), make_async_context(), []() {
return std::string("pass");
});
future.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
}
void test_post_pending() {
g_exit_latch.add(1);
std::cout << "test post_pending ... ";
ks_pending_trigger trigger;
auto future = ks_future<std::string>::post_pending(ks_apartment::default_mta(), make_async_context(), []() {
return std::string("pass");
}, & trigger);
future.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
trigger.start();
g_exit_latch.wait();
}
void test_post_delayed() {
g_exit_latch.add(1);
std::cout << "test post_delayed (400ms) ... ";
const auto post_time = std::chrono::steady_clock::now();
auto future = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), [post_time]() {
auto duration = std::chrono::steady_clock::now() - post_time;
int64_t real_delay = (int64_t)std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
std::stringstream ss;
ss << "pass (" << real_delay << "ms)";
return ss.str();
}, 400);
future.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
}
void test_all() {
g_exit_latch.add(1);
std::cout << "test all ... ";
auto f1 = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> std::string {
return "a";
}, 100);
auto f2 = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> std::string {
return "b";
}, 80);
auto f3 = ks_future<int>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> int {
return 3;
}, 120);
ks_future_util::all(f1, f2, f3)
.then<std::string>(ks_apartment::default_mta(), make_async_context(), [](const std::tuple<std::string, std::string, int>& valueTuple) -> std::string {
std::stringstream ss;
ss << std::get<0>(valueTuple) << std::get<1>(valueTuple) << std::get<2>(valueTuple);
return ss.str();
})
.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
}
void test_any() {
g_exit_latch.add(1);
std::cout << "test any ... ";
auto f1 = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> std::string {
return "a";
}, 100);
auto f2 = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> std::string {
return "b";
}, 80);
auto f3 = ks_future<std::string>::post_delayed(ks_apartment::default_mta(), make_async_context(), []() -> std::string {
return "c";
}, 120);
ks_future_util::any(f1, f2, f3)
.on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
_output_result("completion: ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
}
void test_future_methods() {
g_exit_latch.add(1);
std::cout << "test future ... ";
ks_future<std::string>::resolved("a")
.cast<std::string>()
.then<std::string>(ks_apartment::default_mta(), make_async_context(), [](const std::string& value) {
std::cout << "->then() ";
return value + ".then";
})
.transform<std::string>(ks_apartment::default_mta(), make_async_context(), [](const ks_result<std::string>& result) -> ks_result<std::string> {
std::cout << "->transform() ";
return result.is_value() ? ks_result<std::string>(_result_to_str(result) + ".transform") : ks_result<std::string>(result.to_error());
})
.flat_then<std::string>(ks_apartment::default_mta(), make_async_context(), [](const std::string& value) -> ks_future<std::string> {
return ks_future<std::string>::resolved(value + ".flat_then");
})
.flat_transform<std::string>(ks_apartment::default_mta(), make_async_context(), [](const ks_result<std::string>& result) -> ks_future<std::string> {
return result.is_value() ? ks_future<std::string>::resolved(_result_to_str(result) + ".flat_transform") : ks_future<std::string>::rejected(result.to_error());
})
.on_success(ks_apartment::default_mta(), make_async_context(), [](const auto& value) -> void {
std::cout << "->on_success() ";
})
.on_failure(ks_apartment::default_mta(), make_async_context(), [](const ks_error& error) -> void {
std::cout << "->on_failure() ";
})
.on_completion(ks_apartment::default_mta(), make_async_context(), [](const auto& result) -> void {
std::cout << "->on_completion() ";
_output_result("completion: ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
g_exit_latch.add(1);
ks_future<void>::resolved().cast<nothing_t>()
.cast<void>()
.then<void>(ks_apartment::default_mta(), make_async_context(), []() -> void {
std::cout << "->then(void) ";
})
.transform<void>(ks_apartment::default_mta(), make_async_context(), [](const ks_result<void>& result) -> void {
std::cout << "->transform(void) ";
})
.on_failure(ks_apartment::default_mta(), make_async_context(), [](const ks_error& error) -> void {
std::cout << "->on_failure(void) ";
})
.on_completion(ks_apartment::default_mta(), make_async_context(), [](const ks_result<void>& result) -> void {
std::cout << "->on_completion(void) ";
_output_result("complete(void): ", result);
g_exit_latch.count_down();
});
g_exit_latch.wait();
}
//void test_async_task() {
// g_exit_latch.add(1);
// std::cout << "test async-task ... ";
//
// auto a = ks_async_task<nothing_t>(nothing);
// auto b = ks_async_task<int>(1);
// auto c = ks_async_task<long, int>(ks_apartment::default_mta(), make_async_context(), [](int) -> long {return 2; });
// auto d = ks_async_task<double, int, long>(ks_apartment::default_mta(), make_async_context(), [](int, long) -> double {return 3; });
// auto e = ks_async_task<std::string, int, long, double>(ks_apartment::default_mta(), make_async_context(), [](int, long, double) -> std::string {return "done"; });
//
// c.connect(b);
// d.connect(b, c);
// e.connect(b, c, d);
//
// e.get_future()
// .on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
// _output_result("completion: ", result);
// g_exit_latch.count_down();
// });
//
// g_exit_latch.wait();
//}
void test_alive() {
g_exit_latch.add(1);
std::cout << "test alive ... ";
struct OBJ {
OBJ(int id) : m_id(id) { ++s_counter(); std::cout << "->::OBJ(id:" << m_id << ") "; }
~OBJ() { --s_counter(); std::cout << "->::~OBJ(id:" << m_id << ") "; }
_DISABLE_COPY_CONSTRUCTOR(OBJ);
static std::atomic<int>& s_counter() { static std::atomic<int> s_n(0); return s_n; }
const int m_id;
};
if (true) {
auto obj1 = std::make_shared<OBJ>(1);
ks_future<void>::post_delayed(
ks_apartment::default_mta(),
make_async_context().bind_owner(std::move(obj1)).bind_controller(nullptr),
[]() { std::cout << "->fn() "; },
100
).on_completion(ks_apartment::default_mta(), make_async_context(), [](auto& result) {
ks_future<void>::post_delayed(
ks_apartment::default_mta(), make_async_context(), []() {
g_exit_latch.count_down();
}, 100);
});
}
g_exit_latch.wait();
if (OBJ::s_counter() == 0)
std::cout << "completion: no leak (succ)\n";
else
std::cout << "completion: " << OBJ::s_counter() << " objs leak (error)\n";
}
void test_notification_center() {
g_exit_latch.add(1);
std::cout << "test notification-center ... ";
struct {} sender, observer;
ks_notification_center::default_center()->add_observer(&observer, "a.b.c.d", ks_apartment::default_mta(), make_async_context(), [](const ks_notification& notification) {
ASSERT(false);
std::cout << "a.b.c.d notification: name=" << notification.get_notification_name() << "; ";
});
ks_notification_center::default_center()->add_observer(&observer, "a.b.*", ks_apartment::default_mta(), make_async_context(), [](const ks_notification& notification) {
std::cout << "a.b.* notification: name=" << notification.get_notification_name() << "; ";
g_exit_latch.count_down();
});
ks_notification_center::default_center()->post_notification(&sender, "a.x.y.z", make_async_context(), nothing);
ks_notification_center::default_center()->post_notification(&sender, "a.b.c", make_async_context(), nothing);
g_exit_latch.wait();
ks_notification_center::default_center()->remove_observer(&observer, "a.b.c.d");
ks_notification_center::default_center()->remove_observer(&observer, "a.*");
ks_notification_center::default_center()->remove_observer(&sender);
std::cout << "completion (succ)\n";
}
int main() {
std::cout << "start ...\n";
test_promise();
test_post();
test_post_pending();
test_all();
test_any();
test_future_methods();
test_post_delayed();
//test_async_task();
test_alive();
test_notification_center();
g_exit_latch.wait();
ks_apartment::default_mta()->async_stop();
ks_apartment::background_sta()->async_stop();
ks_apartment::default_mta()->wait();
ks_apartment::background_sta()->wait();
std::cout << "end.\n";
return 0;
}