2019-01-25 03:50:07 +01:00
|
|
|
/*
|
2019-04-18 18:24:16 +02: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:
|
2019-04-18 18:24:16 +02:00
|
|
|
WebSocketClient(int port, bool useHeartBeatMethod);
|
2019-01-25 03:50:07 +01:00
|
|
|
|
|
|
|
void subscribe(const std::string& channel);
|
|
|
|
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;
|
2019-04-18 18:24:16 +02:00
|
|
|
bool _useHeartBeatMethod;
|
2019-01-25 03:50:07 +01:00
|
|
|
};
|
|
|
|
|
2019-04-18 18:24:16 +02: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::WebSocket_ReadyState_Open;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::start()
|
|
|
|
{
|
|
|
|
std::string url;
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "ws://localhost:"
|
2019-02-21 03:59:07 +01:00
|
|
|
<< _port
|
2019-01-25 03:50:07 +01:00
|
|
|
<< "/";
|
|
|
|
|
|
|
|
url = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
_webSocket.setUrl(url);
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
// The important bit for this test.
|
2019-04-18 18:24:16 +02:00
|
|
|
// 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::WebSocket_MessageType_Open)
|
|
|
|
{
|
|
|
|
log("client connected");
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
|
|
{
|
|
|
|
log("client disconnected");
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
|
|
|
{
|
|
|
|
ss << "Error ! " << error.reason;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Pong)
|
|
|
|
{
|
|
|
|
ss << "Received pong message " << str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Ping)
|
|
|
|
{
|
|
|
|
ss << "Received ping message " << str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-01-26 01:11:39 +01:00
|
|
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
|
|
{
|
|
|
|
ss << "Received message " << str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
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(
|
2019-03-21 02:34:24 +01:00
|
|
|
[&server, &receivedPingMessages](std::shared_ptr<ix::WebSocket> webSocket,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
2019-01-25 03:50:07 +01:00
|
|
|
{
|
|
|
|
webSocket->setOnMessageCallback(
|
2019-03-21 02:34:24 +01:00
|
|
|
[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::WebSocket_MessageType_Open)
|
|
|
|
{
|
2019-01-26 01:11:39 +01:00
|
|
|
Logger() << "New server connection";
|
2019-03-21 02:34:24 +01:00
|
|
|
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::WebSocket_MessageType_Close)
|
|
|
|
{
|
2019-01-26 01:11:39 +01:00
|
|
|
log("Server closed connection");
|
2019-01-25 03:50:07 +01:00
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Ping)
|
|
|
|
{
|
2019-01-26 01:11:39 +01:00
|
|
|
log("Server received a ping");
|
2019-01-25 03:50:07 +01:00
|
|
|
receivedPingMessages++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
log(res.second);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
TEST_CASE("Websocket_ping_no_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod]")
|
2019-01-25 03:50:07 +01:00
|
|
|
{
|
2019-04-18 18:24:16 +02: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();
|
|
|
|
|
2019-01-29 00:14:49 +01:00
|
|
|
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();
|
2019-04-18 18:24:16 +02:00
|
|
|
bool useSetHeartBeatPeriodMethod = true;
|
|
|
|
WebSocketClient webSocketClient(port, useSetHeartBeatPeriodMethod);
|
2019-01-25 03:50:07 +01:00
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocketClient.start();
|
2019-01-25 03:50:07 +01:00
|
|
|
|
|
|
|
// Wait for all chat instance to be ready
|
|
|
|
while (true)
|
|
|
|
{
|
2019-04-18 18:24:16 +02:00
|
|
|
if (webSocketClient.isReady()) break;
|
2019-01-25 03:50:07 +01:00
|
|
|
ix::msleep(10);
|
|
|
|
}
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
REQUIRE(server.getClients().size() == 1);
|
|
|
|
|
|
|
|
ix::msleep(1900);
|
|
|
|
|
|
|
|
webSocketClient.stop();
|
|
|
|
|
|
|
|
|
2019-04-20 01:57:38 +02:00
|
|
|
// Here we test ping interval
|
2019-04-18 18:24:16 +02:00
|
|
|
// -> expected ping messages == 1 as 1900 seconds, 1 ping sent every second
|
|
|
|
REQUIRE(serverReceivedPingMessages == 1);
|
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
|
|
|
|
ix::reportWebSocketTraffic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(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);
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocketClient.sendMessage("hello world");
|
2019-01-26 01:11:39 +01:00
|
|
|
ix::msleep(900);
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocketClient.sendMessage("hello world");
|
|
|
|
ix::msleep(1100);
|
|
|
|
|
|
|
|
webSocketClient.stop();
|
|
|
|
|
2019-04-20 01:57:38 +02:00
|
|
|
// Here we test ping interval
|
2019-04-18 18:24:16 +02:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
|
|
|
|
ix::reportWebSocketTraffic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Websocket_ping_no_data_sent_setPingInterval", "[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();
|
|
|
|
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);
|
|
|
|
|
|
|
|
ix::msleep(2100);
|
|
|
|
|
|
|
|
webSocketClient.stop();
|
|
|
|
|
|
|
|
|
2019-04-20 01:57:38 +02:00
|
|
|
// Here we test ping interval
|
2019-04-18 18:24:16 +02:00
|
|
|
// -> expected ping messages == 2 as 2100 seconds, 1 ping sent every second
|
|
|
|
REQUIRE(serverReceivedPingMessages == 2);
|
2019-01-25 03:50:07 +01:00
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
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);
|
|
|
|
|
|
|
|
ix::msleep(900);
|
|
|
|
webSocketClient.sendMessage("hello world");
|
|
|
|
ix::msleep(900);
|
|
|
|
webSocketClient.sendMessage("hello world");
|
|
|
|
ix::msleep(1300);
|
2019-01-25 03:50:07 +01:00
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocketClient.stop();
|
2019-03-21 21:50:59 +01:00
|
|
|
|
2019-04-20 01:57:38 +02:00
|
|
|
// Here we test ping interval
|
2019-04-18 18:24:16 +02:00
|
|
|
// 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);
|
2019-01-25 03:50:07 +01:00
|
|
|
|
|
|
|
// Give us 500ms for the server to notice that clients went away
|
|
|
|
ix::msleep(500);
|
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
|
|
|
|
ix::reportWebSocketTraffic();
|
|
|
|
}
|
|
|
|
}
|