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
|
//
// experimental/concurrent_channel.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/experimental/concurrent_channel.hpp"
#include <utility>
#include "asio/error.hpp"
#include "asio/io_context.hpp"
#include "../unit_test.hpp"
using namespace asio;
using namespace asio::experimental;
void unbuffered_concurrent_channel_test()
{
io_context ctx;
concurrent_channel<void(asio::error_code, std::string)> ch1(ctx);
ASIO_CHECK(ch1.is_open());
ASIO_CHECK(!ch1.ready());
bool b1 = ch1.try_send(asio::error::eof, "hello");
ASIO_CHECK(!b1);
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
bool b2 = ch1.try_send(asio::error::eof, std::move(s1));
ASIO_CHECK(!b2);
ASIO_CHECK(!s1.empty());
asio::error_code ec1;
std::string s2;
ch1.async_receive(
[&](asio::error_code ec, std::string s)
{
ec1 = ec;
s2 = std::move(s);
});
bool b3 = ch1.try_send(asio::error::eof, std::move(s1));
ASIO_CHECK(b3);
ASIO_CHECK(s1.empty());
ctx.run();
ASIO_CHECK(ec1 == asio::error::eof);
ASIO_CHECK(s2 == "abcdefghijklmnopqrstuvwxyz");
bool b4 = ch1.try_receive([](asio::error_code, std::string){});
ASIO_CHECK(!b4);
asio::error_code ec2 = asio::error::would_block;
std::string s3 = "zyxwvutsrqponmlkjihgfedcba";
ch1.async_send(asio::error::eof, std::move(s3),
[&](asio::error_code ec)
{
ec2 = ec;
});
asio::error_code ec3;
std::string s4;
bool b5 = ch1.try_receive(
[&](asio::error_code ec, std::string s)
{
ec3 = ec;
s4 = s;
});
ASIO_CHECK(b5);
ASIO_CHECK(ec3 == asio::error::eof);
ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba");
ctx.restart();
ctx.run();
ASIO_CHECK(!ec2);
};
void buffered_concurrent_channel_test()
{
io_context ctx;
concurrent_channel<void(asio::error_code, std::string)> ch1(ctx, 1);
ASIO_CHECK(ch1.is_open());
ASIO_CHECK(!ch1.ready());
bool b1 = ch1.try_send(asio::error::eof, "hello");
ASIO_CHECK(b1);
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
bool b2 = ch1.try_send(asio::error::eof, std::move(s1));
ASIO_CHECK(!b2);
ASIO_CHECK(!s1.empty());
asio::error_code ec1;
std::string s2;
ch1.async_receive(
[&](asio::error_code ec, std::string s)
{
ec1 = ec;
s2 = std::move(s);
});
ctx.run();
ASIO_CHECK(ec1 == asio::error::eof);
ASIO_CHECK(s2 == "hello");
bool b4 = ch1.try_receive([](asio::error_code, std::string){});
ASIO_CHECK(!b4);
asio::error_code ec2 = asio::error::would_block;
std::string s3 = "zyxwvutsrqponmlkjihgfedcba";
ch1.async_send(asio::error::eof, std::move(s3),
[&](asio::error_code ec)
{
ec2 = ec;
});
asio::error_code ec3;
std::string s4;
bool b5 = ch1.try_receive(
[&](asio::error_code ec, std::string s)
{
ec3 = ec;
s4 = s;
});
ASIO_CHECK(b5);
ASIO_CHECK(ec3 == asio::error::eof);
ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba");
ctx.restart();
ctx.run();
ASIO_CHECK(!ec2);
};
ASIO_TEST_SUITE
(
"experimental/concurrent_channel",
ASIO_TEST_CASE(unbuffered_concurrent_channel_test)
ASIO_TEST_CASE(buffered_concurrent_channel_test)
)
|