IXWebSocket/test/IXWebSocketPingTest.cpp

452 lines
13 KiB
C++
Raw Permalink 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.
*/
2019-09-23 19:25:23 +02:00
#include "IXTest.h"
#include "catch.hpp"
2019-01-25 03:50:07 +01:00
#include <iostream>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXWebSocketServer.h>
2019-09-23 19:25:23 +02:00
#include <queue>
#include <sstream>
2019-01-25 03:50:07 +01:00
using namespace ix;
namespace
{
class WebSocketClient
{
2019-09-23 19:25:23 +02:00
public:
WebSocketClient(int port);
2019-09-23 19:25:23 +02:00
void start();
void stop();
bool isReady() const;
void sendMessage(const std::string& text);
private:
ix::WebSocket _webSocket;
int _port;
2019-01-25 03:50:07 +01:00
};
WebSocketClient::WebSocketClient(int port)
2019-09-23 19:25:23 +02:00
: _port(port)
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;
2019-09-23 19:25:23 +02:00
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
_webSocket.setPingInterval(1);
2019-01-25 03:50:07 +01:00
std::stringstream ss;
log(std::string("Connecting to url: ") + url);
_webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg) {
2019-09-23 19:25:23 +02:00
std::stringstream ss;
if (msg->type == ix::WebSocketMessageType::Open)
2019-01-25 03:50:07 +01:00
{
2019-09-23 19:25:23 +02:00
log("client connected");
}
else if (msg->type == ix::WebSocketMessageType::Close)
2019-09-23 19:25:23 +02:00
{
log("client disconnected");
}
else if (msg->type == ix::WebSocketMessageType::Error)
2019-09-23 19:25:23 +02:00
{
ss << "Error ! " << msg->errorInfo.reason;
2019-09-23 19:25:23 +02:00
log(ss.str());
}
else if (msg->type == ix::WebSocketMessageType::Pong)
2019-09-23 19:25:23 +02:00
{
ss << "Received pong message " << msg->str;
2019-09-23 19:25:23 +02:00
log(ss.str());
}
else if (msg->type == ix::WebSocketMessageType::Ping)
2019-09-23 19:25:23 +02:00
{
ss << "Received ping message " << msg->str;
2019-09-23 19:25:23 +02:00
log(ss.str());
}
else if (msg->type == ix::WebSocketMessageType::Message)
2019-09-23 19:25:23 +02:00
{
// too many messages to log
}
else
{
ss << "Invalid ix::WebSocketMessageType";
log(ss.str());
}
});
2019-01-25 03:50:07 +01:00
_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,
2019-09-23 19:25:23 +02:00
std::shared_ptr<ConnectionState> connectionState) {
2019-01-25 03:50:07 +01:00
webSocket->setOnMessageCallback(
2019-09-23 19:25:23 +02:00
[webSocket, connectionState, &server, &receivedPingMessages](
const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
2019-01-25 03:50:07 +01:00
{
TLogger() << "New server connection";
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
2019-01-25 03:50:07 +01:00
{
TLogger() << it.first << ": " << it.second;
2019-01-25 03:50:07 +01:00
}
}
else if (msg->type == 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 (msg->type == 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 (msg->type == ix::WebSocketMessageType::Message)
{
// to many messages to log
2019-09-23 19:25:23 +02:00
for (auto client : server.getClients())
{
client->sendText("reply");
}
}
2019-09-23 19:25:23 +02:00
});
});
2019-01-25 03:50:07 +01:00
auto res = server.listen();
if (!res.first)
{
log(res.second);
return false;
}
server.start();
return true;
}
2019-09-23 19:25:23 +02:00
} // namespace
2019-01-25 03:50:07 +01:00
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();
WebSocketClient webSocketClient(port);
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();
WebSocketClient webSocketClient(port);
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 >= 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_half_full", "[setPingInterval]")
{
2019-09-23 19:25:23 +02:00
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();
WebSocketClient webSocketClient(port);
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();
2019-09-23 19:25:23 +02:00
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]")
{
2019-09-23 19:25:23 +02:00
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();
WebSocketClient webSocketClient(port);
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();
2019-09-23 19:25:23 +02:00
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 == 2, 1 ping sent every second
// The first ping is sent right away on connect
REQUIRE(serverReceivedPingMessages == 2);
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 setPingInterval
TEST_CASE("Websocket_ping_no_data_sent_setHeartBeatPeriod", "[setPingInterval]")
{
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();
WebSocketClient webSocketClient(port);
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(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);
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", "[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();
WebSocketClient webSocketClient(port);
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
}
}