From b7d6f0669fd03f0b6ad41326a039d3c41193ee6a Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Tue, 15 Jun 2021 17:12:02 +0200 Subject: [PATCH] Make tests more stable, if not fully stable --- FlashMQTests/mainappthread.cpp | 6 ++++++ FlashMQTests/tst_maintests.cpp | 40 +++++++++++++++++++++++++++------------- FlashMQTests/twoclienttestcontext.cpp | 18 ++++++++++++++++-- FlashMQTests/twoclienttestcontext.h | 2 +- settings.h | 4 ++++ 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/FlashMQTests/mainappthread.cpp b/FlashMQTests/mainappthread.cpp index e4dd29d..974ae92 100644 --- a/FlashMQTests/mainappthread.cpp +++ b/FlashMQTests/mainappthread.cpp @@ -19,6 +19,12 @@ License along with FlashMQ. If not, see . MainAppThread::MainAppThread(QObject *parent) : QThread(parent) { + if (appInstance) + { + delete appInstance; + } + appInstance = nullptr; + MainApp::instance = nullptr; MainApp::initMainApp(1, nullptr); appInstance = MainApp::getMainApp(); appInstance->settings->allowAnonymous = true; diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 358218d..eaa51a4 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -48,13 +48,16 @@ class MainTests : public QObject { Q_OBJECT - MainAppThread mainApp; + QScopedPointer mainApp; public: MainTests(); ~MainTests(); private slots: + void init(); + void cleanup(); + void cleanupTestCase(); void test_circbuf(); @@ -89,8 +92,7 @@ private slots: MainTests::MainTests() { - mainApp.start(); - mainApp.waitForStarted(); + } MainTests::~MainTests() @@ -98,9 +100,21 @@ MainTests::~MainTests() } +void MainTests::init() +{ + mainApp.reset(new MainAppThread()); + mainApp->start(); + mainApp->waitForStarted(); +} + +void MainTests::cleanup() +{ + mainApp->stopApp(); +} + void MainTests::cleanupTestCase() { - mainApp.stopApp(); + } void MainTests::test_circbuf() @@ -350,9 +364,9 @@ void MainTests::test_retained() testContext.connectReceiver(); testContext.subscribeReceiver("dummy"); testContext.subscribeReceiver(topic); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(1); - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); + QCOMPARE(testContext.receivedMessages.count(), 1); QMQTT::Message msg = testContext.receivedMessages.first(); QCOMPARE(msg.payload(), payload); @@ -361,7 +375,7 @@ void MainTests::test_retained() testContext.receivedMessages.clear(); testContext.publish(topic, payload, true); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(1); QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); QMQTT::Message msg2 = testContext.receivedMessages.first(); @@ -385,9 +399,9 @@ void MainTests::test_retained_changed() testContext.connectReceiver(); testContext.subscribeReceiver(topic); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(1); - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); + QCOMPARE(testContext.receivedMessages.count(), 1); QMQTT::Message msg = testContext.receivedMessages.first(); QCOMPARE(msg.payload(), payload); @@ -410,7 +424,7 @@ void MainTests::test_retained_removed() testContext.connectReceiver(); testContext.subscribeReceiver(topic); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(0); QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any."); } @@ -427,9 +441,9 @@ void MainTests::test_packet_bigger_than_one_doubling() testContext.subscribeReceiver(topic); testContext.publish(topic, payload); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(1); - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); + QCOMPARE(testContext.receivedMessages.count(), 1); QMQTT::Message msg = testContext.receivedMessages.first(); QCOMPARE(msg.payload(), payload); @@ -449,7 +463,7 @@ void MainTests::test_very_big_packet() testContext.subscribeReceiver(topic); testContext.publish(topic, payload); - testContext.waitReceiverReceived(); + testContext.waitReceiverReceived(1); QCOMPARE(testContext.receivedMessages.count(), 1); diff --git a/FlashMQTests/twoclienttestcontext.cpp b/FlashMQTests/twoclienttestcontext.cpp index f5a6567..13010b0 100644 --- a/FlashMQTests/twoclienttestcontext.cpp +++ b/FlashMQTests/twoclienttestcontext.cpp @@ -27,7 +27,9 @@ TwoClientTestContext::TwoClientTestContext(QObject *parent) : QObject(parent) QHostInfo targetHostInfo = QHostInfo::fromName("localhost"); QHostAddress targetHost(targetHostInfo.addresses().first()); sender.reset(new QMQTT::Client(targetHost)); + sender->setClientId("Sender"); receiver.reset(new QMQTT::Client(targetHost)); + receiver->setClientId("Receiver"); connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); @@ -72,14 +74,26 @@ void TwoClientTestContext::disconnectReceiver() void TwoClientTestContext::subscribeReceiver(const QString &topic) { receiver->subscribe(topic); + + 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::waitReceiverReceived() +void TwoClientTestContext::waitReceiverReceived(int count) { + if (count > 0 && receivedMessages.count() == count) + return; + QEventLoop waiter; QTimer timeout; timeout.setSingleShot(true); - timeout.setInterval(1000); + timeout.setInterval(3000); connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); timeout.start(); diff --git a/FlashMQTests/twoclienttestcontext.h b/FlashMQTests/twoclienttestcontext.h index 355a54e..d1aada8 100644 --- a/FlashMQTests/twoclienttestcontext.h +++ b/FlashMQTests/twoclienttestcontext.h @@ -39,7 +39,7 @@ public: void connectReceiver(); void disconnectReceiver(); void subscribeReceiver(const QString &topic); - void waitReceiverReceived(); + void waitReceiverReceived(int count); void onClientError(const QMQTT::ClientError error); QList receivedMessages; diff --git a/settings.h b/settings.h index 476c3a7..d79e486 100644 --- a/settings.h +++ b/settings.h @@ -41,7 +41,11 @@ public: bool authPluginSerializeAuthChecks = false; int clientInitialBufferSize = 1024; // Must be power of 2 int maxPacketSize = 268435461; // 256 MB + 5 +#ifdef TESTING + bool logDebug = true; +#else bool logDebug = false; +#endif bool logSubscriptions = false; std::string mosquittoPasswordFile; std::string mosquittoAclFile; -- libgit2 0.21.4