]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/websocket/timer.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / beast / test / beast / websocket / timer.cpp
1
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/beast/websocket/stream.hpp>
12
13 #include <boost/beast/_experimental/test/stream.hpp>
14 #include <boost/beast/_experimental/test/tcp.hpp>
15 #include <boost/beast/_experimental/unit_test/suite.hpp>
16 #include <boost/beast/core/flat_buffer.hpp>
17 #include <boost/asio/ip/tcp.hpp>
18 #include <boost/asio/detached.hpp>
19
20 namespace boost {
21 namespace beast {
22 namespace websocket {
23
24 struct timer_test : unit_test::suite
25 {
26 using tcp = boost::asio::ip::tcp;
27
28 void
29 testIdlePing()
30 {
31 net::io_context ioc;
32
33 // idle ping, no timeout
34
35 {
36 stream<tcp::socket> ws1(ioc);
37 stream<tcp::socket> ws2(ioc);
38 test::connect(ws1.next_layer(), ws2.next_layer());
39 ws1.async_accept(test::success_handler());
40 ws2.async_handshake("test", "/", test::success_handler());
41 test::run(ioc);
42
43 ws2.set_option(stream_base::timeout{
44 stream_base::none(),
45 std::chrono::milliseconds(100),
46 true});
47 flat_buffer b1;
48 flat_buffer b2;
49 bool received = false;
50 ws1.control_callback(
51 [&received](frame_type ft, string_view)
52 {
53 received = true;
54 BEAST_EXPECT(ft == frame_type::ping);
55 });
56 ws1.async_read(b1, test::fail_handler(
57 net::error::operation_aborted));
58 ws2.async_read(b2, test::fail_handler(
59 net::error::operation_aborted));
60 test::run_for(ioc, std::chrono::milliseconds(500));
61 BEAST_EXPECT(received);
62 }
63
64 test::run(ioc);
65
66 // idle ping, timeout
67
68 {
69 stream<tcp::socket> ws1(ioc);
70 stream<tcp::socket> ws2(ioc);
71 test::connect(ws1.next_layer(), ws2.next_layer());
72 ws1.async_accept(test::success_handler());
73 ws2.async_handshake("test", "/", test::success_handler());
74 test::run(ioc);
75
76 ws2.set_option(stream_base::timeout{
77 stream_base::none(),
78 std::chrono::milliseconds(50),
79 true});
80 flat_buffer b;
81 ws2.async_read(b,
82 test::fail_handler(beast::error::timeout));
83 test::run(ioc);
84 }
85
86 test::run(ioc);
87 }
88
89 // https://github.com/boostorg/beast/issues/1729
90 void
91 testIssue1729()
92 {
93 {
94 net::io_context ioc;
95 stream<tcp::socket> ws1(ioc);
96 stream<tcp::socket> ws2(ioc);
97 test::connect(ws1.next_layer(), ws2.next_layer());
98
99 ws1.set_option(websocket::stream_base::timeout{
100 std::chrono::milliseconds(50),
101 websocket::stream_base::none(),
102 false});
103
104 ws1.async_accept(net::detached);
105 ws2.async_handshake(
106 "localhost", "/", net::detached);
107 ioc.run();
108 ioc.restart();
109
110 flat_buffer b;
111 error_code ec2;
112 ws1.async_close({},
113 [&ec2](error_code ec)
114 {
115 ec2 = ec;
116 });
117 ioc.run();
118 BEAST_EXPECTS(
119 ec2 == beast::error::timeout ||
120 ec2 == net::error::operation_aborted,
121 ec2.message());
122 }
123 {
124 net::io_context ioc;
125 stream<tcp::socket> ws1(ioc);
126 stream<tcp::socket> ws2(ioc);
127 test::connect(ws1.next_layer(), ws2.next_layer());
128
129 ws1.set_option(websocket::stream_base::timeout{
130 std::chrono::milliseconds(50),
131 websocket::stream_base::none(),
132 false});
133
134 ws1.async_accept(net::detached);
135 ws2.async_handshake(
136 "localhost", "/", net::detached);
137 ioc.run();
138 ioc.restart();
139
140 flat_buffer b;
141 error_code ec1, ec2;
142 ws1.async_read(b,
143 [&ec1](error_code ec, std::size_t)
144 {
145 ec1 = ec;
146 });
147 ws1.async_close({},
148 [&ec2](error_code ec)
149 {
150 ec2 = ec;
151 });
152 ioc.run();
153 BEAST_EXPECT(
154 ec1 == beast::error::timeout ||
155 ec2 == beast::error::timeout);
156 }
157 }
158
159 // https://github.com/boostorg/beast/issues/1729#issuecomment-540481056
160 void
161 testCloseWhileRead()
162 {
163 net::io_context ioc;
164 stream<tcp::socket> ws1(ioc);
165 stream<tcp::socket> ws2(ioc);
166 test::connect(ws1.next_layer(), ws2.next_layer());
167
168 ws1.set_option(websocket::stream_base::timeout{
169 std::chrono::milliseconds(50),
170 websocket::stream_base::none(),
171 false});
172
173 ws1.async_accept(net::detached);
174 ws2.async_handshake(
175 "localhost", "/", net::detached);
176 ioc.run();
177 ioc.restart();
178
179 flat_buffer b;
180 ws1.async_read(b, test::fail_handler(
181 beast::error::timeout));
182 ws1.async_close({}, test::fail_handler(
183 net::error::operation_aborted));
184 ioc.run();
185 }
186
187 void
188 run() override
189 {
190 testIssue1729();
191 testIdlePing();
192 testCloseWhileRead();
193 }
194 };
195
196 BEAST_DEFINE_TESTSUITE(beast,websocket,timer);
197
198 } // websocket
199 } // beast
200 } // boost