From 726e66ca669d7250260eb41c0637c2d5ce3c6ee6 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sat, 16 Feb 2019 10:31:55 -0800 Subject: [PATCH] unittest for sending large messages --- ixwebsocket/IXSocketServer.cpp | 4 +-- makefile | 2 +- test/cmd_websocket_chat.cpp | 62 +++++++++++++++++++++++++++++----- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 56012e88..de67c052 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -42,13 +42,13 @@ namespace ix void SocketServer::logError(const std::string& str) { std::lock_guard lock(_logMutex); - std::cerr << str << std::endl; + fprintf(stderr, "%s\n", str.c_str()); } void SocketServer::logInfo(const std::string& str) { std::lock_guard lock(_logMutex); - std::cout << str << std::endl; + fprintf(stderr, "%s\n", str.c_str()); } std::pair SocketServer::listen() diff --git a/makefile b/makefile index 04e384f8..3f1df944 100644 --- a/makefile +++ b/makefile @@ -24,7 +24,7 @@ test_server: (cd test && npm i ws && node broadcast-server.js) # env TEST=Websocket_server make test -# env TEST=websocket_server make test +# env TEST=Websocket_chat make test # env TEST=heartbeat make test test: python test/run.py diff --git a/test/cmd_websocket_chat.cpp b/test/cmd_websocket_chat.cpp index 5a91cde2..7b3e9d59 100644 --- a/test/cmd_websocket_chat.cpp +++ b/test/cmd_websocket_chat.cpp @@ -11,7 +11,8 @@ #include #include -#include +#include +#include #include #include #include "msgpack11.hpp" @@ -39,9 +40,11 @@ namespace void sendMessage(const std::string& text); size_t getReceivedMessagesCount() const; + const std::vector& getReceivedMessages() const; std::string encodeMessage(const std::string& text); std::pair decodeMessage(const std::string& str); + void appendMessage(const std::string& message); private: std::string _user; @@ -50,7 +53,8 @@ namespace ix::WebSocket _webSocket; - std::queue _receivedQueue; + std::vector _receivedMessages; + mutable std::mutex _mutex; }; WebSocketChat::WebSocketChat(const std::string& user, @@ -65,7 +69,20 @@ namespace size_t WebSocketChat::getReceivedMessagesCount() const { - return _receivedQueue.size(); + std::lock_guard lock(_mutex); + return _receivedMessages.size(); + } + + const std::vector& WebSocketChat::getReceivedMessages() const + { + std::lock_guard lock(_mutex); + return _receivedMessages; + } + + void WebSocketChat::appendMessage(const std::string& message) + { + std::lock_guard lock(_mutex); + _receivedMessages.push_back(message); } bool WebSocketChat::isReady() const @@ -85,7 +102,8 @@ namespace std::stringstream ss; ss << "ws://localhost:" << _port - << "/"; + << "/" + << _user; url = ss.str(); } @@ -127,10 +145,16 @@ namespace // as we do for the satori chat example. // store text - _receivedQueue.push(result.second); + appendMessage(result.second); + + std::string payload = result.second; + if (payload.size() > 2000) + { + payload = ""; + } ss << std::endl - << result.first << " > " << result.second + << result.first << " > " << payload << std::endl << _user << " > "; log(ss.str()); @@ -269,15 +293,35 @@ TEST_CASE("Websocket_chat", "[websocket_chat]") chatB.sendMessage("from B1"); chatB.sendMessage("from B2"); - // Give us 1s for all messages to be received - ix::msleep(1000); + // FIXME: cannot handle large message, we need to break them down + // into small one at the websocket layer (using CONTINUATION opcode) + size_t size = 512 * 1000; // 512K is OK, larger is not !! + std::string bigMessage(size, 'a'); + chatB.sendMessage(bigMessage); + + // Wait until all messages are received. 10s timeout + int attempts = 0; + while (chatA.getReceivedMessagesCount() != 3 || + chatB.getReceivedMessagesCount() != 3) + { + REQUIRE(attempts++ < 10); + ix::msleep(1000); + } chatA.stop(); chatB.stop(); - REQUIRE(chatA.getReceivedMessagesCount() == 2); + REQUIRE(chatA.getReceivedMessagesCount() == 3); REQUIRE(chatB.getReceivedMessagesCount() == 3); + REQUIRE(chatB.getReceivedMessages()[0] == "from A1"); + REQUIRE(chatB.getReceivedMessages()[1] == "from A2"); + REQUIRE(chatB.getReceivedMessages()[2] == "from A3"); + + REQUIRE(chatA.getReceivedMessages()[0] == "from B1"); + REQUIRE(chatA.getReceivedMessages()[1] == "from B2"); + REQUIRE(chatA.getReceivedMessages()[2].size() == bigMessage.size()); + // Give us 500ms for the server to notice that clients went away ix::msleep(500); REQUIRE(server.getClients().size() == 0);