diff --git a/tests/ServerTests.cpp b/tests/ServerTests.cpp index 7d3340a..75ca034 100644 --- a/tests/ServerTests.cpp +++ b/tests/ServerTests.cpp @@ -24,7 +24,11 @@ #include #include +#include #include +#include +#include +#include class ServerTests : public ::testing::Test { @@ -32,6 +36,8 @@ class ServerTests : public ::testing::Test MyServer server; MySocket client; + QTimer timer; + void echoTest(quint16 port) { QSignalSpy connectedSpy(&client, &MySocket::isConnectedChanged); @@ -55,6 +61,62 @@ class ServerTests : public ::testing::Test ASSERT_EQ(s, QString("My String")); } } + + int connectedCount = 0; + bool clientSendError = false; + bool serverSendError = false; + + void runFuzzClientDisconnection(quint16 port) + { + server.sendError = serverSendError; + + QSignalSpy newClientSpy(&server, &MyServer::objectInserted); + client.setWatchdogPeriod(10); + // Send Echo counter every seconds + QObject::connect(&timer, &QTimer::timeout, + [this]() + { + if(client.isConnected()) + { + if(clientSendError) + { + qDebug() << "Send Error"; + Q_EMIT client.sendErrorString(); + } + else + { + Q_EMIT client.sendString("Hello World"); + } + } + }); + QObject::connect(&client, &MySocket::isConnectedChanged, + [this](bool value) + { + if(value) + { + ++connectedCount; + qDebug() << "Client Connected on port " << client.localPort(); + } + }); + server.onInserted([this](net::tcp::Socket* s) + { qDebug() << "New Client connected " << s->peerAddress() << ":" << s->peerPort(); }); + + // server.start(port) can be called to listen from every interfaces + server.start("127.0.0.1", port); + + client.setWatchdogPeriod(1); + client.start("127.0.0.1", port); + + timer.start(10); + + QTest::qWait(10000); + timer.stop(); + // Give time to finish last connection + QTest::qWait(100); + qInfo() << "Connected Count : " << connectedCount; + ASSERT_TRUE(connectedCount > 100); + ASSERT_EQ(newClientSpy.count(), connectedCount); + } }; TEST_F(ServerTests, echoTestMonoThread) @@ -84,3 +146,75 @@ TEST_F(ServerTests, echoTestWorkerThreadClientServer) client.setUseWorkerThread(true); echoTest(30003); } + +TEST_F(ServerTests, fuzzDisconnectionClientServer) +{ + clientSendError = true; + serverSendError = false; + server.setUseWorkerThread(false); + client.setUseWorkerThread(false); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadClient) +{ + clientSendError = true; + serverSendError = false; + server.setUseWorkerThread(true); + client.setUseWorkerThread(false); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadServer) +{ + clientSendError = true; + serverSendError = false; + server.setUseWorkerThread(false); + client.setUseWorkerThread(true); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadClientServer) +{ + clientSendError = true; + serverSendError = false; + server.setUseWorkerThread(true); + client.setUseWorkerThread(true); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionServerClient) +{ + clientSendError = false; + serverSendError = true; + server.setUseWorkerThread(false); + client.setUseWorkerThread(false); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadClient) +{ + clientSendError = false; + serverSendError = true; + server.setUseWorkerThread(true); + client.setUseWorkerThread(false); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadServer) +{ + clientSendError = false; + serverSendError = true; + server.setUseWorkerThread(false); + client.setUseWorkerThread(true); + runFuzzClientDisconnection(30004); +} + +TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadClientServer) +{ + clientSendError = false; + serverSendError = true; + server.setUseWorkerThread(true); + client.setUseWorkerThread(true); + runFuzzClientDisconnection(30004); +}