forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvs_channel.cpp
More file actions
261 lines (222 loc) · 7.44 KB
/
Copy pathvs_channel.cpp
File metadata and controls
261 lines (222 loc) · 7.44 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// ---------------------------------------------------------------------------
//
// Author : github.com/luncliff (luncliff@gmail.com)
// License : CC BY 4.0
//
// ---------------------------------------------------------------------------
#include <coroutine/channel.hpp>
#include <coroutine/concrt.h>
#include <CppUnitTest.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
using namespace std::literals;
using namespace std::experimental;
using namespace coro;
using namespace concrt;
template <typename T, typename M>
auto write_to(channel<T, M>& ch, T value, bool ok = false) -> no_return
{
ok = co_await ch.write(value);
Assert::IsTrue(ok);
}
template <typename T, typename M>
auto read_from(channel<T, M>& ch, T& value, bool ok = false) -> no_return
{
tie(value, ok) = co_await ch.read();
Assert::IsTrue(ok);
}
template <typename T, typename M, typename CountType>
auto write_and_count(channel<T, M>& ch, T value, //
CountType& success_count, CountType& failure_count)
-> no_return
{
bool ok = co_await ch.write(value);
// ... ??? ... // channel address is strange ...
if (ok)
success_count += 1;
else
failure_count += 1;
}
template <typename T, typename M, typename CountType>
auto read_and_count(channel<T, M>& ch, T& ref, //
CountType& success_count, CountType& failure_count)
-> no_return
{
auto [value, ok] = co_await ch.read();
if (ok == false)
{
failure_count += 1;
co_return;
}
ref = value;
success_count += 1;
}
class channel_operation_test : public TestClass<channel_operation_test>
{
using channel_type = channel<uint64_t>;
TEST_METHOD(channel_write_before_read)
{
uint64_t storage = 0;
channel_type ch{};
// Writer coroutine may suspend.(not-returned)
for (uint64_t i = 0U; i < 3; ++i)
write_to(ch, i);
for (uint64_t i = 0U; i < 3; ++i)
{
// read to `storage`
read_from(ch, storage);
// stored value is same with sent value
Assert::IsTrue(storage == i);
}
}
TEST_METHOD(channel_read_before_write)
{
uint64_t storage = 0;
channel_type ch{};
// Reader coroutine may suspend.(not-returned)
for (uint64_t i = 0U; i < 3; ++i)
// read to `storage`
read_from(ch, storage);
for (uint64_t i = 0U; i < 3; ++i)
{
write_to(ch, i);
// stored value is same with sent value
Assert::IsTrue(storage == i);
}
}
TEST_METHOD(channel_cancel_write_when_destroy)
{
uint64_t storage{};
uint32_t success{};
uint32_t failure{};
{
channel_type ch{};
for (auto i = uint64_t{}; i < 2; ++i)
read_and_count(ch, storage, success, failure);
// write more than read
for (auto i = uint64_t{}; i < 3; ++i)
write_and_count(ch, i, success, failure);
}
// channel returns false to writer when it's going to destroy
Assert::IsTrue(success == 4); // 2 read + 2 write
Assert::IsTrue(failure == 1); // 1 write
}
TEST_METHOD(channel_cancel_read_when_destroy)
{
uint64_t storage{};
uint32_t success{};
uint32_t failure{};
{
channel_type ch{};
// read more than write
for (auto i = uint64_t{}; i < 3; ++i)
read_and_count(ch, storage, success, failure);
for (auto i = uint64_t{}; i < 2; ++i)
write_and_count(ch, i, success, failure);
}
// channel returns false to reader when it's going to destroy
Assert::IsTrue(success == 4); // 2 read + 2 write
Assert::IsTrue(failure == 1); // 1 write
}
};
class channel_select_test : public TestClass<channel_select_test>
{
// it's singe thread, so mutex for channels doesn't have to be real lockable
using u32_chan_t = channel<uint32_t>;
using i32_chan_t = channel<int32_t>;
TEST_METHOD(channel_select_match_one)
{
u32_chan_t ch1{};
i32_chan_t ch2{};
write_to(ch1, 17u);
select(ch2, // empty channel. bypass
[](auto v) {
static_assert(is_same_v<decltype(v), int32_t>);
Assert::Fail(L"select on empty channel must bypass");
},
ch1, // if the channel has a writer, peek and invoke callback
[](auto v) -> no_return {
static_assert(is_same_v<decltype(v), uint32_t>);
Assert::IsTrue(v == 17u);
co_await suspend_never{};
});
}
TEST_METHOD(channel_select_no_match)
{
u32_chan_t ch1{};
i32_chan_t ch2{};
// both channels are empty. no callback execution
select(ch1,
[](auto v) {
static_assert(is_same_v<decltype(v), uint32_t>);
Assert::Fail(L"select on empty channel must bypass");
},
ch2,
[](auto v) {
static_assert(is_same_v<decltype(v), int32_t>);
Assert::Fail(L"select on empty channel must bypass");
});
}
TEST_METHOD(channel_select_match_both)
{
u32_chan_t ch1{};
i32_chan_t ch2{};
bool ch1_ok = false;
bool ch2_ok = false;
write_to(ch1, 17u);
write_to(ch2, 15);
// both callback will be executed
select(ch2, [&](auto v) { ch2_ok = (v == 15); }, //
ch1, [&](auto v) { ch1_ok = (v == 17u); } //
);
Assert ::IsTrue(ch1_ok);
Assert ::IsTrue(ch2_ok);
}
};
class channel_race_test : public TestClass<channel_race_test>
{
using value_type = uint64_t;
using channel_type = channel<value_type, section>;
using wait_group = concrt::latch;
public:
TEST_METHOD(channel_no_leak_under_race)
{
// see <coroutine/concrt.h>
using namespace concrt;
// using 'guaranteed' lockable
// however, it doesn't guarantee coroutines' serialized execution
channel_type ch{};
uint32_t success{};
uint32_t failure{};
static constexpr size_t max_try_count = 6'000;
wait_group group{2 * max_try_count};
auto send_with_callback
= [&](channel_type& ch, value_type value) -> no_return {
co_await ptp_work{};
auto w = co_await ch.write(value);
w ? success += 1 : failure += 1;
group.count_down();
};
auto recv_with_callback = [&](channel_type& ch) -> no_return {
co_await ptp_work{};
auto [value, r] = co_await ch.read();
r ? success += 1 : failure += 1;
group.count_down();
};
// Spawn coroutines
uint64_t repeat = max_try_count;
while (repeat--)
{
recv_with_callback(ch);
send_with_callback(ch, repeat);
}
// Wait for all coroutines...
// !!! user should ensure there is no race for destroying channel !!!
group.wait();
// channel ensures the delivery for same number of send/recv
Assert::IsTrue(failure == 0);
// but the race makes the caks success != 2 * max_try_count
Assert::IsTrue(success <= 2 * max_try_count);
Assert::IsTrue(success > 0);
}
};