]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // experimental/channel.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | // Disable autolinking for unit tests. | |
12 | #if !defined(BOOST_ALL_NO_LIB) | |
13 | #define BOOST_ALL_NO_LIB 1 | |
14 | #endif // !defined(BOOST_ALL_NO_LIB) | |
15 | ||
16 | // Test that header file is self-contained. | |
17 | #include <boost/asio/experimental/channel.hpp> | |
18 | ||
19 | #include <utility> | |
20 | #include <boost/asio/error.hpp> | |
21 | #include <boost/asio/io_context.hpp> | |
22 | #include "../unit_test.hpp" | |
23 | ||
24 | using namespace boost::asio; | |
25 | using namespace boost::asio::experimental; | |
26 | ||
27 | void unbuffered_channel_test() | |
28 | { | |
29 | io_context ctx; | |
30 | ||
31 | channel<void(boost::system::error_code, std::string)> ch1(ctx); | |
32 | ||
33 | BOOST_ASIO_CHECK(ch1.is_open()); | |
34 | BOOST_ASIO_CHECK(!ch1.ready()); | |
35 | ||
36 | bool b1 = ch1.try_send(boost::asio::error::eof, "hello"); | |
37 | ||
38 | BOOST_ASIO_CHECK(!b1); | |
39 | ||
40 | std::string s1 = "abcdefghijklmnopqrstuvwxyz"; | |
41 | bool b2 = ch1.try_send(boost::asio::error::eof, std::move(s1)); | |
42 | ||
43 | BOOST_ASIO_CHECK(!b2); | |
44 | BOOST_ASIO_CHECK(!s1.empty()); | |
45 | ||
46 | boost::system::error_code ec1; | |
47 | std::string s2; | |
48 | ch1.async_receive( | |
49 | [&](boost::system::error_code ec, std::string s) | |
50 | { | |
51 | ec1 = ec; | |
52 | s2 = std::move(s); | |
53 | }); | |
54 | ||
55 | bool b3 = ch1.try_send(boost::asio::error::eof, std::move(s1)); | |
56 | ||
57 | BOOST_ASIO_CHECK(b3); | |
58 | BOOST_ASIO_CHECK(s1.empty()); | |
59 | ||
60 | ctx.run(); | |
61 | ||
62 | BOOST_ASIO_CHECK(ec1 == boost::asio::error::eof); | |
63 | BOOST_ASIO_CHECK(s2 == "abcdefghijklmnopqrstuvwxyz"); | |
64 | ||
65 | bool b4 = ch1.try_receive([](boost::system::error_code, std::string){}); | |
66 | ||
67 | BOOST_ASIO_CHECK(!b4); | |
68 | ||
69 | boost::system::error_code ec2 = boost::asio::error::would_block; | |
70 | std::string s3 = "zyxwvutsrqponmlkjihgfedcba"; | |
71 | ch1.async_send(boost::asio::error::eof, std::move(s3), | |
72 | [&](boost::system::error_code ec) | |
73 | { | |
74 | ec2 = ec; | |
75 | }); | |
76 | ||
77 | boost::system::error_code ec3; | |
78 | std::string s4; | |
79 | bool b5 = ch1.try_receive( | |
80 | [&](boost::system::error_code ec, std::string s) | |
81 | { | |
82 | ec3 = ec; | |
83 | s4 = s; | |
84 | }); | |
85 | ||
86 | BOOST_ASIO_CHECK(b5); | |
87 | BOOST_ASIO_CHECK(ec3 == boost::asio::error::eof); | |
88 | BOOST_ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba"); | |
89 | ||
90 | ctx.restart(); | |
91 | ctx.run(); | |
92 | ||
93 | BOOST_ASIO_CHECK(!ec2); | |
94 | }; | |
95 | ||
96 | void buffered_channel_test() | |
97 | { | |
98 | io_context ctx; | |
99 | ||
100 | channel<void(boost::system::error_code, std::string)> ch1(ctx, 1); | |
101 | ||
102 | BOOST_ASIO_CHECK(ch1.is_open()); | |
103 | BOOST_ASIO_CHECK(!ch1.ready()); | |
104 | ||
105 | bool b1 = ch1.try_send(boost::asio::error::eof, "hello"); | |
106 | ||
107 | BOOST_ASIO_CHECK(b1); | |
108 | ||
109 | std::string s1 = "abcdefghijklmnopqrstuvwxyz"; | |
110 | bool b2 = ch1.try_send(boost::asio::error::eof, std::move(s1)); | |
111 | ||
112 | BOOST_ASIO_CHECK(!b2); | |
113 | BOOST_ASIO_CHECK(!s1.empty()); | |
114 | ||
115 | boost::system::error_code ec1; | |
116 | std::string s2; | |
117 | ch1.async_receive( | |
118 | [&](boost::system::error_code ec, std::string s) | |
119 | { | |
120 | ec1 = ec; | |
121 | s2 = std::move(s); | |
122 | }); | |
123 | ||
124 | ctx.run(); | |
125 | ||
126 | BOOST_ASIO_CHECK(ec1 == boost::asio::error::eof); | |
127 | BOOST_ASIO_CHECK(s2 == "hello"); | |
128 | ||
129 | bool b4 = ch1.try_receive([](boost::system::error_code, std::string){}); | |
130 | ||
131 | BOOST_ASIO_CHECK(!b4); | |
132 | ||
133 | boost::system::error_code ec2 = boost::asio::error::would_block; | |
134 | std::string s3 = "zyxwvutsrqponmlkjihgfedcba"; | |
135 | ch1.async_send(boost::asio::error::eof, std::move(s3), | |
136 | [&](boost::system::error_code ec) | |
137 | { | |
138 | ec2 = ec; | |
139 | }); | |
140 | ||
141 | boost::system::error_code ec3; | |
142 | std::string s4; | |
143 | bool b5 = ch1.try_receive( | |
144 | [&](boost::system::error_code ec, std::string s) | |
145 | { | |
146 | ec3 = ec; | |
147 | s4 = s; | |
148 | }); | |
149 | ||
150 | BOOST_ASIO_CHECK(b5); | |
151 | BOOST_ASIO_CHECK(ec3 == boost::asio::error::eof); | |
152 | BOOST_ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba"); | |
153 | ||
154 | ctx.restart(); | |
155 | ctx.run(); | |
156 | ||
157 | BOOST_ASIO_CHECK(!ec2); | |
158 | }; | |
159 | ||
160 | BOOST_ASIO_TEST_SUITE | |
161 | ( | |
162 | "experimental/channel", | |
163 | BOOST_ASIO_TEST_CASE(unbuffered_channel_test) | |
164 | BOOST_ASIO_TEST_CASE(buffered_channel_test) | |
165 | ) |