MCPcopy Create free account
hub / github.com/boostorg/asio / test_sync_operations

Function test_sync_operations

test/buffered_write_stream.cpp:138–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

136}
137
138void test_sync_operations()
139{
140 using namespace std; // For memcmp.
141
142 boost::asio::io_context io_context;
143
144 boost::asio::ip::tcp::acceptor acceptor(io_context,
145 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
146 boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
147 server_endpoint.address(boost::asio::ip::address_v4::loopback());
148
149 stream_type client_socket(io_context);
150 client_socket.lowest_layer().connect(server_endpoint);
151
152 stream_type server_socket(io_context);
153 acceptor.accept(server_socket.lowest_layer());
154
155 const char write_data[]
156 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
157 const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
158
159 std::size_t bytes_written = 0;
160 while (bytes_written < sizeof(write_data))
161 {
162 bytes_written += client_socket.write_some(
163 boost::asio::buffer(write_buf + bytes_written));
164 client_socket.flush();
165 }
166
167 char read_data[sizeof(write_data)];
168 const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
169
170 std::size_t bytes_read = 0;
171 while (bytes_read < sizeof(read_data))
172 {
173 bytes_read += server_socket.read_some(
174 boost::asio::buffer(read_buf + bytes_read));
175 }
176
177 BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
178 BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
179 BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
180
181 bytes_written = 0;
182 while (bytes_written < sizeof(write_data))
183 {
184 bytes_written += server_socket.write_some(
185 boost::asio::buffer(write_buf + bytes_written));
186 server_socket.flush();
187 }
188
189 bytes_read = 0;
190 while (bytes_read < sizeof(read_data))
191 {
192 bytes_read += client_socket.read_some(
193 boost::asio::buffer(read_buf + bytes_read));
194 }
195

Callers

nothing calls this directly

Calls 10

bufferFunction · 0.85
flushMethod · 0.80
endpointClass · 0.50
local_endpointMethod · 0.45
addressMethod · 0.45
connectMethod · 0.45
acceptMethod · 0.45
write_someMethod · 0.45
read_someMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected