Commit 958aeb620c223ccc3769b029d2d3571314164fcf
1 parent
5a4b57de
Fix QoS reduction on retained messages
It was caught by an assert. In release mode, this simply meant subscribing to 0 still gave you QoS messages.
Showing
2 changed files
with
33 additions
and
18 deletions
FlashMQTests/tst_maintests.cpp
| ... | ... | @@ -1710,32 +1710,45 @@ void MainTests::testOutgoingTopicAlias() |
| 1710 | 1710 | |
| 1711 | 1711 | void MainTests::testReceivingRetainedMessageWithQoS() |
| 1712 | 1712 | { |
| 1713 | - FlashMQTestClient sender; | |
| 1714 | - sender.start(); | |
| 1713 | + int testCount = 0; | |
| 1715 | 1714 | |
| 1716 | - const std::string payload = "We are testing"; | |
| 1715 | + for (char sendQos = 0; sendQos < 3; sendQos++) | |
| 1716 | + { | |
| 1717 | + for (char subscribeQos = 0; subscribeQos < 3; subscribeQos++) | |
| 1718 | + { | |
| 1719 | + testCount++; | |
| 1717 | 1720 | |
| 1718 | - sender.connectClient(ProtocolVersion::Mqtt311); | |
| 1721 | + FlashMQTestClient sender; | |
| 1722 | + sender.start(); | |
| 1719 | 1723 | |
| 1720 | - Publish p1("topic1/FOOBAR", payload, 1); | |
| 1721 | - p1.retain = true; | |
| 1722 | - sender.publish(p1); | |
| 1724 | + const std::string payload = "We are testing"; | |
| 1723 | 1725 | |
| 1724 | - FlashMQTestClient receiver; | |
| 1725 | - receiver.start(); | |
| 1726 | - receiver.connectClient(ProtocolVersion::Mqtt5); | |
| 1726 | + sender.connectClient(ProtocolVersion::Mqtt311); | |
| 1727 | 1727 | |
| 1728 | - receiver.subscribe("+/+", 1); | |
| 1728 | + Publish p1("topic1/FOOBAR", payload, sendQos); | |
| 1729 | + p1.retain = true; | |
| 1730 | + sender.publish(p1); | |
| 1729 | 1731 | |
| 1730 | - receiver.waitForMessageCount(1); | |
| 1732 | + FlashMQTestClient receiver; | |
| 1733 | + receiver.start(); | |
| 1734 | + receiver.connectClient(ProtocolVersion::Mqtt5); | |
| 1731 | 1735 | |
| 1732 | - MYCASTCOMPARE(receiver.receivedPublishes.size(), 1); | |
| 1733 | - MYCASTCOMPARE(receiver.receivedPublishes.front().getQos(), 1); | |
| 1734 | - MYCASTCOMPARE(receiver.receivedPublishes.front().getTopic(), "topic1/FOOBAR"); | |
| 1735 | - MYCASTCOMPARE(receiver.receivedPublishes.front().getPayloadCopy(), payload); | |
| 1736 | - MYCASTCOMPARE(receiver.receivedPublishes.front().getRetain(), true); | |
| 1737 | -} | |
| 1736 | + receiver.subscribe("+/+", subscribeQos); | |
| 1737 | + | |
| 1738 | + receiver.waitForMessageCount(1); | |
| 1739 | + | |
| 1740 | + const char expQos = std::min<char>(sendQos, subscribeQos); | |
| 1738 | 1741 | |
| 1742 | + MYCASTCOMPARE(receiver.receivedPublishes.size(), 1); | |
| 1743 | + MYCASTCOMPARE(receiver.receivedPublishes.front().getQos(), expQos); | |
| 1744 | + MYCASTCOMPARE(receiver.receivedPublishes.front().getTopic(), "topic1/FOOBAR"); | |
| 1745 | + MYCASTCOMPARE(receiver.receivedPublishes.front().getPayloadCopy(), payload); | |
| 1746 | + MYCASTCOMPARE(receiver.receivedPublishes.front().getRetain(), true); | |
| 1747 | + } | |
| 1748 | + } | |
| 1749 | + | |
| 1750 | + MYCASTCOMPARE(9, testCount); | |
| 1751 | +} | |
| 1739 | 1752 | |
| 1740 | 1753 | int main(int argc, char *argv[]) |
| 1741 | 1754 | { | ... | ... |
publishcopyfactory.cpp
| ... | ... | @@ -53,6 +53,8 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto |
| 53 | 53 | // Getting an instance of a Publish object happens at least on retained messages, will messages and SYS topics. It's low traffic, anyway. |
| 54 | 54 | assert(publish); |
| 55 | 55 | |
| 56 | + publish->qos = getEffectiveQos(max_qos); | |
| 57 | + | |
| 56 | 58 | this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish); |
| 57 | 59 | return this->oneShotPacket.get(); |
| 58 | 60 | } | ... | ... |