Commit b7d6f0669fd03f0b6ad41326a039d3c41193ee6a
1 parent
00dd7943
Make tests more stable, if not fully stable
Three parts to it: - Start the app fresh per test. This avoids annoyances like getting retained messages on subscribe, messing up the test. - Waiting for suback was apparently necessary. - Because the Qt event loop was given time, waiting for publishes was sometimes pointless because it had already arrived. So, checking the receive list first.
Showing
5 changed files
with
54 additions
and
16 deletions
FlashMQTests/mainappthread.cpp
| ... | ... | @@ -19,6 +19,12 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>. |
| 19 | 19 | |
| 20 | 20 | MainAppThread::MainAppThread(QObject *parent) : QThread(parent) |
| 21 | 21 | { |
| 22 | + if (appInstance) | |
| 23 | + { | |
| 24 | + delete appInstance; | |
| 25 | + } | |
| 26 | + appInstance = nullptr; | |
| 27 | + MainApp::instance = nullptr; | |
| 22 | 28 | MainApp::initMainApp(1, nullptr); |
| 23 | 29 | appInstance = MainApp::getMainApp(); |
| 24 | 30 | appInstance->settings->allowAnonymous = true; | ... | ... |
FlashMQTests/tst_maintests.cpp
| ... | ... | @@ -48,13 +48,16 @@ class MainTests : public QObject |
| 48 | 48 | { |
| 49 | 49 | Q_OBJECT |
| 50 | 50 | |
| 51 | - MainAppThread mainApp; | |
| 51 | + QScopedPointer<MainAppThread> mainApp; | |
| 52 | 52 | |
| 53 | 53 | public: |
| 54 | 54 | MainTests(); |
| 55 | 55 | ~MainTests(); |
| 56 | 56 | |
| 57 | 57 | private slots: |
| 58 | + void init(); | |
| 59 | + void cleanup(); | |
| 60 | + | |
| 58 | 61 | void cleanupTestCase(); |
| 59 | 62 | |
| 60 | 63 | void test_circbuf(); |
| ... | ... | @@ -89,8 +92,7 @@ private slots: |
| 89 | 92 | |
| 90 | 93 | MainTests::MainTests() |
| 91 | 94 | { |
| 92 | - mainApp.start(); | |
| 93 | - mainApp.waitForStarted(); | |
| 95 | + | |
| 94 | 96 | } |
| 95 | 97 | |
| 96 | 98 | MainTests::~MainTests() |
| ... | ... | @@ -98,9 +100,21 @@ MainTests::~MainTests() |
| 98 | 100 | |
| 99 | 101 | } |
| 100 | 102 | |
| 103 | +void MainTests::init() | |
| 104 | +{ | |
| 105 | + mainApp.reset(new MainAppThread()); | |
| 106 | + mainApp->start(); | |
| 107 | + mainApp->waitForStarted(); | |
| 108 | +} | |
| 109 | + | |
| 110 | +void MainTests::cleanup() | |
| 111 | +{ | |
| 112 | + mainApp->stopApp(); | |
| 113 | +} | |
| 114 | + | |
| 101 | 115 | void MainTests::cleanupTestCase() |
| 102 | 116 | { |
| 103 | - mainApp.stopApp(); | |
| 117 | + | |
| 104 | 118 | } |
| 105 | 119 | |
| 106 | 120 | void MainTests::test_circbuf() |
| ... | ... | @@ -350,9 +364,9 @@ void MainTests::test_retained() |
| 350 | 364 | testContext.connectReceiver(); |
| 351 | 365 | testContext.subscribeReceiver("dummy"); |
| 352 | 366 | testContext.subscribeReceiver(topic); |
| 353 | - testContext.waitReceiverReceived(); | |
| 367 | + testContext.waitReceiverReceived(1); | |
| 354 | 368 | |
| 355 | - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); | |
| 369 | + QCOMPARE(testContext.receivedMessages.count(), 1); | |
| 356 | 370 | |
| 357 | 371 | QMQTT::Message msg = testContext.receivedMessages.first(); |
| 358 | 372 | QCOMPARE(msg.payload(), payload); |
| ... | ... | @@ -361,7 +375,7 @@ void MainTests::test_retained() |
| 361 | 375 | testContext.receivedMessages.clear(); |
| 362 | 376 | |
| 363 | 377 | testContext.publish(topic, payload, true); |
| 364 | - testContext.waitReceiverReceived(); | |
| 378 | + testContext.waitReceiverReceived(1); | |
| 365 | 379 | |
| 366 | 380 | QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); |
| 367 | 381 | QMQTT::Message msg2 = testContext.receivedMessages.first(); |
| ... | ... | @@ -385,9 +399,9 @@ void MainTests::test_retained_changed() |
| 385 | 399 | |
| 386 | 400 | testContext.connectReceiver(); |
| 387 | 401 | testContext.subscribeReceiver(topic); |
| 388 | - testContext.waitReceiverReceived(); | |
| 402 | + testContext.waitReceiverReceived(1); | |
| 389 | 403 | |
| 390 | - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); | |
| 404 | + QCOMPARE(testContext.receivedMessages.count(), 1); | |
| 391 | 405 | |
| 392 | 406 | QMQTT::Message msg = testContext.receivedMessages.first(); |
| 393 | 407 | QCOMPARE(msg.payload(), payload); |
| ... | ... | @@ -410,7 +424,7 @@ void MainTests::test_retained_removed() |
| 410 | 424 | |
| 411 | 425 | testContext.connectReceiver(); |
| 412 | 426 | testContext.subscribeReceiver(topic); |
| 413 | - testContext.waitReceiverReceived(); | |
| 427 | + testContext.waitReceiverReceived(0); | |
| 414 | 428 | |
| 415 | 429 | QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any."); |
| 416 | 430 | } |
| ... | ... | @@ -427,9 +441,9 @@ void MainTests::test_packet_bigger_than_one_doubling() |
| 427 | 441 | testContext.subscribeReceiver(topic); |
| 428 | 442 | |
| 429 | 443 | testContext.publish(topic, payload); |
| 430 | - testContext.waitReceiverReceived(); | |
| 444 | + testContext.waitReceiverReceived(1); | |
| 431 | 445 | |
| 432 | - QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); | |
| 446 | + QCOMPARE(testContext.receivedMessages.count(), 1); | |
| 433 | 447 | |
| 434 | 448 | QMQTT::Message msg = testContext.receivedMessages.first(); |
| 435 | 449 | QCOMPARE(msg.payload(), payload); |
| ... | ... | @@ -449,7 +463,7 @@ void MainTests::test_very_big_packet() |
| 449 | 463 | testContext.subscribeReceiver(topic); |
| 450 | 464 | |
| 451 | 465 | testContext.publish(topic, payload); |
| 452 | - testContext.waitReceiverReceived(); | |
| 466 | + testContext.waitReceiverReceived(1); | |
| 453 | 467 | |
| 454 | 468 | QCOMPARE(testContext.receivedMessages.count(), 1); |
| 455 | 469 | ... | ... |
FlashMQTests/twoclienttestcontext.cpp
| ... | ... | @@ -27,7 +27,9 @@ TwoClientTestContext::TwoClientTestContext(QObject *parent) : QObject(parent) |
| 27 | 27 | QHostInfo targetHostInfo = QHostInfo::fromName("localhost"); |
| 28 | 28 | QHostAddress targetHost(targetHostInfo.addresses().first()); |
| 29 | 29 | sender.reset(new QMQTT::Client(targetHost)); |
| 30 | + sender->setClientId("Sender"); | |
| 30 | 31 | receiver.reset(new QMQTT::Client(targetHost)); |
| 32 | + receiver->setClientId("Receiver"); | |
| 31 | 33 | |
| 32 | 34 | connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); |
| 33 | 35 | connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); |
| ... | ... | @@ -72,14 +74,26 @@ void TwoClientTestContext::disconnectReceiver() |
| 72 | 74 | void TwoClientTestContext::subscribeReceiver(const QString &topic) |
| 73 | 75 | { |
| 74 | 76 | receiver->subscribe(topic); |
| 77 | + | |
| 78 | + QEventLoop waiter; | |
| 79 | + QTimer timeout; | |
| 80 | + timeout.setSingleShot(true); | |
| 81 | + timeout.setInterval(1000); | |
| 82 | + connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); | |
| 83 | + connect(receiver.data(), &QMQTT::Client::subscribed, &waiter, &QEventLoop::quit); | |
| 84 | + timeout.start(); | |
| 85 | + waiter.exec(); | |
| 75 | 86 | } |
| 76 | 87 | |
| 77 | -void TwoClientTestContext::waitReceiverReceived() | |
| 88 | +void TwoClientTestContext::waitReceiverReceived(int count) | |
| 78 | 89 | { |
| 90 | + if (count > 0 && receivedMessages.count() == count) | |
| 91 | + return; | |
| 92 | + | |
| 79 | 93 | QEventLoop waiter; |
| 80 | 94 | QTimer timeout; |
| 81 | 95 | timeout.setSingleShot(true); |
| 82 | - timeout.setInterval(1000); | |
| 96 | + timeout.setInterval(3000); | |
| 83 | 97 | connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); |
| 84 | 98 | connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); |
| 85 | 99 | timeout.start(); | ... | ... |
FlashMQTests/twoclienttestcontext.h
| ... | ... | @@ -39,7 +39,7 @@ public: |
| 39 | 39 | void connectReceiver(); |
| 40 | 40 | void disconnectReceiver(); |
| 41 | 41 | void subscribeReceiver(const QString &topic); |
| 42 | - void waitReceiverReceived(); | |
| 42 | + void waitReceiverReceived(int count); | |
| 43 | 43 | void onClientError(const QMQTT::ClientError error); |
| 44 | 44 | |
| 45 | 45 | QList<QMQTT::Message> receivedMessages; | ... | ... |
settings.h
| ... | ... | @@ -41,7 +41,11 @@ public: |
| 41 | 41 | bool authPluginSerializeAuthChecks = false; |
| 42 | 42 | int clientInitialBufferSize = 1024; // Must be power of 2 |
| 43 | 43 | int maxPacketSize = 268435461; // 256 MB + 5 |
| 44 | +#ifdef TESTING | |
| 45 | + bool logDebug = true; | |
| 46 | +#else | |
| 44 | 47 | bool logDebug = false; |
| 48 | +#endif | |
| 45 | 49 | bool logSubscriptions = false; |
| 46 | 50 | std::string mosquittoPasswordFile; |
| 47 | 51 | std::string mosquittoAclFile; | ... | ... |