-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathchannel_test.cpp
More file actions
499 lines (384 loc) · 11.8 KB
/
channel_test.cpp
File metadata and controls
499 lines (384 loc) · 11.8 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "msd/channel.hpp"
#include <gtest/gtest.h>
#include "msd/static_channel.hpp"
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <future>
#include <numeric>
#include <string>
#include <thread>
#include <type_traits>
#include <vector>
TEST(ChannelTest, Traits)
{
using type = int;
using channel = msd::channel<type>;
EXPECT_TRUE((std::is_same<channel::value_type, type>::value));
using iterator = msd::blocking_iterator<msd::channel<type>>;
EXPECT_TRUE((std::is_same<channel::iterator, iterator>::value));
EXPECT_TRUE((std::is_same<channel::size_type, std::size_t>::value));
}
TEST(ChannelTest, ConstructStaticChannel)
{
msd::static_channel<int, 10> channel;
EXPECT_EQ(channel.size(), 0);
}
TEST(ChannelTest, PushAndFetch)
{
msd::channel<int> channel;
int in = 1;
channel << in;
const int cin = 3;
channel << cin;
channel << 2 << 4;
int out = 0;
channel >> out;
EXPECT_EQ(1, out);
channel >> out;
EXPECT_EQ(3, out);
channel >> out;
EXPECT_EQ(2, out);
channel >> out;
EXPECT_EQ(4, out);
}
TEST(ChannelTest, WriteAndRead)
{
msd::channel<int> channel;
int in = 1;
EXPECT_TRUE(channel.write(in));
const int cin = 3;
EXPECT_TRUE(channel.write(cin));
channel.close();
EXPECT_FALSE(channel.write(2));
int out = 0;
EXPECT_TRUE(channel.read(out));
EXPECT_EQ(1, out);
EXPECT_TRUE(channel.read(out));
EXPECT_EQ(3, out);
EXPECT_FALSE(channel.read(out));
}
TEST(ChannelTest, PushAndFetchWithBufferedChannel)
{
msd::channel<int> channel{2};
auto push = [&channel]() {
channel << 1;
channel << 2;
channel << 3;
};
auto read = [&channel]() {
// Wait before reading to test the case where the channel is full and waiting
// for the reader to read some items.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
int out = 0;
channel >> out;
EXPECT_EQ(1, out);
channel >> out;
EXPECT_EQ(2, out);
channel >> out;
EXPECT_EQ(3, out);
};
std::thread push_thread{push};
std::thread read_thread{read};
push_thread.join();
read_thread.join();
}
TEST(ChannelTest, PushAndFetchMultiple)
{
msd::channel<std::string> channel;
std::string non_const_value{"1"};
const std::string const_value{"3"};
channel << non_const_value << std::string{"2"} << const_value << std::move(non_const_value);
std::string out{};
std::string out2{};
channel >> out;
EXPECT_EQ("1", out);
channel >> out;
EXPECT_EQ("2", out);
channel >> out >> out2;
EXPECT_EQ("3", out);
EXPECT_EQ("1", out2);
}
TEST(ChannelTest, PushByMoveAndFetch)
{
msd::channel<std::string> channel;
std::string in{"abc"};
channel << std::move(in);
channel << std::string{"def"};
std::string out{};
channel >> out;
EXPECT_EQ("abc", out);
channel >> out;
EXPECT_EQ("def", out);
}
TEST(ChannelTest, size)
{
msd::channel<int> channel;
EXPECT_EQ(0, channel.size());
int in = 1;
channel << in;
EXPECT_EQ(1, channel.size());
channel >> in;
EXPECT_EQ(0, channel.size());
}
TEST(ChannelTest, empty)
{
msd::channel<int> channel;
EXPECT_TRUE(channel.empty());
int in = 1;
channel << in;
EXPECT_FALSE(channel.empty());
channel >> in;
EXPECT_TRUE(channel.empty());
}
TEST(ChannelTest, close)
{
msd::channel<std::string> channel;
EXPECT_FALSE(channel.closed());
std::string in{"1"};
channel << in;
channel.close();
EXPECT_TRUE(channel.closed());
std::string out{};
channel >> out;
EXPECT_EQ("1", out);
EXPECT_NO_THROW(channel >> out);
EXPECT_THROW(channel << in, msd::closed_channel);
EXPECT_THROW(channel << std::move(in), msd::closed_channel);
}
TEST(ChannelTest, drained)
{
msd::channel<int> channel;
EXPECT_FALSE(channel.drained());
int in = 1;
channel << in;
channel.close();
EXPECT_FALSE(channel.drained());
int out = 0;
channel >> out;
EXPECT_EQ(1, out);
EXPECT_TRUE(channel.drained());
}
TEST(ChannelTest, Iterator)
{
msd::channel<int> channel;
channel << 1;
for (auto it = channel.begin(); it != channel.end();) {
EXPECT_EQ(1, *it);
break;
}
}
TEST(ChannelTest, Multithreading)
{
const int numbers = 10000;
const std::int64_t expected = 50005000;
constexpr std::size_t threads_to_read_from = 100;
msd::channel<int> channel{10};
std::mutex mtx_read{};
std::condition_variable cond_read{};
bool ready_to_read{};
std::atomic<std::int64_t> count_numbers{};
std::atomic<std::int64_t> sum_numbers{};
std::mutex mtx_wait{};
std::condition_variable cond_wait{};
std::atomic<std::size_t> wait_counter{threads_to_read_from};
auto worker = [&] {
// Wait until there is data on the channel
std::unique_lock<std::mutex> lock{mtx_read};
cond_read.wait(lock, [&ready_to_read] { return ready_to_read; });
// Read until all items have been read from the channel
while (count_numbers < numbers) {
int out{};
channel >> out;
sum_numbers += out;
++count_numbers;
}
--wait_counter;
cond_wait.notify_one();
};
std::vector<std::thread> threads{};
for (std::size_t i = 0U; i < threads_to_read_from; ++i) {
threads.emplace_back(worker);
}
// Send numbers to channel
for (int i = 1; i <= numbers; ++i) {
channel << i;
// Notify threads than then can start reading
if (!ready_to_read) {
ready_to_read = true;
cond_read.notify_all();
}
}
// Wait until all items have been read
std::unique_lock<std::mutex> lock{mtx_wait};
cond_wait.wait(lock, [&wait_counter]() { return wait_counter.load() == 0; });
std::for_each(threads.begin(), threads.end(), [](std::thread& thread) { thread.join(); });
EXPECT_EQ(expected, sum_numbers);
}
TEST(ChannelTest, ReadWriteClose)
{
const int numbers = 10000;
const std::int64_t expected_sum = 50005000;
constexpr std::size_t threads_to_read_from = 20;
msd::channel<int> channel{threads_to_read_from};
std::atomic<std::int64_t> sum{0};
std::atomic<std::int64_t> nums{0};
std::thread writer([&channel]() {
for (int i = 1; i <= numbers; ++i) {
channel << i;
}
channel.close();
});
std::vector<std::thread> readers;
for (std::size_t i = 0; i < threads_to_read_from; ++i) {
readers.emplace_back([&channel, &sum, &nums]() {
while (true) {
int value = 0;
if (!channel.read(value)) {
return;
}
sum += value;
++nums;
}
});
}
writer.join();
for (auto& reader : readers) {
reader.join();
}
EXPECT_EQ(sum, expected_sum);
EXPECT_EQ(nums, numbers);
}
class MovableOnly {
public:
explicit MovableOnly(int value) : value_{value} {}
MovableOnly() = default;
MovableOnly(const MovableOnly&)
{
std::cout << "Copy constructor should not be called";
std::abort();
}
MovableOnly(MovableOnly&& other) noexcept : value_{other.value_} { other.value_ = 0; }
MovableOnly& operator=(const MovableOnly& other)
{
if (this == &other) {
return *this;
}
std::cout << "Copy assignment should not be called";
std::abort();
}
MovableOnly& operator=(MovableOnly&& other) noexcept
{
if (this != &other) {
value_ = other.value_;
other.value_ = 0;
}
return *this;
}
int get_value() const { return value_; }
virtual ~MovableOnly() = default;
private:
int value_{0};
};
TEST(ChannelTest, Transform)
{
const int numbers = 100;
const int expected_sum = 5050 * 2;
std::atomic<int> sum{0};
std::atomic<int> nums{0};
msd::channel<MovableOnly> input_chan{30};
msd::channel<int> output_chan{10};
// Send to input channel
const auto writer = [&input_chan]() {
for (int i = 1; i <= numbers; ++i) {
input_chan.write(MovableOnly{i});
}
input_chan.close();
};
// Transform input channel values from movable_only to int by multiplying by 2 and write to output channel
const auto double_transformer = [&input_chan, &output_chan]() {
const auto double_value = [](const MovableOnly& value) { return value.get_value() * 2; };
#ifdef _MSC_VER
for (auto&& value : input_chan) {
output_chan.write(double_value(value));
}
// Does not work with std::transform
// -- Building for: Visual Studio 17 2022
// -- The C compiler identification is MSVC 19.43.34808.0
// -- The CXX compiler identification is MSVC 19.43.34808.0
//
// Release: does not compile - warning C4702: unreachable code
// Debug: compiles, but copies the movable_only object instead of moving it
//
// Possibilities:
// - I am doing something very wrong (see operator* in blocking_writer_iterator)
// - MSVC has a bug
// - https://github.com/ericniebler/range-v3/issues/1814
// - https://github.com/ericniebler/range-v3/issues/1762
// - Other compilers are more permissive
#else
std::transform(input_chan.begin(), input_chan.end(), msd::back_inserter(output_chan), double_value);
#endif // _MSC_VER
output_chan.close();
};
// Read from output channel
const auto reader = [&output_chan, &sum, &nums]() {
for (auto&& out : output_chan) { // blocking until channel is drained (closed and empty)
sum += out;
++nums;
}
};
// Create async tasks for reading, transforming, and writing
const auto reader_task_1 = std::async(std::launch::async, reader);
const auto reader_task_2 = std::async(std::launch::async, reader);
const auto writer_task = std::async(std::launch::async, writer);
const auto transformer_task = std::async(std::launch::async, double_transformer);
writer_task.wait();
transformer_task.wait();
reader_task_1.wait();
reader_task_2.wait();
EXPECT_EQ(sum, expected_sum);
EXPECT_EQ(nums, numbers);
}
TEST(ChannelTest, FilterAndAccumulate)
{
msd::channel<int> input_chan{10};
msd::channel<int> output_chan{10};
// Producer: send numbers on input channel
const auto producer = [&input_chan]() {
for (int i = 1; i <= 101; ++i) {
input_chan.write(i);
}
input_chan.close();
};
// Filter: take even numbers from input channel and write them to output channel
const auto filter = [&input_chan, &output_chan]() {
std::copy_if(input_chan.begin(), input_chan.end(), msd::back_inserter(output_chan),
[](int value) { return value % 2 == 0; });
output_chan.close();
};
const auto producer_task = std::async(std::launch::async, producer);
const auto filter_task = std::async(std::launch::async, filter);
// Consumer: accumulate output channel values
const int sum = std::accumulate(output_chan.begin(), output_chan.end(), 0);
producer_task.wait();
filter_task.wait();
EXPECT_EQ(sum, 2550);
}
TEST(ChannelTest, CopyToVector)
{
msd::channel<int> chan{10};
std::vector<int> results;
// Producer: write 1..4 into channel and close
const auto producer = [&]() {
std::fill_n(msd::back_inserter(chan), 4, 0);
for (int i = 1; i <= 4; ++i) {
chan.write(i);
}
chan.close();
};
producer();
// Copy from channel to vector
std::copy(chan.begin(), chan.end(), std::back_inserter(results));
EXPECT_EQ(results, std::vector<int>({0, 0, 0, 0, 1, 2, 3, 4}));
}