Commit 592e12eb6412f247da25b91172d8e9adf13f0f87

Authored by Wiebe Cazemier
1 parent ae1fbb00

Test for the '+/+' retained messages bug

Committing separately, because I want to be able to revert the fix I'm
about to commit.
Showing 1 changed file with 37 additions and 0 deletions
FlashMQTests/tst_maintests.cpp
@@ -77,6 +77,7 @@ private slots: @@ -77,6 +77,7 @@ private slots:
77 void test_retained(); 77 void test_retained();
78 void test_retained_changed(); 78 void test_retained_changed();
79 void test_retained_removed(); 79 void test_retained_removed();
  80 + void test_retained_tree();
80 81
81 void test_packet_bigger_than_one_doubling(); 82 void test_packet_bigger_than_one_doubling();
82 void test_very_big_packet(); 83 void test_very_big_packet();
@@ -443,6 +444,42 @@ void MainTests::test_retained_removed() @@ -443,6 +444,42 @@ void MainTests::test_retained_removed()
443 QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any."); 444 QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any.");
444 } 445 }
445 446
  447 +/**
  448 + * @brief MainTests::test_retained_tree tests a bug I found, where '+/+' yields different results than '#', where it should be the same.
  449 + */
  450 +void MainTests::test_retained_tree()
  451 +{
  452 + TwoClientTestContext testContext;
  453 +
  454 + QByteArray payload = "We are testing";
  455 + const QString topic1 = "TopicA/B";
  456 + const QString topic2 = "Topic/C";
  457 + const QString topic3 = "TopicB/C";
  458 + const QStringList topics {topic1, topic2, topic3};
  459 +
  460 + testContext.connectSender();
  461 + testContext.publish(topic1, payload, true);
  462 + testContext.publish(topic2, payload, true);
  463 + testContext.publish(topic3, payload, true);
  464 +
  465 + testContext.connectReceiver();
  466 + testContext.subscribeReceiver("+/+");
  467 + testContext.waitReceiverReceived(1);
  468 +
  469 + QCOMPARE(testContext.receivedMessages.count(), topics.count());
  470 +
  471 + for (const QString &s : topics)
  472 + {
  473 + bool r = std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [&](QMQTT::Message &msg)
  474 + {
  475 + return msg.topic() == s && msg.payload() == payload;
  476 + });
  477 +
  478 + QVERIFY2(r, formatString("%s not found in retained messages.", s.toStdString().c_str()).c_str());
  479 + }
  480 +
  481 +}
  482 +
446 void MainTests::test_packet_bigger_than_one_doubling() 483 void MainTests::test_packet_bigger_than_one_doubling()
447 { 484 {
448 TwoClientTestContext testContext; 485 TwoClientTestContext testContext;