예제 #1
0
    def test_decode_string(self):
        async def test_coro():
            stream = BufferAdapter(b"\x00\x02AA")
            ret = await decode_string(stream)
            self.assertEqual(ret, "AA")

        anyio.run(test_coro)
예제 #2
0
    def test_start_stop(self, MockPluginManager):  # pylint: disable=unused-argument
        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())

        anyio.run(test_coro, backend="trio")
예제 #3
0
    def test_client_publish_retain_subscribe(
        self, MockPluginManager
    ):  # pylint: disable=unused-argument
        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())

        anyio.run(test_coro)
예제 #4
0
    def test_client_subscribe_publish_dollar_topic_1(
        self, MockPluginManager
    ):  # pylint: disable=unused-argument
        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([("#", 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(1):
                            message = await sub_client.deliver_message()
                    self.assertIsNone(message)
            self.assertTrue(broker.transitions.is_stopped())

        anyio.run(test_coro)
예제 #5
0
def syncify_ctx_manager(ctx):
    # assumes no error handler in the async context manager
    result = anyio.run(ctx.__aenter__)
    try:
        yield result
    finally:
        anyio.run(ctx.__aexit__, None, None, None)
예제 #6
0
파일: worker.py 프로젝트: zyrif/runnel
    def start(self, processors="all"):
        """
        The main entrypoint.

        Parameters
        ----------
        processors : Union[str, List[str]]
            If "all", then run all processors known to the app. Otherwise only run those
            named in the processors list.

        Notes
        -----
        For every processor, the first worker to start will create a consumer group for
        in Redis if it does not already exist. It will set the starting ID to "0", which
        means "process the entire stream history". If you want to select a specific
        consumer group starting ID, see :func:`runnel.Processor.reset`.

        Examples
        --------
        >>> from runnel import Worker
        >>> from mymodule import myapp
        ...
        >>> # Run all processors.
        >>> Worker(myapp).start()
        ...
        >>> # Run specific processors.
        >>> Worker(myapp).start(["myproc1", "myproc2"])

        $ # Run named processor starting at specific ID from the shell
        $ runnel processor reset mymodule:myproc --start=12345-0
        $ runnel worker mymodule:myapp --processors=myproc
        """
        anyio.run(self._start, processors, backend="asyncio")
예제 #7
0
    def test_client_connect_will_flag(self, MockPluginManager):
        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, {})

        anyio.run(test_coro, backend="trio")
예제 #8
0
        async def main():
            limiter = RateLimiter()
            self.assertIsInstance(limiter, RateLimiter)

            # Hack a fake response object.
            response = object()
            response.headers = {
                'Date': format_datetime(datetime.now(timezone.utc)),
                'X-RateLimit-Remaining': randint(1, 10),
                # 2 minutes in the future.
                'X-RateLimit-Reset': datetime.now(timezone.utc).timestamp() + (2 * 60),
            }

            bucket = ('POST', '/random')

            while True:
                if await limiter.cooldown_bucket(bucket) > 0:
                    break

                # The loop is supposed to be interrupted
                # before this is no longer true.
                self.assertGreaterEqual(limiter.buckets[bucket]._remaining, 0)

                # Update headers
                response.headers['Date'] = format_datetime(datetime.now(timezone.utc))
                response.headers['X-RateLimit-Remaining'] -= 1

                # Update the limiter
                await limiter.update_bucket(bucket, response)

            anyio.run(main)
예제 #9
0
    def test_client_publish(self, MockPluginManager):
        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)

        anyio.run(test_coro)
예제 #10
0
    def test_client_subscribe(self, MockPluginManager):
        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,
            )

        anyio.run(test_coro, backend="trio")
예제 #11
0
def test_misuse():
    assert not greenback.has_portal()  # shouldn't raise an error

    with pytest.raises(RuntimeError,
                       match="only supported.*running under Trio"):
        anyio.run(greenback.with_portal_run_tree,
                  anyio.sleep,
                  1,
                  backend="asyncio")

    with pytest.raises(sniffio.AsyncLibraryNotFoundError):
        greenback.await_(42)

    @trio.run
    async def wrong_library():
        sniffio.current_async_library_cvar.set("tokio")
        with pytest.raises(RuntimeError,
                           match="greenback does not support tokio"):
            greenback.await_(trio.sleep(1))

    @trio.run
    async def not_awaitable():
        await greenback.ensure_portal()
        with pytest.raises(TypeError,
                           match="int can't be used in 'await' expression"):
            greenback.await_(42)
예제 #12
0
    def test_deliver_retain_direct(self):
        data = b'data 123'

        async def test_coro():
            async with distkv_server(0):
                async with create_broker(
                        broker_config,
                        plugin_namespace="hbmqtt.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)

        anyio.run(test_coro, backend='trio')
예제 #13
0
    def test_client_publish_big(self, MockPluginManager):
        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,
            )

        anyio.run(test_coro, backend="trio")
예제 #14
0
    def test_deliver_direct(self):
        data = b"data 123 b"

        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)
                        async with open_mqttclient(
                                config=broker_config["broker"]) as client_pub:
                            await client_pub.publish("test_topic",
                                                     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)

        anyio.run(test_coro, backend="trio")
예제 #15
0
 def target_fn():
     import purerpc
     server = purerpc.Server(port=0)
     server.add_service(service)
     socket = server._create_socket_and_listen()
     yield socket.getsockname()[1]
     anyio.run(server._run_async_server, socket)
예제 #16
0
    def test_client_unsubscribe(self, MockPluginManager):
        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 open_mqttclient() as client:
                    ret = await client.connect('mqtt://127.0.0.1/')
                    self.assertEqual(ret, 0)
                    await client.subscribe([('/topic', QOS_0)])

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

                    await client.unsubscribe(['/topic'])
                    self.assertEqual(broker._subscriptions['/topic'], [])
            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),
                    call().fire_event(EVENT_BROKER_CLIENT_UNSUBSCRIBED,
                                        client_id=client.session.client_id,
                                        topic='/topic')
                ], any_order=True)

        anyio.run(test_coro, backend="trio")
예제 #17
0
    def test_client_subscribe_publish_dollar_topic_2(self, MockPluginManager):
        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([('+/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(2):
                            message = await sub_client.deliver_message()
                    self.assertIsNone(message)
            self.assertTrue(broker.transitions.is_stopped())

        anyio.run(test_coro)
예제 #18
0
    def test_client_subscribe_publish(self, MockPluginManager):
        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())

        anyio.run(test_coro)
예제 #19
0
    def test_client_publish_retain_subscribe(self, MockPluginManager):
        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 open_mqttclient() as sub_client:
                    await sub_client.connect('mqtt://127.0.0.1', 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: " + repr(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())

        anyio.run(test_coro)
예제 #20
0
    def test_client_connect(self, MockPluginManager):
        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('mqtt://127.0.0.1/')
                    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)

        anyio.run(test_coro)
예제 #21
0
    def test_client_publish_retain(self, MockPluginManager):
        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())

        anyio.run(test_coro)
예제 #22
0
    def test_client_publish_dup(self):
        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)

        anyio.run(test_coro, backend="trio")
예제 #23
0
def main():
    load_dotenv()
    logging.basicConfig(level=logging.DEBUG)
    args = get_args_parser().parse_args()
    try:
        run(handle_registration, args.host, args.port, args.path)
    except (TkAppClosed, KeyboardInterrupt):
        sys.stderr.write("Регистрация завершена.\n")
예제 #24
0
    def test_connect_ws(self):
        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)

        anyio.run(test_coro, backend="trio")
예제 #25
0
def test_task_group_in_generator(anyio_backend):
    @async_generator
    async def task_group_generator():
        async with create_task_group():
            await yield_()

    gen = task_group_generator()
    anyio.run(gen.__anext__, backend=anyio_backend)
    pytest.raises(StopAsyncIteration, anyio.run, gen.__anext__, backend=anyio_backend)
예제 #26
0
    def test_authorization_header(self):
        async def main():
            http = HTTP('secret')
            self.assertIsInstance(http, HTTP)

            self.assertEqual(http.headers['Authorization'],
                             'Bot {}'.format(http.token))

        anyio.run(main)
예제 #27
0
    def test_load_plugin(self):
        async def coro():
            async with anyio.create_task_group() as tg:
                manager = PluginManager(tg,
                                        "distmqtt.test.plugins",
                                        context=None)
                self.assertTrue(len(manager._plugins) > 0)

        anyio.run(coro)
예제 #28
0
def syncify_docs(docs):
    for doc in anyio.run(to_list, docs):
        for name in (doc.attachments or {}):
            async_att = doc.attachments[name]
            if not async_att.is_stub:
                data = b''.join(anyio.run(to_list, async_att))
                att = InMemoryAttachment(async_att.meta, data, False)
                doc.attachments[name] = att
        yield doc
예제 #29
0
def test_task_group_in_generator(anyio_backend_name, anyio_backend_options):
    async def task_group_generator():
        async with create_task_group():
            yield

    gen = task_group_generator()
    anyio.run(gen.__anext__, backend=anyio_backend_name, backend_options=anyio_backend_options)
    pytest.raises(StopAsyncIteration, anyio.run, gen.__anext__, backend=anyio_backend_name,
                  backend_options=anyio_backend_options)
예제 #30
0
    def test_connect_tcp(self):
        async def test_coro():
            async with open_mqttclient() as client:
                await client.connect("mqtt://test.mosquitto.org/")
                self.assertIsNotNone(client.session)

        try:
            anyio.run(test_coro)
        except ConnectException:
            log.error("Broken by server")