2019-01-07 03:54:16 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketTestConnectionDisconnection.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <set>
|
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include "IXTest.h"
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
|
|
|
|
|
|
|
using namespace ix;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2019-01-07 20:28:53 +01:00
|
|
|
const std::string WEBSOCKET_DOT_ORG_URL("wss://echo.websocket.org");
|
2019-01-07 03:54:16 +01:00
|
|
|
const std::string GOOGLE_URL("wss://google.com");
|
|
|
|
const std::string UNKNOWN_URL("wss://asdcasdcaasdcasdcasdcasdcasdcasdcasassdd.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class IXWebSocketTestConnectionDisconnection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IXWebSocketTestConnectionDisconnection();
|
|
|
|
void start(const std::string& url);
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ix::WebSocket _webSocket;
|
|
|
|
};
|
|
|
|
|
|
|
|
IXWebSocketTestConnectionDisconnection::IXWebSocketTestConnectionDisconnection()
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IXWebSocketTestConnectionDisconnection::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IXWebSocketTestConnectionDisconnection::start(const std::string& url)
|
|
|
|
{
|
|
|
|
_webSocket.setUrl(url);
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
log(std::string("Connecting to url: ") + url);
|
|
|
|
|
|
|
|
_webSocket.setOnMessageCallback(
|
2019-06-09 20:33:17 +02:00
|
|
|
[](const ix::WebSocketMessagePtr& msg)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: connected !");
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: disconnected !");
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
ss << "TestConnectionDisconnection: Error! ";
|
2019-06-09 20:33:17 +02:00
|
|
|
ss << msg->errorInfo.reason;
|
2019-05-09 18:21:05 +02:00
|
|
|
log(ss.str());
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: received message.!");
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Ping)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: received ping message.!");
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: received pong message.!");
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
2019-05-09 18:21:05 +02:00
|
|
|
{
|
2019-05-14 02:32:57 +02:00
|
|
|
log("TestConnectionDisconnection: received fragment.!");
|
2019-05-09 18:21:05 +02:00
|
|
|
}
|
2019-01-07 03:54:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
log("Invalid ix::WebSocketMessageType");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-14 20:26:37 +02:00
|
|
|
_webSocket.enableAutomaticReconnection();
|
|
|
|
REQUIRE(_webSocket.isAutomaticReconnectionEnabled() == true);
|
|
|
|
|
|
|
|
_webSocket.disableAutomaticReconnection();
|
|
|
|
REQUIRE(_webSocket.isAutomaticReconnectionEnabled() == false);
|
|
|
|
|
2019-01-07 03:54:16 +01:00
|
|
|
// Start the connection
|
|
|
|
_webSocket.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// We try to connect to different servers, and make sure there are no crashes.
|
|
|
|
// FIXME: We could do more checks (make sure that we were not able to connect to unknown servers, etc...)
|
|
|
|
//
|
|
|
|
TEST_CASE("websocket_connections", "[websocket]")
|
|
|
|
{
|
|
|
|
SECTION("Try to connect to invalid servers.")
|
|
|
|
{
|
2019-05-11 21:20:58 +02:00
|
|
|
IXWebSocketTestConnectionDisconnection test;
|
2019-01-07 03:54:16 +01:00
|
|
|
|
2019-05-11 21:20:58 +02:00
|
|
|
test.start(GOOGLE_URL);
|
2019-01-07 03:54:16 +01:00
|
|
|
ix::msleep(1000);
|
2019-05-11 21:20:58 +02:00
|
|
|
test.stop();
|
2019-01-07 03:54:16 +01:00
|
|
|
|
2019-05-11 21:20:58 +02:00
|
|
|
test.start(UNKNOWN_URL);
|
2019-01-07 03:54:16 +01:00
|
|
|
ix::msleep(1000);
|
2019-05-11 21:20:58 +02:00
|
|
|
test.stop();
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 18:33:18 +02:00
|
|
|
SECTION("Try to connect and disconnect with different timing, not enough time to succesfully connect")
|
2019-01-07 03:54:16 +01:00
|
|
|
{
|
2019-05-11 21:20:58 +02:00
|
|
|
IXWebSocketTestConnectionDisconnection test;
|
2019-05-16 03:43:57 +02:00
|
|
|
log(std::string("50 Runs"));
|
|
|
|
|
2019-01-07 03:54:16 +01:00
|
|
|
for (int i = 0; i < 50; ++i)
|
|
|
|
{
|
|
|
|
log(std::string("Run: ") + std::to_string(i));
|
2019-05-11 21:20:58 +02:00
|
|
|
test.start(WEBSOCKET_DOT_ORG_URL);
|
2019-05-16 03:43:57 +02:00
|
|
|
|
|
|
|
log(std::string("Sleeping"));
|
2019-01-07 03:54:16 +01:00
|
|
|
ix::msleep(i);
|
2019-05-16 03:43:57 +02:00
|
|
|
|
|
|
|
log(std::string("Stopping"));
|
2019-05-11 21:20:58 +02:00
|
|
|
test.stop();
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 18:33:18 +02:00
|
|
|
|
2019-05-11 21:25:40 +02:00
|
|
|
// This test breaks on travis CI - Ubuntu Xenial + gcc + tsan
|
|
|
|
// We should fix this.
|
2019-05-16 01:01:05 +02:00
|
|
|
SECTION("Try to connect and disconnect with different timing, from not enough time to successfull connect")
|
2019-05-09 18:33:18 +02:00
|
|
|
{
|
2019-05-11 21:20:58 +02:00
|
|
|
IXWebSocketTestConnectionDisconnection test;
|
2019-05-16 03:43:57 +02:00
|
|
|
log(std::string("20 Runs"));
|
|
|
|
|
2019-05-09 18:33:18 +02:00
|
|
|
for (int i = 0; i < 20; ++i)
|
|
|
|
{
|
|
|
|
log(std::string("Run: ") + std::to_string(i));
|
2019-05-11 21:20:58 +02:00
|
|
|
test.start(WEBSOCKET_DOT_ORG_URL);
|
2019-05-16 03:43:57 +02:00
|
|
|
|
|
|
|
log(std::string("Sleeping"));
|
2019-05-09 18:33:18 +02:00
|
|
|
ix::msleep(i*50);
|
2019-05-16 03:43:57 +02:00
|
|
|
|
|
|
|
log(std::string("Stopping"));
|
2019-05-11 21:20:58 +02:00
|
|
|
test.stop();
|
2019-05-09 18:33:18 +02:00
|
|
|
}
|
2019-05-16 01:01:05 +02:00
|
|
|
}
|
2019-01-07 03:54:16 +01:00
|
|
|
}
|