diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 48b3dce..3b240f6 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -131,6 +131,8 @@ private slots: void testIncomingTopicAlias(); void testOutgoingTopicAlias(); + void testReceivingRetainedMessageWithQoS(); + }; MainTests::MainTests() @@ -1706,6 +1708,34 @@ void MainTests::testOutgoingTopicAlias() }); } +void MainTests::testReceivingRetainedMessageWithQoS() +{ + FlashMQTestClient sender; + sender.start(); + + const std::string payload = "We are testing"; + + sender.connectClient(ProtocolVersion::Mqtt311); + + Publish p1("topic1/FOOBAR", payload, 1); + p1.retain = true; + sender.publish(p1); + + FlashMQTestClient receiver; + receiver.start(); + receiver.connectClient(ProtocolVersion::Mqtt5); + + receiver.subscribe("+/+", 1); + + receiver.waitForMessageCount(1); + + MYCASTCOMPARE(receiver.receivedPublishes.size(), 1); + MYCASTCOMPARE(receiver.receivedPublishes.front().getQos(), 1); + MYCASTCOMPARE(receiver.receivedPublishes.front().getTopic(), "topic1/FOOBAR"); + MYCASTCOMPARE(receiver.receivedPublishes.front().getPayloadCopy(), payload); + MYCASTCOMPARE(receiver.receivedPublishes.front().getRetain(), true); +} + int main(int argc, char *argv[]) { diff --git a/publishcopyfactory.cpp b/publishcopyfactory.cpp index 81de4ae..12b3761 100644 --- a/publishcopyfactory.cpp +++ b/publishcopyfactory.cpp @@ -95,16 +95,18 @@ bool PublishCopyFactory::getRetain() const Publish PublishCopyFactory::getNewPublish() const { - assert(packet->getQos() > 0); - assert(orgQos > 0); // We only need to construct new publishes for QoS. If you're doing it elsewhere, it's a bug. - if (packet) { + assert(packet->getQos() > 0); + assert(orgQos > 0); // We only need to construct new publishes for QoS. If you're doing it elsewhere, it's a bug. + Publish p(packet->getPublishData()); p.qos = orgQos; return p; } + assert(publish->qos > 0); // Same check as above, but then for Publish objects. + Publish p(*publish); p.qos = orgQos; return p;