async def test_client_publish_dup(broker, event_loop): conn_reader, conn_writer = await asyncio.open_connection("127.0.0.1", 1883, loop=event_loop) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False payload.client_id = "test_id" connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(writer) await ConnackPacket.from_stream(reader) publish_1 = PublishPacket.build("/test", b"data", 1, False, QOS_2, False) await publish_1.to_stream(writer) asyncio.ensure_future(PubrecPacket.from_stream(reader), loop=event_loop) await asyncio.sleep(2) publish_dup = PublishPacket.build("/test", b"data", 1, True, QOS_2, False) await publish_dup.to_stream(writer) await PubrecPacket.from_stream(reader) pubrel = PubrelPacket.build(1) await pubrel.to_stream(writer) await PubcompPacket.from_stream(reader) disconnect = DisconnectPacket() await disconnect.to_stream(writer)
async def test_coro(): async with create_broker(test_config, plugin_namespace="hbmqtt.test.plugins") as broker: broker.plugins_manager._tg = broker._tg self.assertTrue(broker.transitions.is_started()) async with await anyio.connect_tcp("127.0.0.1", 1883) as conn: stream = StreamAdapter(conn) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False payload.client_id = 'test_id' connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(stream) await ConnackPacket.from_stream(stream) publish_1 = PublishPacket.build('/test', b'data', 1, False, QOS_2, False) await publish_1.to_stream(stream) await PubrecPacket.from_stream(stream) publish_dup = PublishPacket.build('/test', b'data', 1, True, QOS_2, False) await publish_dup.to_stream(stream) await PubrecPacket.from_stream(stream) pubrel = PubrelPacket.build(1) await pubrel.to_stream(stream) #await PubcompPacket.from_stream(stream) disconnect = DisconnectPacket() await disconnect.to_stream(stream)
async def test_coro(): try: broker = Broker( self._test_config, plugin_namespace="hbmqtt.test.plugins" ) await broker.start() self.assertTrue(broker.transitions.is_started()) conn_reader, conn_writer = await asyncio.open_connection( "127.0.0.1", self._test_port, loop=self.loop ) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False payload.client_id = "test_id" connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(writer) await ConnackPacket.from_stream(reader) publish_1 = PublishPacket.build( "/test", b"data", 1, False, QOS_2, False ) await publish_1.to_stream(writer) asyncio.ensure_future(PubrecPacket.from_stream(reader), loop=self.loop) await asyncio.sleep(2) publish_dup = PublishPacket.build( "/test", b"data", 1, True, QOS_2, False ) await publish_dup.to_stream(writer) await PubrecPacket.from_stream(reader) pubrel = PubrelPacket.build(1) await pubrel.to_stream(writer) await PubcompPacket.from_stream(reader) disconnect = DisconnectPacket() await disconnect.to_stream(writer) await asyncio.sleep(0.1) await broker.shutdown() future.set_result(True) except Exception as ae: future.set_exception(ae)
def test_encode(self): header = MQTTFixedHeader(CONNECT, 0x00, 0) variable_header = ConnectVariableHeader(0xce, 0, 'MQTT', 4) payload = ConnectPayload('0123456789', 'WillTopic', b'WillMessage', 'user', 'password') message = ConnectPacket(header, variable_header, payload) encoded = message.to_bytes() self.assertEqual(encoded, b'\x10\x3e\x00\x04MQTT\x04\xce\x00\x00\x00\x0a0123456789\x00\x09WillTopic\x00\x0bWillMessage\x00\x04user\x00\x08password')
def test_coro(): try: broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins") yield from broker.start() self.assertTrue(broker.transitions.is_started()) conn_reader, conn_writer = \ yield from asyncio.open_connection('127.0.0.1', 1883, loop=self.loop) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False payload.client_id = 'test_id' connect = ConnectPacket(vh=vh, payload=payload) yield from connect.to_stream(writer) yield from ConnackPacket.from_stream(reader) publish_1 = PublishPacket.build('/test', b'data', 1, False, QOS_2, False) yield from publish_1.to_stream(writer) asyncio.ensure_future(PubrecPacket.from_stream(reader), loop=self.loop) yield from asyncio.sleep(2) publish_dup = PublishPacket.build('/test', b'data', 1, True, QOS_2, False) yield from publish_dup.to_stream(writer) yield from PubrecPacket.from_stream(reader) pubrel = PubrelPacket.build(1) yield from pubrel.to_stream(writer) yield from PubcompPacket.from_stream(reader) disconnect = DisconnectPacket() yield from disconnect.to_stream(writer) yield from asyncio.sleep(0.1) yield from broker.shutdown() future.set_result(True) except Exception as ae: future.set_exception(ae)
async def test_coro(): async with create_broker(test_config, plugin_namespace="hbmqtt.test.plugins") as broker: broker.plugins_manager._tg = broker._tg self.assertTrue(broker.transitions.is_started()) async with await anyio.connect_tcp("127.0.0.1", 1883) as conn: stream = StreamAdapter(conn) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False vh.will_flag = True vh.will_qos = QOS_0 payload.client_id = 'test_id' payload.will_message = b'test' payload.will_topic = '/topic' connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(stream) await ConnackPacket.from_stream(stream) disconnect = DisconnectPacket() await disconnect.to_stream(stream) self.assertTrue(broker.transitions.is_stopped()) self.assertDictEqual(broker._sessions, {})
async def test_client_connect_will_flag(broker, event_loop): conn_reader, conn_writer = await asyncio.open_connection("127.0.0.1", 1883, loop=event_loop) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False vh.will_flag = True vh.will_qos = QOS_0 payload.client_id = "test_id" payload.will_message = b"test" payload.will_topic = "/topic" connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(writer) await ConnackPacket.from_stream(reader) await asyncio.sleep(0.1) disconnect = DisconnectPacket() await disconnect.to_stream(writer) await asyncio.sleep(0.1)
def test_encode(self): header = MQTTFixedHeader(CONNECT, 0x00, 0) variable_header = ConnectVariableHeader(0xCE, 0, "MQTT", 4) payload = ConnectPayload( "0123456789", "WillTopic", b"WillMessage", "user", "password" ) message = ConnectPacket(header, variable_header, payload) encoded = message.to_bytes() self.assertEqual( encoded, b"\x10\x3e\x00\x04MQTT\x04\xce\x00\x00\x00\x0a0123456789\x00\x09WillTopic\x00\x0bWillMessage\x00\x04user\x00\x08password", )
async def test_coro(): try: broker = Broker( self._test_config, plugin_namespace="hbmqtt.test.plugins" ) await broker.start() self.assertTrue(broker.transitions.is_started()) conn_reader, conn_writer = await asyncio.open_connection( "127.0.0.1", self._test_port, loop=self.loop ) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False vh.will_flag = True vh.will_qos = QOS_0 payload.client_id = "test_id" payload.will_message = b"test" payload.will_topic = "/topic" connect = ConnectPacket(vh=vh, payload=payload) await connect.to_stream(writer) await ConnackPacket.from_stream(reader) await asyncio.sleep(0.1) disconnect = DisconnectPacket() await disconnect.to_stream(writer) await asyncio.sleep(0.1) await broker.shutdown() self.assertTrue(broker.transitions.is_stopped()) self.assertDictEqual(broker._sessions, {}) future.set_result(True) except Exception as ae: future.set_exception(ae)
def test_coro(): try: broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins") yield from broker.start() self.assertTrue(broker.transitions.is_started()) conn_reader, conn_writer = \ yield from asyncio.open_connection('localhost', 1883, loop=self.loop) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False vh.will_flag = True vh.will_qos = QOS_0 payload.client_id = 'test_id' payload.will_message = b'test' payload.will_topic = '/topic' connect = ConnectPacket(vh=vh, payload=payload) yield from connect.to_stream(writer) yield from ConnackPacket.from_stream(reader) yield from asyncio.sleep(0.1) disconnect = DisconnectPacket() yield from disconnect.to_stream(writer) yield from asyncio.sleep(0.1) yield from broker.shutdown() self.assertTrue(broker.transitions.is_stopped()) self.assertDictEqual(broker._sessions, {}) future.set_result(True) except Exception as ae: future.set_exception(ae)
def test_coro(): try: broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins") yield from broker.start() self.assertTrue(broker.transitions.is_started()) conn_reader, conn_writer = \ yield from asyncio.open_connection('127.0.0.1', 1883, loop=self.loop) reader = StreamReaderAdapter(conn_reader) writer = StreamWriterAdapter(conn_writer) vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = 10 vh.clean_session_flag = False vh.will_retain_flag = False vh.will_flag = True vh.will_qos = QOS_0 payload.client_id = 'test_id' payload.will_message = b'test' payload.will_topic = '/topic' connect = ConnectPacket(vh=vh, payload=payload) yield from connect.to_stream(writer) yield from ConnackPacket.from_stream(reader) yield from asyncio.sleep(0.1) disconnect = DisconnectPacket() yield from disconnect.to_stream(writer) yield from asyncio.sleep(0.1) yield from broker.shutdown() self.assertTrue(broker.transitions.is_stopped()) self.assertDictEqual(broker._sessions, {}) future.set_result(True) except Exception as ae: future.set_exception(ae)
def build_connect_packet(session): vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = session.keep_alive vh.clean_session_flag = session.clean_session vh.will_retain_flag = session.will_retain payload.client_id = session.client_id if session.username: vh.username_flag = True payload.username = session.username else: vh.username_flag = False if session.password: vh.password_flag = True payload.password = session.password else: vh.password_flag = False if session.will_flag: vh.will_flag = True vh.will_qos = session.will_qos payload.will_message = session.will_message payload.will_topic = session.will_topic else: vh.will_flag = False header = MQTTFixedHeader(PacketType.CONNECT, 0x00) packet = ConnectPacket(header, vh, payload) return packet
def _build_connect_packet(self): vh = ConnectVariableHeader() payload = ConnectPayload() vh.keep_alive = self.session.keep_alive vh.clean_session_flag = self.session.clean_session vh.will_retain_flag = self.session.will_retain payload.client_id = self.session.client_id if self.session.username: vh.username_flag = True payload.username = self.session.username else: vh.username_flag = False if self.session.password: vh.password_flag = True payload.password = self.session.password else: vh.password_flag = False if self.session.will_flag: vh.will_flag = True vh.will_qos = self.session.will_qos payload.will_message = self.session.will_message payload.will_topic = self.session.will_topic else: vh.will_flag = False packet = ConnectPacket(vh=vh, payload=payload) return packet