diff --git a/FlashMQTests/FlashMQTests.pro b/FlashMQTests/FlashMQTests.pro index 53a2776..0775f19 100644 --- a/FlashMQTests/FlashMQTests.pro +++ b/FlashMQTests/FlashMQTests.pro @@ -1,7 +1,6 @@ QT += testlib QT -= gui QT += network -QT += qmqtt DEFINES += TESTING \ "FLASHMQ_VERSION=\\\"0.0.0\\\"" @@ -56,8 +55,7 @@ SOURCES += tst_maintests.cpp \ ../derivablecounter.cpp \ ../packetdatatypes.cpp \ ../flashmqtestclient.cpp \ - mainappthread.cpp \ - twoclienttestcontext.cpp + mainappthread.cpp HEADERS += \ @@ -104,8 +102,7 @@ HEADERS += \ ../derivablecounter.h \ ../packetdatatypes.h \ ../flashmqtestclient.h \ - mainappthread.h \ - twoclienttestcontext.h + mainappthread.h LIBS += -ldl -lssl -lcrypto diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 682e440..2596ba5 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -18,7 +18,6 @@ License along with FlashMQ. If not, see . #include -#include #include #include #include @@ -27,7 +26,6 @@ License along with FlashMQ. If not, see . #include "cirbuf.h" #include "mainapp.h" #include "mainappthread.h" -#include "twoclienttestcontext.h" #include "threadlocalutils.h" #include "retainedmessagesdb.h" #include "sessionsandsubscriptionsdb.h" diff --git a/FlashMQTests/twoclienttestcontext.cpp b/FlashMQTests/twoclienttestcontext.cpp deleted file mode 100644 index 512bd98..0000000 --- a/FlashMQTests/twoclienttestcontext.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* -This file is part of FlashMQ (https://www.flashmq.org) -Copyright (C) 2021 Wiebe Cazemier - -FlashMQ is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as -published by the Free Software Foundation, version 3. - -FlashMQ is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public -License along with FlashMQ. If not, see . -*/ - -#include "twoclienttestcontext.h" - -#include -#include - -// TODO: port to QMqttClient that newer Qts now have? - -TwoClientTestContext::TwoClientTestContext(int clientNr, QObject *parent) : QObject(parent) -{ - QHostInfo targetHostInfo = QHostInfo::fromName("localhost"); - QHostAddress targetHost(targetHostInfo.addresses().first()); - sender.reset(new QMQTT::Client(targetHost, 21883)); - sender->setClientId(QString("Sender%1").arg(clientNr)); - receiver.reset(new QMQTT::Client(targetHost, 21883)); - receiver->setClientId(QString("Receiver%1").arg(clientNr)); - - connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); - connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); -} - -void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload) -{ - publish(topic, payload, 0, false); -} - -void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload, bool retain) -{ - publish(topic, payload, 0, retain); -} - -void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload, const quint8 qos, bool retain) -{ - QMQTT::Message msg; - msg.setTopic(topic); - msg.setRetain(retain); - msg.setQos(qos); - msg.setPayload(payload); - sender->publish(msg); -} - -void TwoClientTestContext::connectSender() -{ - sender->connectToHost(); - QEventLoop waiter; - connect(sender.data(), &QMQTT::Client::connected, &waiter, &QEventLoop::quit); - waiter.exec(); -} - -void TwoClientTestContext::connectReceiver() -{ - connect(receiver.data(), &QMQTT::Client::received, this, &TwoClientTestContext::onReceiverReceived); - - receiver->connectToHost(); - QEventLoop waiter; - connect(receiver.data(), &QMQTT::Client::connected, &waiter, &QEventLoop::quit); - waiter.exec(); -} - -void TwoClientTestContext::disconnectReceiver() -{ - receiver->disconnectFromHost(); - QEventLoop waiter; - connect(sender.data(), &QMQTT::Client::disconnected, &waiter, &QEventLoop::quit); - waiter.exec(); -} - -void TwoClientTestContext::subscribeReceiver(const QString &topic, const quint8 qos) -{ - receiver->subscribe(topic, qos); - - QEventLoop waiter; - QTimer timeout; - timeout.setSingleShot(true); - timeout.setInterval(1000); - connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); - connect(receiver.data(), &QMQTT::Client::subscribed, &waiter, &QEventLoop::quit); - timeout.start(); - waiter.exec(); -} - -void TwoClientTestContext::unsubscribeReceiver(const QString &topic) -{ - receiver->unsubscribe(topic); - - QEventLoop waiter; - QTimer timeout; - timeout.setSingleShot(true); - timeout.setInterval(1000); - connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); - connect(receiver.data(), &QMQTT::Client::unsubscribed, &waiter, &QEventLoop::quit); - timeout.start(); - waiter.exec(); -} - -void TwoClientTestContext::waitReceiverReceived(const int count) -{ - if (count > 0 && receivedMessages.count() == count) - return; - - int attempt = 0; - while(receivedMessages.count() != count && attempt++ < count) - { - QEventLoop waiter; - QTimer timeout; - timeout.setSingleShot(true); - timeout.setInterval(3000); - connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); - connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); - timeout.start(); - waiter.exec(); - } -} - -void TwoClientTestContext::onClientError(const QMQTT::ClientError error) -{ - const QMQTT::Client *_sender = sender.data(); - - // TODO: arg, doesn't qmqtt have a better way for this? - QString errStr = QString("unknown error"); - if (error == QMQTT::SocketConnectionRefusedError) - errStr = "Connection refused"; - if (error == QMQTT::SocketRemoteHostClosedError) - errStr = "Remote host closed"; - if (error == QMQTT::SocketHostNotFoundError) - errStr = "Remote host not found"; - if (error == QMQTT::MqttBadUserNameOrPasswordError) - errStr = "MQTT bad user or password"; - if (error == QMQTT::MqttNotAuthorizedError) - errStr = "MQTT not authorized"; - if (error == QMQTT::SocketResourceError) - errStr = "Socket resource error. Is your OS limiting you? Ulimit, etc?"; - if (error == QMQTT::SocketSslInternalError) - errStr = "Socket SSL internal error."; - if (error == QMQTT::SocketTimeoutError) - errStr = "Socket timeout"; - - QString msg = QString("Client %1 error code: %2 (%3). Initiated delayed reconnect.\n").arg(_sender->clientId()).arg(error).arg(errStr); - throw std::runtime_error(msg.toStdString()); -} - -void TwoClientTestContext::onReceiverReceived(const QMQTT::Message &message) -{ - receivedMessages.append(message); -} diff --git a/FlashMQTests/twoclienttestcontext.h b/FlashMQTests/twoclienttestcontext.h deleted file mode 100644 index 68ddbf2..0000000 --- a/FlashMQTests/twoclienttestcontext.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -This file is part of FlashMQ (https://www.flashmq.org) -Copyright (C) 2021 Wiebe Cazemier - -FlashMQ is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as -published by the Free Software Foundation, version 3. - -FlashMQ is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public -License along with FlashMQ. If not, see . -*/ - -#ifndef RETAINTESTCONTEXT_H -#define RETAINTESTCONTEXT_H - -#include -#include -#include - -class TwoClientTestContext : public QObject -{ - Q_OBJECT - - QScopedPointer sender; - QScopedPointer receiver; - -private slots: - void onReceiverReceived(const QMQTT::Message& message); - -public: - explicit TwoClientTestContext(int clientNr = 0, QObject *parent = nullptr); - void publish(const QString &topic, const QByteArray &payload); - void publish(const QString &topic, const QByteArray &payload, bool retain); - void publish(const QString &topic, const QByteArray &payload, const quint8 qos, bool retain); - void connectSender(); - void connectReceiver(); - void disconnectReceiver(); - void subscribeReceiver(const QString &topic, const quint8 qos = 0); - void unsubscribeReceiver(const QString &topic); - void waitReceiverReceived(const int count); - void onClientError(const QMQTT::ClientError error); - - QList receivedMessages; - -signals: - -}; - -#endif // RETAINTESTCONTEXT_H