Commit 0765167b77c0ab11011e06e6e27dfd540a5da3dd
1 parent
8cabbca0
Make test_retained test cross-protocol and fix retain flag bug
Cross-protocol, received messages on existing subscriptions would get retain=1, which is wrong.
Showing
2 changed files
with
39 additions
and
30 deletions
FlashMQTests/tst_maintests.cpp
| @@ -408,48 +408,56 @@ void MainTests::test_validSubscribePath() | @@ -408,48 +408,56 @@ void MainTests::test_validSubscribePath() | ||
| 408 | 408 | ||
| 409 | void MainTests::test_retained() | 409 | void MainTests::test_retained() |
| 410 | { | 410 | { |
| 411 | - FlashMQTestClient sender; | ||
| 412 | - FlashMQTestClient receiver; | 411 | + std::vector<ProtocolVersion> protocols {ProtocolVersion::Mqtt311, ProtocolVersion::Mqtt5}; |
| 413 | 412 | ||
| 414 | - sender.start(); | ||
| 415 | - receiver.start(); | 413 | + for (const ProtocolVersion senderVersion : protocols) |
| 414 | + { | ||
| 415 | + for (const ProtocolVersion receiverVersion : protocols) | ||
| 416 | + { | ||
| 417 | + FlashMQTestClient sender; | ||
| 418 | + FlashMQTestClient receiver; | ||
| 416 | 419 | ||
| 417 | - const std::string payload = "We are testing"; | ||
| 418 | - const std::string topic = "retaintopic"; | 420 | + sender.start(); |
| 421 | + receiver.start(); | ||
| 419 | 422 | ||
| 420 | - sender.connectClient(ProtocolVersion::Mqtt311); | 423 | + const std::string payload = "We are testing"; |
| 424 | + const std::string topic = "retaintopic"; | ||
| 421 | 425 | ||
| 422 | - Publish pub1(topic, payload, 0); | ||
| 423 | - pub1.retain = true; | ||
| 424 | - sender.publish(pub1); | 426 | + sender.connectClient(senderVersion); |
| 425 | 427 | ||
| 426 | - Publish pub2("dummy2", "Nobody sees this", 0); | ||
| 427 | - pub2.retain = true; | ||
| 428 | - sender.publish(pub2); | 428 | + Publish pub1(topic, payload, 0); |
| 429 | + pub1.retain = true; | ||
| 430 | + sender.publish(pub1); | ||
| 429 | 431 | ||
| 430 | - receiver.connectClient(ProtocolVersion::Mqtt311); | ||
| 431 | - receiver.subscribe("dummy", 0); | ||
| 432 | - receiver.subscribe(topic, 0); | 432 | + Publish pub2("dummy2", "Nobody sees this", 0); |
| 433 | + pub2.retain = true; | ||
| 434 | + sender.publish(pub2); | ||
| 433 | 435 | ||
| 434 | - receiver.waitForMessageCount(1); | 436 | + receiver.connectClient(receiverVersion); |
| 437 | + receiver.subscribe("dummy", 0); | ||
| 438 | + receiver.subscribe(topic, 0); | ||
| 435 | 439 | ||
| 436 | - MYCASTCOMPARE(receiver.receivedPublishes.size(), 1); | 440 | + receiver.waitForMessageCount(1); |
| 437 | 441 | ||
| 438 | - MqttPacket &msg = receiver.receivedPublishes.front(); | ||
| 439 | - QCOMPARE(msg.getPayloadCopy(), payload); | ||
| 440 | - QCOMPARE(msg.getTopic(), topic); | ||
| 441 | - QVERIFY(msg.getRetain()); | 442 | + MYCASTCOMPARE(receiver.receivedPublishes.size(), 1); |
| 442 | 443 | ||
| 443 | - receiver.clearReceivedLists(); | 444 | + MqttPacket &msg = receiver.receivedPublishes.front(); |
| 445 | + QCOMPARE(msg.getPayloadCopy(), payload); | ||
| 446 | + QCOMPARE(msg.getTopic(), topic); | ||
| 447 | + QVERIFY(msg.getRetain()); | ||
| 444 | 448 | ||
| 445 | - sender.publish(pub1); | ||
| 446 | - receiver.waitForMessageCount(1); | 449 | + receiver.clearReceivedLists(); |
| 447 | 450 | ||
| 448 | - QVERIFY2(receiver.receivedPublishes.size() == 1, "There must be one message in the received list"); | ||
| 449 | - MqttPacket &msg2 = receiver.receivedPublishes.front(); | ||
| 450 | - QCOMPARE(msg2.getPayloadCopy(), payload); | ||
| 451 | - QCOMPARE(msg2.getTopic(), topic); | ||
| 452 | - QVERIFY2(!msg2.getRetain(), "Getting a retained message while already being subscribed must be marked as normal, not retain."); | 451 | + sender.publish(pub1); |
| 452 | + receiver.waitForMessageCount(1); | ||
| 453 | + | ||
| 454 | + QVERIFY2(receiver.receivedPublishes.size() == 1, "There must be one message in the received list"); | ||
| 455 | + MqttPacket &msg2 = receiver.receivedPublishes.front(); | ||
| 456 | + QCOMPARE(msg2.getPayloadCopy(), payload); | ||
| 457 | + QCOMPARE(msg2.getTopic(), topic); | ||
| 458 | + QVERIFY2(!msg2.getRetain(), "Getting a retained message while already being subscribed must be marked as normal, not retain."); | ||
| 459 | + } | ||
| 460 | + } | ||
| 453 | } | 461 | } |
| 454 | 462 | ||
| 455 | void MainTests::test_retained_changed() | 463 | void MainTests::test_retained_changed() |
mqttpacket.cpp
| @@ -1281,6 +1281,7 @@ void MqttPacket::handlePublish() | @@ -1281,6 +1281,7 @@ void MqttPacket::handlePublish() | ||
| 1281 | // Existing subscribers don't get retain=1. [MQTT-3.3.1-9] | 1281 | // Existing subscribers don't get retain=1. [MQTT-3.3.1-9] |
| 1282 | bites[0] &= 0b11110110; | 1282 | bites[0] &= 0b11110110; |
| 1283 | first_byte = bites[0]; | 1283 | first_byte = bites[0]; |
| 1284 | + publishData.retain = false; | ||
| 1284 | 1285 | ||
| 1285 | PublishCopyFactory factory(this); | 1286 | PublishCopyFactory factory(this); |
| 1286 | MainApp::getMainApp()->getSubscriptionStore()->queuePacketAtSubscribers(factory); | 1287 | MainApp::getMainApp()->getSubscriptionStore()->queuePacketAtSubscribers(factory); |