diff options
author | eidheim <eidheim@gmail.com> | 2017-12-08 10:00:13 +0300 |
---|---|---|
committer | eidheim <eidheim@gmail.com> | 2017-12-08 10:00:13 +0300 |
commit | 6e61623a9ff4d72294730fd8b333cf1de406d37c (patch) | |
tree | 8e5a8eba2cf0000f57137902c37c6ec430951b8d /tests | |
parent | e9cced40960a6bcddcc7bfd6f6d8520632f76dd0 (diff) |
Added fragmented message test to io_test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/io_test.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/io_test.cpp b/tests/io_test.cpp index 1272bc2..765f511 100644 --- a/tests/io_test.cpp +++ b/tests/io_test.cpp @@ -59,6 +59,23 @@ int main() { connection->send(send_stream); }; + auto &fragmented_message = server.endpoint["^/fragmented_message/?$"]; + fragmented_message.on_message = [&server_callback_count](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) { + ++server_callback_count; + assert(message->string() == "fragmented message"); + auto send_stream = make_shared<WsServer::SendStream>(); + *send_stream << "fragmented"; + connection->send(send_stream, nullptr, 1); + + send_stream = make_shared<WsServer::SendStream>(); + *send_stream << " "; + connection->send(send_stream, nullptr, 0); + + send_stream = make_shared<WsServer::SendStream>(); + *send_stream << "message"; + connection->send(send_stream, nullptr, 128); + }; + thread server_thread([&server]() { server.start(); }); @@ -232,6 +249,63 @@ int main() { assert(server_callback_count == 202); } + { + WsClient client("localhost:8080/fragmented_message"); + + server_callback_count = 0; + atomic<int> client_callback_count(0); + atomic<bool> closed(false); + + client.on_message = [&](shared_ptr<WsClient::Connection> connection, shared_ptr<WsClient::Message> message) { + assert(message->string() == "fragmented message"); + + ++client_callback_count; + + connection->send_close(1000); + }; + + client.on_open = [&](shared_ptr<WsClient::Connection> connection) { + ++client_callback_count; + + assert(!closed); + + auto send_stream = make_shared<WsClient::SendStream>(); + *send_stream << "fragmented"; + connection->send(send_stream, nullptr, 1); + + send_stream = make_shared<WsClient::SendStream>(); + *send_stream << " "; + connection->send(send_stream, nullptr, 0); + + send_stream = make_shared<WsClient::SendStream>(); + *send_stream << "message"; + connection->send(send_stream, nullptr, 128); + }; + + client.on_close = [&](shared_ptr<WsClient::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { + assert(!closed); + closed = true; + }; + + client.on_error = [](shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) { + cerr << ec.message() << endl; + assert(false); + }; + + thread client_thread([&client]() { + client.start(); + }); + + while(!closed) + this_thread::sleep_for(chrono::milliseconds(5)); + + client.stop(); + client_thread.join(); + + assert(client_callback_count == 2); + assert(server_callback_count == 1); + } + server.stop(); server_thread.join(); } |