You need to sign in before continuing.

Commit b7d6f0669fd03f0b6ad41326a039d3c41193ee6a

Authored by Wiebe Cazemier
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.
FlashMQTests/mainappthread.cpp
@@ -19,6 +19,12 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>. @@ -19,6 +19,12 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
19 19
20 MainAppThread::MainAppThread(QObject *parent) : QThread(parent) 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 MainApp::initMainApp(1, nullptr); 28 MainApp::initMainApp(1, nullptr);
23 appInstance = MainApp::getMainApp(); 29 appInstance = MainApp::getMainApp();
24 appInstance->settings->allowAnonymous = true; 30 appInstance->settings->allowAnonymous = true;
FlashMQTests/tst_maintests.cpp
@@ -48,13 +48,16 @@ class MainTests : public QObject @@ -48,13 +48,16 @@ class MainTests : public QObject
48 { 48 {
49 Q_OBJECT 49 Q_OBJECT
50 50
51 - MainAppThread mainApp; 51 + QScopedPointer<MainAppThread> mainApp;
52 52
53 public: 53 public:
54 MainTests(); 54 MainTests();
55 ~MainTests(); 55 ~MainTests();
56 56
57 private slots: 57 private slots:
  58 + void init();
  59 + void cleanup();
  60 +
58 void cleanupTestCase(); 61 void cleanupTestCase();
59 62
60 void test_circbuf(); 63 void test_circbuf();
@@ -89,8 +92,7 @@ private slots: @@ -89,8 +92,7 @@ private slots:
89 92
90 MainTests::MainTests() 93 MainTests::MainTests()
91 { 94 {
92 - mainApp.start();  
93 - mainApp.waitForStarted(); 95 +
94 } 96 }
95 97
96 MainTests::~MainTests() 98 MainTests::~MainTests()
@@ -98,9 +100,21 @@ 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 void MainTests::cleanupTestCase() 115 void MainTests::cleanupTestCase()
102 { 116 {
103 - mainApp.stopApp(); 117 +
104 } 118 }
105 119
106 void MainTests::test_circbuf() 120 void MainTests::test_circbuf()
@@ -350,9 +364,9 @@ void MainTests::test_retained() @@ -350,9 +364,9 @@ void MainTests::test_retained()
350 testContext.connectReceiver(); 364 testContext.connectReceiver();
351 testContext.subscribeReceiver("dummy"); 365 testContext.subscribeReceiver("dummy");
352 testContext.subscribeReceiver(topic); 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 QMQTT::Message msg = testContext.receivedMessages.first(); 371 QMQTT::Message msg = testContext.receivedMessages.first();
358 QCOMPARE(msg.payload(), payload); 372 QCOMPARE(msg.payload(), payload);
@@ -361,7 +375,7 @@ void MainTests::test_retained() @@ -361,7 +375,7 @@ void MainTests::test_retained()
361 testContext.receivedMessages.clear(); 375 testContext.receivedMessages.clear();
362 376
363 testContext.publish(topic, payload, true); 377 testContext.publish(topic, payload, true);
364 - testContext.waitReceiverReceived(); 378 + testContext.waitReceiverReceived(1);
365 379
366 QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list"); 380 QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list");
367 QMQTT::Message msg2 = testContext.receivedMessages.first(); 381 QMQTT::Message msg2 = testContext.receivedMessages.first();
@@ -385,9 +399,9 @@ void MainTests::test_retained_changed() @@ -385,9 +399,9 @@ void MainTests::test_retained_changed()
385 399
386 testContext.connectReceiver(); 400 testContext.connectReceiver();
387 testContext.subscribeReceiver(topic); 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 QMQTT::Message msg = testContext.receivedMessages.first(); 406 QMQTT::Message msg = testContext.receivedMessages.first();
393 QCOMPARE(msg.payload(), payload); 407 QCOMPARE(msg.payload(), payload);
@@ -410,7 +424,7 @@ void MainTests::test_retained_removed() @@ -410,7 +424,7 @@ void MainTests::test_retained_removed()
410 424
411 testContext.connectReceiver(); 425 testContext.connectReceiver();
412 testContext.subscribeReceiver(topic); 426 testContext.subscribeReceiver(topic);
413 - testContext.waitReceiverReceived(); 427 + testContext.waitReceiverReceived(0);
414 428
415 QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any."); 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,9 +441,9 @@ void MainTests::test_packet_bigger_than_one_doubling()
427 testContext.subscribeReceiver(topic); 441 testContext.subscribeReceiver(topic);
428 442
429 testContext.publish(topic, payload); 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 QMQTT::Message msg = testContext.receivedMessages.first(); 448 QMQTT::Message msg = testContext.receivedMessages.first();
435 QCOMPARE(msg.payload(), payload); 449 QCOMPARE(msg.payload(), payload);
@@ -449,7 +463,7 @@ void MainTests::test_very_big_packet() @@ -449,7 +463,7 @@ void MainTests::test_very_big_packet()
449 testContext.subscribeReceiver(topic); 463 testContext.subscribeReceiver(topic);
450 464
451 testContext.publish(topic, payload); 465 testContext.publish(topic, payload);
452 - testContext.waitReceiverReceived(); 466 + testContext.waitReceiverReceived(1);
453 467
454 QCOMPARE(testContext.receivedMessages.count(), 1); 468 QCOMPARE(testContext.receivedMessages.count(), 1);
455 469
FlashMQTests/twoclienttestcontext.cpp
@@ -27,7 +27,9 @@ TwoClientTestContext::TwoClientTestContext(QObject *parent) : QObject(parent) @@ -27,7 +27,9 @@ TwoClientTestContext::TwoClientTestContext(QObject *parent) : QObject(parent)
27 QHostInfo targetHostInfo = QHostInfo::fromName("localhost"); 27 QHostInfo targetHostInfo = QHostInfo::fromName("localhost");
28 QHostAddress targetHost(targetHostInfo.addresses().first()); 28 QHostAddress targetHost(targetHostInfo.addresses().first());
29 sender.reset(new QMQTT::Client(targetHost)); 29 sender.reset(new QMQTT::Client(targetHost));
  30 + sender->setClientId("Sender");
30 receiver.reset(new QMQTT::Client(targetHost)); 31 receiver.reset(new QMQTT::Client(targetHost));
  32 + receiver->setClientId("Receiver");
31 33
32 connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); 34 connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError);
33 connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError); 35 connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError);
@@ -72,14 +74,26 @@ void TwoClientTestContext::disconnectReceiver() @@ -72,14 +74,26 @@ void TwoClientTestContext::disconnectReceiver()
72 void TwoClientTestContext::subscribeReceiver(const QString &topic) 74 void TwoClientTestContext::subscribeReceiver(const QString &topic)
73 { 75 {
74 receiver->subscribe(topic); 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 QEventLoop waiter; 93 QEventLoop waiter;
80 QTimer timeout; 94 QTimer timeout;
81 timeout.setSingleShot(true); 95 timeout.setSingleShot(true);
82 - timeout.setInterval(1000); 96 + timeout.setInterval(3000);
83 connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); 97 connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
84 connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); 98 connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit);
85 timeout.start(); 99 timeout.start();
FlashMQTests/twoclienttestcontext.h
@@ -39,7 +39,7 @@ public: @@ -39,7 +39,7 @@ public:
39 void connectReceiver(); 39 void connectReceiver();
40 void disconnectReceiver(); 40 void disconnectReceiver();
41 void subscribeReceiver(const QString &topic); 41 void subscribeReceiver(const QString &topic);
42 - void waitReceiverReceived(); 42 + void waitReceiverReceived(int count);
43 void onClientError(const QMQTT::ClientError error); 43 void onClientError(const QMQTT::ClientError error);
44 44
45 QList<QMQTT::Message> receivedMessages; 45 QList<QMQTT::Message> receivedMessages;
settings.h
@@ -41,7 +41,11 @@ public: @@ -41,7 +41,11 @@ public:
41 bool authPluginSerializeAuthChecks = false; 41 bool authPluginSerializeAuthChecks = false;
42 int clientInitialBufferSize = 1024; // Must be power of 2 42 int clientInitialBufferSize = 1024; // Must be power of 2
43 int maxPacketSize = 268435461; // 256 MB + 5 43 int maxPacketSize = 268435461; // 256 MB + 5
  44 +#ifdef TESTING
  45 + bool logDebug = true;
  46 +#else
44 bool logDebug = false; 47 bool logDebug = false;
  48 +#endif
45 bool logSubscriptions = false; 49 bool logSubscriptions = false;
46 std::string mosquittoPasswordFile; 50 std::string mosquittoPasswordFile;
47 std::string mosquittoAclFile; 51 std::string mosquittoAclFile;