3
3
#include < array>
4
4
#include < chrono>
5
5
#include < iostream>
6
+ #include < memory>
7
+ #include < system_error>
6
8
#include < ylt/coro_io/coro_io.hpp>
9
+
10
+ #include " async_simple/coro/Lazy.h"
7
11
using namespace std ::chrono_literals;
8
12
9
13
#ifndef __clang__
@@ -34,18 +38,28 @@ async_simple::coro::Lazy<void> test_channel() {
34
38
CHECK (val == 42 );
35
39
}
36
40
41
+ async_simple::coro::Lazy<std::pair<std::error_code, int >> async_receive_wrapper (
42
+ std::shared_ptr<coro_io::channel<int >> ch) {
43
+ co_return co_await async_receive (*ch);
44
+ }
45
+
46
+ async_simple::coro::Lazy<void > wait_wrapper (
47
+ std::shared_ptr<coro_io::period_timer> t) {
48
+ co_await t->async_await ();
49
+ }
37
50
async_simple::coro::Lazy<void > test_select_channel () {
38
51
using namespace coro_io ;
39
52
using namespace async_simple ::coro;
40
- auto ch1 = coro_io::create_channel <int >(1000 );
41
- auto ch2 = coro_io::create_channel <int >(1000 );
53
+ auto ch1 = coro_io::create_shared_channel <int >(1000 );
54
+ auto ch2 = coro_io::create_shared_channel <int >(1000 );
42
55
43
- co_await async_send (ch1, 41 );
44
- co_await async_send (ch2, 42 );
56
+ co_await async_send (* ch1, 41 );
57
+ co_await async_send (* ch2, 42 );
45
58
46
59
std::array<int , 2 > arr{41 , 42 };
47
60
48
- auto result = co_await collectAny (async_receive (ch1), async_receive (ch2));
61
+ auto result = co_await collectAny (async_receive_wrapper (ch1),
62
+ async_receive_wrapper (ch2));
49
63
int val = std::visit (
50
64
[&val](auto & v) {
51
65
return static_cast <int >(v.value ().second );
@@ -54,22 +68,22 @@ async_simple::coro::Lazy<void> test_select_channel() {
54
68
55
69
CHECK (val == arr[result.index ()]);
56
70
57
- co_await async_send (ch1, 41 );
58
- co_await async_send (ch2, 42 );
71
+ co_await async_send (* ch1, 41 );
72
+ co_await async_send (* ch2, 42 );
59
73
60
74
std::vector<Lazy<std::pair<std::error_code, int >>> vec;
61
- vec.push_back (async_receive (ch1));
62
- vec.push_back (async_receive (ch2));
75
+ vec.push_back (async_receive_wrapper (ch1));
76
+ vec.push_back (async_receive_wrapper (ch2));
63
77
64
78
auto result2 = co_await collectAny (std::move (vec));
65
79
val = result2.value ().second ;
66
80
CHECK (val == arr[result2.index ()]);
67
81
68
- period_timer timer1 (coro_io::get_global_executor ());
69
- timer1. expires_after (100ms);
70
- period_timer timer2 (coro_io::get_global_executor ());
71
- timer2. expires_after (200ms);
72
- auto val1 = co_await collectAny (timer1. async_await ( ), timer2. async_await ( ));
82
+ auto timer1 = std::make_shared<period_timer> (coro_io::get_global_executor ());
83
+ timer1-> expires_after (100ms);
84
+ auto timer2 = std::make_shared<period_timer> (coro_io::get_global_executor ());
85
+ timer2-> expires_after (200ms);
86
+ auto val1 = co_await collectAny (wait_wrapper (timer1 ), wait_wrapper (timer2 ));
73
87
74
88
CHECK (val1.index () == 0 );
75
89
}
0 commit comments