Skip to content

Commit b565b12

Browse files
author
pfeatherstone
committed
more tests
1 parent b3a2d1f commit b565b12

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

examples/unit_tests/async.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,95 @@ TEST_SUITE("[ASYNC]")
396396
}
397397
}
398398

399+
SUBCASE("awaitable - server sends")
400+
{
401+
try
402+
{
403+
const auto run_server = [&]() -> awaitable
404+
{
405+
co_await acceptor.async_accept(peer.lowest_layer());
406+
co_await http::async_http_read(peer, req);
407+
REQUIRE(req.is_websocket_req());
408+
co_await http::async_ws_accept(peer, req);
409+
data_peer.assign(begin(data), end(data));
410+
co_await http::async_ws_write(peer, data_peer, false);
411+
data_peer.assign(begin(text), end(text));
412+
co_await http::async_ws_write(peer, data_peer, true);
413+
co_await http::async_ws_close(peer, http::ws_going_away);
414+
};
415+
416+
const auto run_client = [&]() -> awaitable
417+
{
418+
bool is_text{};
419+
co_await boost::asio::async_connect(client.lowest_layer(), co_await resolver.async_resolve("localhost", "6667"));
420+
co_await http::async_ws_handshake(client, "localhost", "/ws");
421+
is_text = co_await http::async_ws_read(client, data_client);
422+
REQUIRE(!is_text);
423+
REQUIRE(data_client.size() == data.size());
424+
REQUIRE(std::equal(begin(data_client), end(data_client), begin(data)));
425+
is_text = co_await http::async_ws_read(client, data_client);
426+
REQUIRE(is_text);
427+
REQUIRE(data_client.size() == text.size());
428+
REQUIRE(std::equal(begin(data_client), end(data_client), begin(text)));
429+
auto [ec, is_text2] = co_await http::async_ws_read(client, data_client, as_tuple(deferred));
430+
REQUIRE(ec == http::ws_going_away);
431+
};
432+
433+
co_spawn(ioc, run_server(), detached);
434+
co_spawn(ioc, run_client(), detached);
435+
ioc.run();
436+
}
437+
catch(const std::exception& e)
438+
{
439+
exception_thrown = true;
440+
}
441+
}
442+
443+
SUBCASE("coro - server sends")
444+
{
445+
try
446+
{
447+
const auto run_server = [&](yield_context yield)
448+
{
449+
acceptor.async_accept(peer.lowest_layer(), yield);
450+
http::async_http_read(peer, req, yield);
451+
REQUIRE(req.is_websocket_req());
452+
http::async_ws_accept(peer, req, yield);
453+
data_peer.assign(begin(data), end(data));
454+
http::async_ws_write(peer, data_peer, false, yield);
455+
data_peer.assign(begin(text), end(text));
456+
http::async_ws_write(peer, data_peer, true, yield);
457+
http::async_ws_close(peer, http::ws_going_away, yield);
458+
};
459+
460+
const auto run_client = [&](yield_context yield)
461+
{
462+
bool is_text{};
463+
boost::asio::async_connect(client.lowest_layer(), resolver.async_resolve("localhost", "6667", yield), yield);
464+
http::async_ws_handshake(client, "localhost", "/ws", yield);
465+
is_text = http::async_ws_read(client, data_client, yield);
466+
REQUIRE(!is_text);
467+
REQUIRE(data_client.size() == data.size());
468+
REQUIRE(std::equal(begin(data_client), end(data_client), begin(data)));
469+
is_text = http::async_ws_read(client, data_client, yield);
470+
REQUIRE(is_text);
471+
REQUIRE(data_client.size() == text.size());
472+
REQUIRE(std::equal(begin(data_client), end(data_client), begin(text)));
473+
boost::system::error_code ec{};
474+
is_text = http::async_ws_read(client, data_client, yield[ec]);
475+
REQUIRE(ec == http::ws_going_away);
476+
};
477+
478+
boost::asio::spawn(ioc, run_server, detached);
479+
boost::asio::spawn(ioc, run_client, detached);
480+
ioc.run();
481+
}
482+
catch(const std::exception& e)
483+
{
484+
exception_thrown = true;
485+
}
486+
}
487+
399488
REQUIRE(!exception_thrown);
400489
}
401490
}

0 commit comments

Comments
 (0)