From ac751aa243592933a3978a5ce02663dadbb92d10 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 4 Aug 2016 11:08:21 +0200 Subject: Added io_test --- tests/CMakeLists.txt | 7 +++ tests/io_test.cpp | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 tests/io_test.cpp (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2887f7d..77c9123 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,12 +4,19 @@ add_executable(crypto_test crypto_test.cpp) target_link_libraries(crypto_test ${OPENSSL_CRYPTO_LIBRARY}) add_test(crypto_test crypto_test) +add_executable(io_test io_test.cpp) +target_link_libraries(io_test ${Boost_LIBRARIES}) +target_link_libraries(io_test ${OPENSSL_CRYPTO_LIBRARY}) +target_link_libraries(io_test ${CMAKE_THREAD_LIBS_INIT}) + add_executable(parse_test parse_test.cpp) target_link_libraries(parse_test ${Boost_LIBRARIES}) target_link_libraries(parse_test ${CMAKE_THREAD_LIBS_INIT}) if(MSYS) + target_link_libraries(io_test ws2_32 wsock32) target_link_libraries(parse_test ws2_32 wsock32) endif() +add_test(io_test io_test) add_test(parse_test parse_test) diff --git a/tests/io_test.cpp b/tests/io_test.cpp new file mode 100644 index 0000000..edac4d4 --- /dev/null +++ b/tests/io_test.cpp @@ -0,0 +1,161 @@ +#include "server_ws.hpp" +#include "client_ws.hpp" + +#include + +using namespace std; + +typedef SimpleWeb::SocketServer WsServer; +typedef SimpleWeb::SocketClient WsClient; + +int main() { + WsServer server(8080, 4); + + auto& echo=server.endpoint["^/echo/?$"]; + + atomic server_callback_count(0); + + echo.onmessage=[&server, &server_callback_count](shared_ptr connection, shared_ptr message) { + auto message_str=message->string(); + assert(message_str=="Hello"); + + ++server_callback_count; + auto send_stream=make_shared(); + *send_stream << message_str; + server.send(connection, send_stream, [](const boost::system::error_code& ec){ + if(ec) { + cerr << ec.message() << endl; + assert(false); + } + }); + }; + + echo.onopen=[&server_callback_count](shared_ptr /*connection*/) { + ++server_callback_count; + }; + + echo.onclose=[&server_callback_count](shared_ptr /*connection*/, int /*status*/, const string& /*reason*/) { + ++server_callback_count; + }; + + echo.onerror=[](shared_ptr /*connection*/, const boost::system::error_code& ec) { + cerr << ec.message() << endl; + assert(false); + }; + + auto& echo_thrice=server.endpoint["^/echo_thrice/?$"]; + echo_thrice.onmessage=[&server](shared_ptr connection, shared_ptr message) { + auto message_str=message->string(); + + auto send_stream1=make_shared(); + *send_stream1 << message_str; + server.send(connection, send_stream1, [&server, connection, message_str](const boost::system::error_code& ec) { + if(!ec) { + auto send_stream3=make_shared(); + *send_stream3 << message_str; + server.send(connection, send_stream3); //Sent after send_stream1 is sent, and most likely after send_stream2 + } + }); + //Do not reuse send_stream1 here as it most likely is not sent yet + auto send_stream2=make_shared(); + *send_stream2 << message_str; + server.send(connection, send_stream2); //Most likely queued, and sent after send_stream1 + }; + + thread server_thread([&server](){ + server.start(); + }); + + this_thread::sleep_for(chrono::seconds(1)); + + { + WsClient client("localhost:8080/echo"); + + atomic client_callback_count(0); + + client.onmessage=[&client, &client_callback_count](shared_ptr message) { + assert(message->string()=="Hello"); + + ++client_callback_count; + + client.send_close(1000); + }; + + client.onopen=[&client, &client_callback_count]() { + ++client_callback_count; + + auto send_stream=make_shared(); + *send_stream << "Hello"; + client.send(send_stream); + }; + + client.onclose=[&client_callback_count](int /*status*/, const string& /*reason*/) { + ++client_callback_count; + }; + + client.onerror=[](const boost::system::error_code& ec) { + cerr << ec.message() << endl; + assert(false); + }; + + thread client_thread([&client](){ + client.start(); + }); + + this_thread::sleep_for(chrono::seconds(1)); + + client.stop(); + client_thread.join(); + + assert(client_callback_count==3); + } + + { + WsClient client("localhost:8080/echo_thrice"); + + atomic client_callback_count(0); + + client.onmessage=[&client, &client_callback_count](shared_ptr message) { + assert(message->string()=="Hello"); + + ++client_callback_count; + + client.send_close(1000); + }; + + client.onopen=[&client, &client_callback_count]() { + ++client_callback_count; + + auto send_stream=make_shared(); + *send_stream << "Hello"; + client.send(send_stream); + }; + + client.onclose=[&client_callback_count](int /*status*/, const string& /*reason*/) { + ++client_callback_count; + }; + + client.onerror=[](const boost::system::error_code& ec) { + cerr << ec.message() << endl; + assert(false); + }; + + thread client_thread([&client](){ + client.start(); + }); + + this_thread::sleep_for(chrono::seconds(1)); + + client.stop(); + client_thread.join(); + + assert(client_callback_count==5); + } + + server.stop(); + server_thread.join(); + + assert(server_callback_count==3); + + return 0; +} -- cgit v1.2.3