Commit af89bf902e28859d3cd7515c49c399ff11d422ac
1 parent
c0b67cfb
Test unsubscribe
Only mqtt3 though.
Showing
3 changed files
with
79 additions
and
6 deletions
FlashMQTests/tst_maintests.cpp
| ... | ... | @@ -115,6 +115,8 @@ private slots: |
| 115 | 115 | |
| 116 | 116 | void testNotMessingUpQosLevels(); |
| 117 | 117 | |
| 118 | + void testUnSubscribe(); | |
| 119 | + | |
| 118 | 120 | }; |
| 119 | 121 | |
| 120 | 122 | MainTests::MainTests() |
| ... | ... | @@ -1291,6 +1293,58 @@ void MainTests::testNotMessingUpQosLevels() |
| 1291 | 1293 | QCOMPARE(testContextReceiver5.receivedMessages.first().id(), 0); |
| 1292 | 1294 | } |
| 1293 | 1295 | |
| 1296 | +void MainTests::testUnSubscribe() | |
| 1297 | +{ | |
| 1298 | + TwoClientTestContext testContext; | |
| 1299 | + | |
| 1300 | + testContext.connectSender(); | |
| 1301 | + testContext.connectReceiver(); | |
| 1302 | + | |
| 1303 | + testContext.subscribeReceiver("Rebecca/Bunch", 2); | |
| 1304 | + testContext.subscribeReceiver("Josh/Chan", 1); | |
| 1305 | + testContext.subscribeReceiver("White/Josh", 1); | |
| 1306 | + | |
| 1307 | + testContext.publish("Rebecca/Bunch", "Bunch here", 2); | |
| 1308 | + testContext.publish("White/Josh", "Anteater", 2); | |
| 1309 | + testContext.publish("Josh/Chan", "Human flip-flop", 2); | |
| 1310 | + | |
| 1311 | + testContext.waitReceiverReceived(3); | |
| 1312 | + | |
| 1313 | + QVERIFY(std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [](const QMQTT::Message &msg) { | |
| 1314 | + return msg.payload() == "Bunch here" && msg.topic() == "Rebecca/Bunch"; | |
| 1315 | + })); | |
| 1316 | + | |
| 1317 | + QVERIFY(std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [](const QMQTT::Message &msg) { | |
| 1318 | + return msg.payload() == "Anteater" && msg.topic() == "White/Josh"; | |
| 1319 | + })); | |
| 1320 | + | |
| 1321 | + QVERIFY(std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [](const QMQTT::Message &msg) { | |
| 1322 | + return msg.payload() == "Human flip-flop" && msg.topic() == "Josh/Chan"; | |
| 1323 | + })); | |
| 1324 | + | |
| 1325 | + QCOMPARE(testContext.receivedMessages.count(), 3); | |
| 1326 | + | |
| 1327 | + testContext.receivedMessages.clear(); | |
| 1328 | + | |
| 1329 | + testContext.unsubscribeReceiver("Josh/Chan"); | |
| 1330 | + | |
| 1331 | + testContext.publish("Rebecca/Bunch", "Bunch here", 2); | |
| 1332 | + testContext.publish("White/Josh", "Anteater", 2); | |
| 1333 | + testContext.publish("Josh/Chan", "Human flip-flop", 2); | |
| 1334 | + | |
| 1335 | + testContext.waitReceiverReceived(2); | |
| 1336 | + | |
| 1337 | + QCOMPARE(testContext.receivedMessages.count(), 2); | |
| 1338 | + | |
| 1339 | + QVERIFY(std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [](const QMQTT::Message &msg) { | |
| 1340 | + return msg.payload() == "Bunch here" && msg.topic() == "Rebecca/Bunch"; | |
| 1341 | + })); | |
| 1342 | + | |
| 1343 | + QVERIFY(std::any_of(testContext.receivedMessages.begin(), testContext.receivedMessages.end(), [](const QMQTT::Message &msg) { | |
| 1344 | + return msg.payload() == "Anteater" && msg.topic() == "White/Josh"; | |
| 1345 | + })); | |
| 1346 | +} | |
| 1347 | + | |
| 1294 | 1348 | |
| 1295 | 1349 | int main(int argc, char *argv[]) |
| 1296 | 1350 | { | ... | ... |
FlashMQTests/twoclienttestcontext.cpp
| ... | ... | @@ -95,21 +95,39 @@ void TwoClientTestContext::subscribeReceiver(const QString &topic, const quint8 |
| 95 | 95 | waiter.exec(); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | -void TwoClientTestContext::waitReceiverReceived(int count) | |
| 98 | +void TwoClientTestContext::unsubscribeReceiver(const QString &topic) | |
| 99 | 99 | { |
| 100 | - if (count > 0 && receivedMessages.count() == count) | |
| 101 | - return; | |
| 100 | + receiver->unsubscribe(topic); | |
| 102 | 101 | |
| 103 | 102 | QEventLoop waiter; |
| 104 | 103 | QTimer timeout; |
| 105 | 104 | timeout.setSingleShot(true); |
| 106 | - timeout.setInterval(3000); | |
| 105 | + timeout.setInterval(1000); | |
| 107 | 106 | connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); |
| 108 | - connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); | |
| 107 | + connect(receiver.data(), &QMQTT::Client::unsubscribed, &waiter, &QEventLoop::quit); | |
| 109 | 108 | timeout.start(); |
| 110 | 109 | waiter.exec(); |
| 111 | 110 | } |
| 112 | 111 | |
| 112 | +void TwoClientTestContext::waitReceiverReceived(const int count) | |
| 113 | +{ | |
| 114 | + if (count > 0 && receivedMessages.count() == count) | |
| 115 | + return; | |
| 116 | + | |
| 117 | + int attempt = 0; | |
| 118 | + while(receivedMessages.count() != count && attempt++ < count) | |
| 119 | + { | |
| 120 | + QEventLoop waiter; | |
| 121 | + QTimer timeout; | |
| 122 | + timeout.setSingleShot(true); | |
| 123 | + timeout.setInterval(3000); | |
| 124 | + connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit); | |
| 125 | + connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit); | |
| 126 | + timeout.start(); | |
| 127 | + waiter.exec(); | |
| 128 | + } | |
| 129 | +} | |
| 130 | + | |
| 113 | 131 | void TwoClientTestContext::onClientError(const QMQTT::ClientError error) |
| 114 | 132 | { |
| 115 | 133 | const QMQTT::Client *_sender = sender.data(); | ... | ... |
FlashMQTests/twoclienttestcontext.h
| ... | ... | @@ -41,7 +41,8 @@ public: |
| 41 | 41 | void connectReceiver(); |
| 42 | 42 | void disconnectReceiver(); |
| 43 | 43 | void subscribeReceiver(const QString &topic, const quint8 qos = 0); |
| 44 | - void waitReceiverReceived(int count); | |
| 44 | + void unsubscribeReceiver(const QString &topic); | |
| 45 | + void waitReceiverReceived(const int count); | |
| 45 | 46 | void onClientError(const QMQTT::ClientError error); |
| 46 | 47 | |
| 47 | 48 | QList<QMQTT::Message> receivedMessages; | ... | ... |