Example #1
0
 def test_to_stream_packet(self):
     variable_header = PublishVariableHeader('topic', 10)
     payload = PublishPayload(b'0123456789')
     publish = PublishPacket(variable_header=variable_header,
                             payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out, b'\x30\x13\x00\x05topic\00\x0a0123456789')
Example #2
0
 def test_to_stream_no_packet_id(self):
     variable_header = PublishVariableHeader("topic", None)
     payload = PublishPayload(b"0123456789")
     publish = PublishPacket(variable_header=variable_header,
                             payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out, b"\x30\x11\x00\x05topic0123456789")
Example #3
0
 async def server_mock(stream):
     packet = PublishPacket.build(
         "/topic", b"test_data", rand_packet_id(), False, QOS_1, False
     )
     await packet.to_stream(stream)
     puback = await PubackPacket.from_stream(stream)
     self.assertIsNotNone(puback)
     self.assertEqual(packet.packet_id, puback.packet_id)
Example #4
0
    def build_publish_packet(self, dup=False):
        """
            Build :class:`distmqtt.mqtt.publish.PublishPacket` from attributes

        :param dup: force dup flag
        :return: :class:`distmqtt.mqtt.publish.PublishPacket` built from ApplicationMessage instance attributes
        """
        return PublishPacket.build(self.topic, self.data, self.packet_id, dup,
                                   self.qos, self.retain)
Example #5
0
 async def test_coro(stream_adapted):
     self.session = Session(None)
     message = OutgoingApplicationMessage(1, "/topic", QOS_2, b"test_data", False)
     message.publish_packet = PublishPacket.build(
         "/topic", b"test_data", rand_packet_id(), False, QOS_2, False
     )
     self.session.inflight_out[1] = message
     self.handler = ProtocolHandler(self.plugin_manager)
     await self.handler.attach(self.session, stream_adapted)
     await self.handler.start()
     await self.stop_handler(self.handler, self.session)
Example #6
0
 async def server_mock(stream):
     packet = PublishPacket.build(
         "/topic", b"test_data", rand_packet_id(), False, QOS_2, False
     )
     await packet.to_stream(stream)
     pubrec = await PubrecPacket.from_stream(stream)
     self.assertIsNotNone(pubrec)
     self.assertEqual(packet.packet_id, pubrec.packet_id)
     self.assertIn(packet.packet_id, self.handler._pubrel_waiters)
     pubrel = PubrelPacket.build(packet.packet_id)
     await pubrel.to_stream(stream)
     pubcomp = await PubcompPacket.from_stream(stream)
     self.assertIsNotNone(pubcomp)
     self.assertEqual(packet.packet_id, pubcomp.packet_id)
Example #7
0
    def test_build(self):
        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_0, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_0)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_1, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_1)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_2, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_2)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_0, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_0)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_1, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_1)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_2, False)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_2)
        self.assertFalse(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_0, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_0)
        self.assertTrue(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_1, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_1)
        self.assertTrue(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, False, QOS_2, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertFalse(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_2)
        self.assertTrue(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_0, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_0)
        self.assertTrue(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_1, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_1)
        self.assertTrue(packet.retain_flag)

        packet = PublishPacket.build('/topic', b'data', 1, True, QOS_2, True)
        self.assertEqual(packet.packet_id, 1)
        self.assertTrue(packet.dup_flag)
        self.assertEqual(packet.qos, QOS_2)
        self.assertTrue(packet.retain_flag)
Example #8
0
 async def server_mock(stream):
     packet = PublishPacket.build(
         "/topic", b"test_data", rand_packet_id(), False, QOS_0, False
     )
     await packet.to_stream(stream)