Example #1
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.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", PORT) 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)
Example #2
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.test.plugins"
            ) as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as sub_client:
                    await sub_client.connect(URL, cleansession=False)
                    ret = await sub_client.subscribe(
                        [("/qos0", QOS_0), ("/qos1", QOS_1), ("/qos2", QOS_2)]
                    )
                    self.assertEqual(ret, [QOS_0, QOS_1, QOS_2])
                    await sub_client.disconnect()

                    await self._client_publish("/qos0", b"data", QOS_0, retain=True)
                    await self._client_publish("/qos1", b"data", QOS_1, retain=True)
                    await self._client_publish("/qos2", b"data", QOS_2, retain=True)
                    await sub_client.reconnect()
                    for qos in [QOS_0, QOS_1, QOS_2]:
                        log.debug("TEST QOS: %d", qos)
                        message = await sub_client.deliver_message()
                        log.debug("Message: %r", message.publish_packet)
                        self.assertIsNotNone(message)
                        self.assertEqual(message.topic, "/qos%s" % qos)
                        self.assertEqual(message.data, b"data")
                        self.assertEqual(message.qos, qos)
            self.assertTrue(broker.transitions.is_stopped())
Example #3
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.test.plugins"
            ) as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as client:
                    ret = await client.connect(URL)
                    self.assertEqual(ret, 0)
                    await client.subscribe([("/topic", QOS_0)])

                    # Test if the client test client subscription is registered
                    subs = broker._subscriptions[("", "topic")]
                    self.assertEqual(len(subs), 1)
                    (s, qos) = subs[0]
                    self.assertEqual(s, client.session)
                    self.assertEqual(qos, QOS_0)

            self.assertTrue(broker.transitions.is_stopped())
            MockPluginManager.assert_has_calls(
                [
                    call().fire_event(
                        EVENT_BROKER_CLIENT_SUBSCRIBED,
                        client_id=client.session.client_id,
                        topic="/topic",
                        qos=QOS_0,
                    )
                ],
                any_order=True,
            )
Example #4
0
 async def test_coro():
     async with create_broker(
         test_config, plugin_namespace="distmqtt.test.plugins"
     ) as broker:
         broker.plugins_manager._tg = broker._tg
         self.assertTrue(broker.transitions.is_started())
         async with open_mqttclient() as client:
             ret = await client.connect(URL)
             self.assertEqual(ret, 0)
             self.assertEqual(len(broker._sessions), 1)
             self.assertIn(client.session.client_id, broker._sessions)
         await anyio.sleep(0.1)  # let the broker task process the packet
     self.assertTrue(broker.transitions.is_stopped())
     self.assertDictEqual(broker._sessions, {})
     MockPluginManager.assert_has_calls(
         [
             call().fire_event(
                 EVENT_BROKER_CLIENT_CONNECTED, client_id=client.session.client_id,
             ),
             call().fire_event(
                 EVENT_BROKER_CLIENT_DISCONNECTED, client_id=client.session.client_id,
             ),
         ],
         any_order=True,
     )
Example #5
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.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", PORT) 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, {})
Example #6
0
 async def test_coro():
     async with distkv_server(1):
         async with create_broker(
                 broker_config,
                 plugin_namespace="distmqtt.test.plugins"):
             async with open_mqttclient(
                     config=broker_config["broker"]) as client:
                 self.assertIsNotNone(client.session)
                 ret = await client.subscribe([("test/vis/foo", QOS_0)])
                 self.assertEqual(ret[0], QOS_0)
                 async with open_mqttclient(
                         config=broker_config["broker"]) as client_pub:
                     await client_pub.publish("test/vis/foo",
                                              data,
                                              QOS_0,
                                              retain=False)
                 async with anyio.fail_after(0.5):
                     message = await client.deliver_message()
                 self.assertIsNotNone(message)
                 self.assertIsNotNone(message.publish_packet)
                 self.assertEqual(message.data, data)
                 pass  # exit client
             pass  # exit broker
         pass  # exit server
     pass  # exit test
Example #7
0
async def distkv_server(n):
    msgs = []
    async with anyio.create_task_group() as tg:
        async with create_broker(test_config,
                                 plugin_namespace="distmqtt.test.plugins"):
            s = Server("test", cfg=broker_config["distkv"], init="test")
            evt = anyio.create_event()
            await tg.spawn(partial(s.serve, ready_evt=evt))
            await evt.wait()

            async with open_client(**broker_config["distkv"]) as cl:

                async def msglog(task_status=trio.TASK_STATUS_IGNORED):
                    async with cl._stream(
                            "msg_monitor", topic="*"
                    ) as mon:  # , topic=broker_config['distkv']['topic']) as mon:
                        log.info("Monitor Start")
                        task_status.started()
                        async for m in mon:
                            log.info("Monitor Msg %r", m.data)
                            msgs.append(m.data)

                await cl.scope.spawn(msglog)
                yield s
                await cl.scope.cancel()
            await tg.cancel_scope.cancel()
    if len(msgs) != n:
        log.error("MsgCount %d %d", len(msgs), n)
Example #8
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.test.plugins"
            ) as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as pub_client:
                    ret = await pub_client.connect(URL)
                    self.assertEqual(ret, 0)

                    ret_message = await pub_client.publish(
                        "/topic", bytearray(b"\x99" * 256 * 1024), QOS_2
                    )
                self.assertEqual(broker._retained_messages, {})

            self.assertTrue(broker.transitions.is_stopped())
            MockPluginManager.assert_has_calls(
                [
                    call().fire_event(
                        EVENT_BROKER_MESSAGE_RECEIVED,
                        client_id=pub_client.session.client_id,
                        message=ret_message,
                    )
                ],
                any_order=True,
            )
Example #9
0
 async def test_coro():
     async with create_broker(broker_config,
                              plugin_namespace="distmqtt.test.plugins"):
         async with open_mqttclient() as client:
             await client.connect(URI)
             self.assertIsNotNone(client.session)
             await client.ping()
Example #10
0
 async def test_coro():
     async with create_broker(broker_config,
                              plugin_namespace="distmqtt.test.plugins"):
         async with open_mqttclient() as client:
             ca = os.path.join(
                 os.path.dirname(os.path.realpath(__file__)),
                 "mosquitto.org.crt")
             await client.connect("ws://127.0.0.1:8081/", cafile=ca)
             self.assertIsNotNone(client.session)
Example #11
0
        async def test_coro():
            async with create_broker(broker_config,
                                     plugin_namespace="distmqtt.test.plugins"):
                async with open_mqttclient() as client:
                    await client.connect("ws://*****:*****@127.0.0.1:8080/")
                    self.assertIsNotNone(client.session)
                    await client.reconnect()

                    self.assertIsNotNone(client.session.username)
                    self.assertIsNotNone(client.session.password)
Example #12
0
 async def test_coro():
     async with create_broker(broker_config,
                              plugin_namespace="distmqtt.test.plugins"):
         async with open_mqttclient() as client:
             await client.connect(URI)
             self.assertIsNotNone(client.session)
             ret = await client.subscribe([("$SYS/broker/uptime", QOS_0)
                                           ])
             self.assertEqual(ret[0], QOS_0)
             await client.unsubscribe(["$SYS/broker/uptime"])
Example #13
0
 async def test_coro():
     async with create_broker(broker_config,
                              plugin_namespace="distmqtt.test.plugins"):
         async with open_mqttclient() as client:
             await client.connect(URI)
             self.assertIsNotNone(client.session)
             ret = await client.subscribe([("test_topic", QOS_0)])
             self.assertEqual(ret[0], QOS_0)
             with self.assertRaises(TimeoutError):
                 async with anyio.fail_after(2):
                     await client.deliver_message()
             await client.unsubscribe(["$SYS/broker/uptime"])
Example #14
0
 async def test_coro():
     async with create_broker(
             broker_config,
             plugin_namespace="distmqtt.test.plugins") as broker:
         async with open_mqttclient() as client:
             await client.connect('mqtt://127.0.0.1/')
             self.assertIsNotNone(client.session)
             ret = await client.subscribe([
                 ('$SYS/broker/uptime', QOS_0),
             ])
             self.assertEqual(ret[0], QOS_0)
             await client.unsubscribe(['$SYS/broker/uptime'])
Example #15
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as pub_client:
                    ret = await pub_client.connect('mqtt://127.0.0.1/')
                    self.assertEqual(ret, 0)

                    await pub_client.publish('/+', b'data', QOS_0)

            self.assertTrue(broker.transitions.is_stopped())
Example #16
0
 async def test_coro():
     async with distkv_server(0):
         async with create_broker(
                 broker_config,
                 plugin_namespace="distmqtt.test.plugins"):
             async with open_mqttclient(
                     config=broker_config["broker"]) as client:
                 self.assertIsNotNone(client.session)
                 ret = await client.subscribe([("test_topic", QOS_0)])
                 self.assertEqual(ret[0], QOS_0)
                 with self.assertRaises(TimeoutError):
                     async with anyio.fail_after(2):
                         await client.deliver_message()
Example #17
0
async def main(config, debug):
    formatter = "[%(asctime)s] :: %(levelname)s - %(message)s"

    level = logging.DEBUG if debug else logging.INFO
    logging.basicConfig(level=level, format=formatter)

    if not config:
        config = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default_broker.yaml')
        logger.debug("Using default configuration")
    config = read_yaml_config(config)

    async with create_broker(config) as broker:
        while True:
            await anyio.sleep(99999)
Example #18
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.test.plugins"
            ) as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())

                async with open_mqttclient() as pub_client:
                    ret = await pub_client.connect(URL)
                    self.assertEqual(ret, 0)
                    await pub_client.publish("/topic", b"", QOS_0, retain=True)
                await anyio.sleep(0.1)  # let the broker task process the packet
                self.assertNotIn("/topic", broker._retained_messages)
            self.assertTrue(broker.transitions.is_stopped())
Example #19
0
 async def test_coro():
     async with distkv_server(0):
         async with create_broker(
                 broker_config,
                 plugin_namespace="distmqtt.test.plugins") as broker:
             async with open_mqttclient() as client:
                 await client.connect('mqtt://127.0.0.1/')
                 self.assertIsNotNone(client.session)
                 ret = await client.subscribe([
                     ('test_topic', QOS_0),
                 ])
                 self.assertEqual(ret[0], QOS_0)
                 with self.assertRaises(TimeoutError):
                     async with anyio.fail_after(2):
                         await client.deliver_message()
Example #20
0
async def test_server(mqtt_port: int = None, distkv_port: int = None):
    """
    An async context manager which creates a stand-alone DistKV server.

    The server has a `test_client` method: an async context manager that
    returns a client taht's connected to this server.

    Ports are allocated based on the current process's PID.
    """
    if mqtt_port is None:
        mqtt_port = 40000 + os.getpid() % 10000
    if distkv_port is None:
        distkv_port = 40000 + (os.getpid() + 1) % 10000

    broker_cfg = {
        "listeners": {
            "default": {
                "type": "tcp",
                "bind": "127.0.0.1:%d" % mqtt_port
            }
        },
        "timeout-disconnect-delay": 2,
        "auth": {
            "allow-anonymous": True,
            "password-file": None
        },
    }
    server_cfg = {
        "server": {
            "bind_default": {
                "host": "127.0.0.1",
                "port": distkv_port
            },
            "backend": "mqtt",
            "mqtt": {
                "uri": "mqtt://127.0.0.1:%d/" % mqtt_port
            },
        }
    }

    server = Server(name="gpio_test", cfg=server_cfg, init="GPIO")
    async with create_broker(config=broker_cfg) as broker:
        evt = anyio.create_event()
        await broker._tg.spawn(partial(server.serve, ready_evt=evt))
        await evt.wait()

        server.distkv_port = distkv_port  # pylint: disable=attribute-defined-outside-init
        yield server
Example #21
0
 async def test_coro():
     async with create_broker(
         test_config, plugin_namespace="distmqtt.test.plugins"
     ) as broker:
         broker.plugins_manager._tg = broker._tg
         self.assertTrue(broker.transitions.is_started())
         async with open_mqttclient(
             client_id="", config={"auto_reconnect": False}
         ) as client:
             return_code = None
             try:
                 await client.connect(URL, cleansession=False)
             except ConnectException as ce:
                 return_code = ce.return_code
             self.assertEqual(return_code, 0x02)
             self.assertNotIn(client.session.client_id, broker._sessions)
Example #22
0
 async def test_coro():
     async with create_broker(broker_config,
                              plugin_namespace="distmqtt.test.plugins"):
         async with open_mqttclient() as client:
             await client.connect(URI)
             self.assertIsNotNone(client.session)
             ret = await client.subscribe([("test_topic", QOS_0)])
             self.assertEqual(ret[0], QOS_0)
             async with open_mqttclient() as client_pub:
                 await client_pub.connect(URI)
                 await client_pub.publish("test_topic", data, QOS_0)
             message = await client.deliver_message()
             self.assertIsNotNone(message)
             self.assertIsNotNone(message.publish_packet)
             self.assertEqual(message.data, data)
             await client.unsubscribe(["$SYS/broker/uptime"])
Example #23
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as sub_client:
                    await sub_client.connect('mqtt://127.0.0.1')
                    ret = await sub_client.subscribe([('+', QOS_0),
                                                      ('+/tennis/#', QOS_0),
                                                      ('sport+', QOS_0),
                                                      ('sport/+/player1',
                                                       QOS_0)])
                    self.assertEqual(ret, [QOS_0, QOS_0, 0x80, QOS_0])

            self.assertTrue(broker.transitions.is_stopped())
Example #24
0
 async def test_coro():
     async with create_broker(
             test_config,
             plugin_namespace="distmqtt.test.plugins") as broker:
         broker.plugins_manager._tg = broker._tg
         self.assertTrue(broker.transitions.is_started())
         self.assertDictEqual(broker._sessions, {})
         self.assertIn('default', broker._servers)
         MockPluginManager.assert_has_calls([
             call().fire_event(EVENT_BROKER_PRE_START),
             call().fire_event(EVENT_BROKER_POST_START)
         ],
                                            any_order=True)
         MockPluginManager.reset_mock()
     MockPluginManager.assert_has_calls([
         call().fire_event(EVENT_BROKER_PRE_SHUTDOWN),
         call().fire_event(EVENT_BROKER_POST_SHUTDOWN)
     ],
                                        any_order=True)
     self.assertTrue(broker.transitions.is_stopped())
Example #25
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as sub_client:
                    await sub_client.connect('mqtt://127.0.0.1')
                    ret = await sub_client.subscribe([('#', QOS_0)])
                    self.assertEqual(ret, [QOS_0])

                    await self._client_publish('/topic', b'data', QOS_0)
                    message = await sub_client.deliver_message()
                    self.assertIsNotNone(message)

                    await self._client_publish('$topic', b'data', QOS_0)
                    message = None
                    with self.assertRaises(TimeoutError):
                        async with anyio.fail_after(2):
                            message = await sub_client.deliver_message()
                    self.assertIsNone(message)
            self.assertTrue(broker.transitions.is_stopped())
Example #26
0
        async def test_coro():
            async with create_broker(
                test_config, plugin_namespace="distmqtt.test.plugins"
            ) as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as sub_client:
                    await sub_client.connect(URL)
                    ret = await sub_client.subscribe([("+/monitor/Clients", QOS_0)])
                    self.assertEqual(ret, [QOS_0])

                    await self._client_publish("test/monitor/Clients", b"data", QOS_0)
                    message = await sub_client.deliver_message()
                    self.assertIsNotNone(message)

                    await self._client_publish("$SYS/monitor/Clients", b"data", QOS_0)
                    message = None
                    with self.assertRaises(TimeoutError):
                        async with anyio.fail_after(1):
                            message = await sub_client.deliver_message()
                    self.assertIsNone(message)
            self.assertTrue(broker.transitions.is_stopped())
Example #27
0
 async def test_coro():
     async with distkv_server(0):
         async with create_broker(
                 broker_config,
                 plugin_namespace="distmqtt.test.plugins") as broker:
             async with open_mqttclient() as client:
                 await client.connect('mqtt://127.0.0.1/')
                 self.assertIsNotNone(client.session)
                 ret = await client.subscribe([
                     ('test_topic', QOS_0),
                 ])
                 self.assertEqual(ret[0], QOS_0)
                 async with open_mqttclient() as client_pub:
                     await client_pub.connect('mqtt://127.0.0.1/')
                     await client_pub.publish('test_topic',
                                              data,
                                              QOS_0,
                                              retain=True)
                 async with anyio.fail_after(0.5):
                     message = await client.deliver_message()
                 self.assertIsNotNone(message)
                 self.assertIsNotNone(message.publish_packet)
                 self.assertEqual(message.data, data)
Example #28
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as sub_client:
                    await sub_client.connect('mqtt://127.0.0.1')
                    ret = await sub_client.subscribe([('/qos0', QOS_0),
                                                      ('/qos1', QOS_1),
                                                      ('/qos2', QOS_2)])
                    self.assertEqual(ret, [QOS_0, QOS_1, QOS_2])

                    await self._client_publish('/qos0', b'data', QOS_0)
                    await self._client_publish('/qos1', b'data', QOS_1)
                    await self._client_publish('/qos2', b'data', QOS_2)
                    for qos in [QOS_0, QOS_1, QOS_2]:
                        message = await sub_client.deliver_message()
                        self.assertIsNotNone(message)
                        self.assertEqual(message.topic, '/qos%s' % qos)
                        self.assertEqual(message.data, b'data')
                        self.assertEqual(message.qos, qos)
            self.assertTrue(broker.transitions.is_stopped())
Example #29
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())
                async with open_mqttclient() as pub_client:
                    ret = await pub_client.connect('mqtt://127.0.0.1/')
                    self.assertEqual(ret, 0)

                    ret_message = await pub_client.publish(
                        '/topic', b'data', QOS_0)
                await anyio.sleep(0.1
                                  )  # let the broker task process the packet
                self.assertEqual(broker._retained_messages, {})

            self.assertTrue(broker.transitions.is_stopped())
            MockPluginManager.assert_has_calls([
                call().fire_event(EVENT_BROKER_MESSAGE_RECEIVED,
                                  client_id=pub_client.session.client_id,
                                  message=ret_message),
            ],
                                               any_order=True)
Example #30
0
        async def test_coro():
            async with create_broker(
                    test_config,
                    plugin_namespace="distmqtt.test.plugins") as broker:
                broker.plugins_manager._tg = broker._tg
                self.assertTrue(broker.transitions.is_started())

                async with open_mqttclient() as pub_client:
                    ret = await pub_client.connect('mqtt://127.0.0.1/')
                    self.assertEqual(ret, 0)
                    await pub_client.publish('/topic',
                                             b'data',
                                             QOS_0,
                                             retain=True)
                await anyio.sleep(0.1
                                  )  # let the broker task process the packet
                self.assertIn('/topic', broker._retained_messages)
                retained_message = broker._retained_messages['/topic']
                self.assertEqual(retained_message.source_session,
                                 pub_client.session)
                self.assertEqual(retained_message.topic, '/topic')
                self.assertEqual(retained_message.data, b'data')
                self.assertEqual(retained_message.qos, QOS_0)
            self.assertTrue(broker.transitions.is_stopped())