IXWebSocket/test/IXWebSocketPingTest.cpp

484 lines
15 KiB
C++
Raw Normal View History

2019-01-25 03:50:07 +01:00
/*
* IXWebSocketPingTest.cpp
2019-01-25 03:50:07 +01:00
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <queue>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXWebSocketServer.h>
#include "IXTest.h"
#include "catch.hpp"
using namespace ix;
namespace
{
class WebSocketClient
{
public:
WebSocketClient(int port, bool useHeartBeatMethod);
2019-01-25 03:50:07 +01:00
void start();
void stop();
bool isReady() const;
2019-01-26 01:11:39 +01:00
void sendMessage(const std::string& text);
2019-01-25 03:50:07 +01:00
private:
ix::WebSocket _webSocket;
int _port;
bool _useHeartBeatMethod;
2019-01-25 03:50:07 +01:00
};
WebSocketClient::WebSocketClient(int port, bool useHeartBeatMethod)
: _port(port),
_useHeartBeatMethod(useHeartBeatMethod)
2019-01-25 03:50:07 +01:00
{
;
}
bool WebSocketClient::isReady() const
{
return _webSocket.getReadyState() == ix::ReadyState::Open;
2019-01-25 03:50:07 +01:00
}
void WebSocketClient::stop()
{
_webSocket.stop();
}
void WebSocketClient::start()
{
std::string url;
{
std::stringstream ss;
ss << "ws://127.0.0.1:"
<< _port
2019-01-25 03:50:07 +01:00
<< "/";
url = ss.str();
}
_webSocket.setUrl(url);
// The important bit for this test.
// Set a 1 second heartbeat with the setter method to test
if (_useHeartBeatMethod)
{
_webSocket.setHeartBeatPeriod(1);
}
else
{
_webSocket.setPingInterval(1);
}
2019-01-25 03:50:07 +01:00
std::stringstream ss;
log(std::string("Connecting to url: ") + url);
_webSocket.setOnMessageCallback(
2019-01-25 06:16:32 +01:00
[](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
2019-01-25 03:50:07 +01:00
const ix::WebSocketCloseInfo& closeInfo)
{
std::stringstream ss;
if (messageType == ix::WebSocketMessageType::Open)
2019-01-25 03:50:07 +01:00
{
log("client connected");
}
else if (messageType == ix::WebSocketMessageType::Close)
2019-01-25 03:50:07 +01:00
{
log("client disconnected");
}
else if (messageType == ix::WebSocketMessageType::Error)
2019-01-25 03:50:07 +01:00
{
ss << "Error ! " << error.reason;
log(ss.str());
}
else if (messageType == ix::WebSocketMessageType::Pong)
2019-01-25 03:50:07 +01:00
{
ss << "Received pong message " << str;
log(ss.str());
}
else if (messageType == ix::WebSocketMessageType::Ping)
2019-01-25 03:50:07 +01:00
{
ss << "Received ping message " << str;
log(ss.str());
}
else if (messageType == ix::WebSocketMessageType::Message)
2019-01-26 01:11:39 +01:00
{
// too many messages to log
2019-01-26 01:11:39 +01:00
}
2019-01-25 03:50:07 +01:00
else
{
ss << "Invalid ix::WebSocketMessageType";
log(ss.str());
}
});
_webSocket.start();
}
2019-01-26 01:11:39 +01:00
void WebSocketClient::sendMessage(const std::string& text)
{
_webSocket.send(text);
}
2019-01-25 03:50:07 +01:00
bool startServer(ix::WebSocketServer& server, std::atomic<int>& receivedPingMessages)
{
2019-01-26 01:11:39 +01:00
// A dev/null server
2019-01-25 03:50:07 +01:00
server.setOnConnectionCallback(
[&server, &receivedPingMessages](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState)
2019-01-25 03:50:07 +01:00
{
webSocket->setOnMessageCallback(
[webSocket, connectionState, &server, &receivedPingMessages](ix::WebSocketMessageType messageType,
2019-01-25 03:50:07 +01:00
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{
if (messageType == ix::WebSocketMessageType::Open)
2019-01-25 03:50:07 +01:00
{
2019-01-26 01:11:39 +01:00
Logger() << "New server connection";
Logger() << "id: " << connectionState->getId();
2019-01-25 03:50:07 +01:00
Logger() << "Uri: " << openInfo.uri;
Logger() << "Headers:";
for (auto it : openInfo.headers)
{
Logger() << it.first << ": " << it.second;
}
}
else if (messageType == ix::WebSocketMessageType::Close)
2019-01-25 03:50:07 +01:00
{
2019-01-26 01:11:39 +01:00
log("Server closed connection");
2019-01-25 03:50:07 +01:00
}
else if (messageType == ix::WebSocketMessageType::Ping)
2019-01-25 03:50:07 +01:00
{
2019-01-26 01:11:39 +01:00
log("Server received a ping");
2019-01-25 03:50:07 +01:00
receivedPingMessages++;
}
else if (messageType == ix::WebSocketMessageType::Message)
{
// to many messages to log
for(auto client: server.getClients())
{
client->sendText("reply");
}
}
2019-01-25 03:50:07 +01:00
}
);
}
);
auto res = server.listen();
if (!res.first)
{
log(res.second);
return false;
}
server.start();
return true;
}
}
TEST_CASE("Websocket_ping_no_data_sent_setPingInterval", "[setPingInterval]")
2019-01-25 03:50:07 +01:00
{
SECTION("Make sure that ping messages are sent when no other data are sent.")
2019-01-25 03:50:07 +01:00
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
2019-01-25 03:50:07 +01:00
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = false; // so use setPingInterval
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
2019-01-25 03:50:07 +01:00
webSocketClient.start();
2019-01-25 03:50:07 +01:00
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
2019-01-25 03:50:07 +01:00
ix::msleep(10);
}
REQUIRE(server.getClients().size() == 1);
ix::msleep(2100);
webSocketClient.stop();
2019-04-20 01:57:38 +02:00
// Here we test ping interval
// -> expected ping messages == 2 as 2100 seconds, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 2);
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
}
}
TEST_CASE("Websocket_ping_data_sent_setPingInterval", "[setPingInterval]")
{
SECTION("Make sure that ping messages are sent, even if other messages are sent")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = false; // so use setPingInterval
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
webSocketClient.start();
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
ix::msleep(10);
}
REQUIRE(server.getClients().size() == 1);
2019-01-25 03:50:07 +01:00
2019-01-26 01:11:39 +01:00
ix::msleep(900);
webSocketClient.sendMessage("hello world");
2019-01-26 01:11:39 +01:00
ix::msleep(900);
webSocketClient.sendMessage("hello world");
ix::msleep(1300);
webSocketClient.stop();
2019-04-20 01:57:38 +02:00
// Here we test ping interval
// client has sent data, but ping should have been sent no matter what
// -> expected ping messages == 3 as 900+900+1300 = 3100 seconds, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 3);
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
}
}
TEST_CASE("Websocket_ping_data_sent_setPingInterval_half_full", "[setPingInterval]")
{
SECTION("Make sure that ping messages are sent, even if other messages are sent continuously during a given time")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = false; // so use setPingInterval
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
webSocketClient.start();
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
ix::msleep(10);
}
REQUIRE(server.getClients().size() == 1);
// send continuously for 1100ms
auto now = std::chrono::steady_clock::now();
while(std::chrono::steady_clock::now() - now <= std::chrono::milliseconds(900))
{
webSocketClient.sendMessage("message");
ix::msleep(1);
}
ix::msleep(150);
// Here we test ping interval
// client has sent data, but ping should have been sent no matter what
// -> expected ping messages == 1, as 900+150 = 1050ms, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 1);
ix::msleep(100);
webSocketClient.stop();
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
}
}
TEST_CASE("Websocket_ping_data_sent_setPingInterval_full", "[setPingInterval]")
{
SECTION("Make sure that ping messages are sent, even if other messages are sent continuously for longer than ping interval")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = false; // so use setPingInterval
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
webSocketClient.start();
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
ix::msleep(1);
}
REQUIRE(server.getClients().size() == 1);
// send continuously for 1100ms
auto now = std::chrono::steady_clock::now();
while(std::chrono::steady_clock::now() - now <= std::chrono::milliseconds(1100))
{
webSocketClient.sendMessage("message");
ix::msleep(1);
}
// Here we test ping interval
// client has sent data, but ping should have been sent no matter what
// -> expected ping messages == 1, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 1);
ix::msleep(100);
webSocketClient.stop();
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
}
}
// Using setHeartBeatPeriod
TEST_CASE("Websocket_ping_no_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod]")
{
SECTION("Make sure that ping messages are sent when no other data are sent.")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = true;
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
webSocketClient.start();
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
ix::msleep(1);
}
REQUIRE(server.getClients().size() == 1);
ix::msleep(1900);
webSocketClient.stop();
2019-04-20 01:57:38 +02:00
// Here we test ping interval
// -> expected ping messages == 1 as 1900 seconds, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 1);
2019-01-25 03:50:07 +01:00
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
server.stop();
}
}
TEST_CASE("Websocket_ping_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod]")
{
SECTION("Make sure that ping messages are sent, even if other messages are sent")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = getFreePort();
ix::WebSocketServer server(port);
std::atomic<int> serverReceivedPingMessages(0);
REQUIRE(startServer(server, serverReceivedPingMessages));
std::string session = ix::generateSessionId();
bool useSetHeartBeatPeriodMethod = true;
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
webSocketClient.start();
// Wait for all chat instance to be ready
while (true)
{
if (webSocketClient.isReady()) break;
ix::msleep(1);
}
REQUIRE(server.getClients().size() == 1);
ix::msleep(900);
webSocketClient.sendMessage("hello world");
ix::msleep(900);
webSocketClient.sendMessage("hello world");
ix::msleep(1100);
2019-01-25 03:50:07 +01:00
webSocketClient.stop();
2019-03-21 21:50:59 +01:00
// without this sleep test fails on Windows
ix::msleep(100);
2019-04-20 01:57:38 +02:00
// Here we test ping interval
// client has sent data, but ping should have been sent no matter what
// -> expected ping messages == 2 as 900+900+1100 = 2900 seconds, 1 ping sent every second
REQUIRE(serverReceivedPingMessages == 2);
2019-01-25 03:50:07 +01:00
// Give us 1000ms for the server to notice that clients went away
ix::msleep(1000);
2019-01-25 03:50:07 +01:00
REQUIRE(server.getClients().size() == 0);
ix::reportWebSocketTraffic();
server.stop();
2019-01-25 03:50:07 +01:00
}
}