async def test_check_route_registry_add_headers_per_handler(self):
        expected_route = ["/v2/events", "/other/path"]
        aditional_headers = {"X-Other-Header": "X-Other-Value"}
        app = SSEApplication(**self.connection_parameters)

        @app.route(
            expected_route,
            type=RouteTypes.SSE,
            headers=aditional_headers,
            options={
                Options.BULK_SIZE: 1024,
                Options.BULK_FLUSH_INTERVAL: 120
            },
        )
        async def _handler(message):
            return 42

        routes = app.routes_registry.sse_routes
        self.assertIsNotNone(routes)
        expected_registry_entry = {
            "type": RouteTypes.SSE,
            "routes": expected_route,
            "handler": _handler,
            "options": {
                "bulk_size": 1024,
                "bulk_flush_interval": 120,
                "headers": {
                    **self.default_headers,
                    **aditional_headers
                },
            },
        }
        self.assertEqual(expected_registry_entry, routes[0])
        self.assertEqual(42, await routes[0]["handler"](None))
Example #2
0
    async def test_check_route_registry_full_options(self):
        expected_route = ["/v2/events", "/other/path"]
        app = SSEApplication(**self.connection_parameters)

        @app.route(expected_route,
                   options={
                       Options.BULK_SIZE: 1024,
                       Options.BULK_FLUSH_INTERVAL: 120
                   })
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        expected_registry_entry = {
            "routes": expected_route,
            "handler": _handler,
            "options": {
                "bulk_size": 1024,
                "bulk_flush_interval": 120,
                "headers": self.default_headers,
            }
        }
        self.assertEqual(expected_registry_entry,
                         app.routes_registry[_handler])
        self.assertEqual(42, await app.routes_registry[_handler]['handler']
                         (None))
        self.assertEqual(self.logger, app.logger)
Example #3
0
 async def test_app_receives_queue_connection(self):
     app = SSEApplication(url="http://127.0.0.1",
                          user="******",
                          password="******",
                          headers={"A": "B"},
                          logger=None)
     self.assertEqual("http://127.0.0.1", app.url)
     self.assertEqual("guest", app.user)
     self.assertEqual("guest", app.password)
     self.assertEqual({"A": "B"}, app.headers)
Example #4
0
    async def test_register_action_on_exception(self):
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"], options={Events.ON_EXCEPTION: Actions.ACK})
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(
            Actions.ACK,
            app.routes_registry[_handler]['options'][Events.ON_EXCEPTION])
Example #5
0
    async def test_register_action_on_success(self):
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"], options={Events.ON_SUCCESS: Actions.REJECT})
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(
            Actions.REJECT,
            app.routes_registry[_handler]['options'][Events.ON_SUCCESS])
    async def test_register_hander_on_route_registry(self):
        expected_route = ["/asgard/counts/ok"]
        app = SSEApplication(**self.connection_parameters)

        @app.route(expected_route, type=RouteTypes.SSE)
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(_handler, app.routes_registry[_handler]["handler"])
        self.assertEqual(42, await app.routes_registry[_handler]["handler"]
                         (None))
Example #7
0
    async def test_register_bulk_size(self):
        expected_bulk_size = 1024
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"],
                   options={Options.BULK_SIZE: expected_bulk_size})
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(expected_bulk_size,
                         app.routes_registry[_handler]['options']['bulk_size'])
        self.assertEqual(42, await app.routes_registry[_handler]['handler']
                         (None))
Example #8
0
    async def test_test_register_default_actions(self):
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"])
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(
            Actions.ACK,
            app.routes_registry[_handler]['options'][Events.ON_SUCCESS])
        self.assertEqual(
            Actions.REQUEUE,
            app.routes_registry[_handler]['options'][Events.ON_EXCEPTION])
    async def test_register_bulk_size(self):
        expected_bulk_size = 1024
        app = SSEApplication(**self.connection_parameters)

        @app.route(
            ["my-queue"],
            type=RouteTypes.SSE,
            options={Options.BULK_SIZE: expected_bulk_size},
        )
        async def _handler(message):
            return 42

        routes = app.routes_registry.sse_routes
        self.assertEqual(len(routes), 1)
        self.assertEqual(expected_bulk_size, routes[0]["options"]["bulk_size"])
        self.assertEqual(42, await routes[0]["handler"](None))
Example #10
0
    async def test_register_default_bulk_size_and_default_bulk_flush_timeout(
            self):
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"])
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(Defaultvalues.BULK_SIZE,
                         app.routes_registry[_handler]['options']['bulk_size'])
        self.assertEqual(
            Defaultvalues.BULK_FLUSH_INTERVAL,
            app.routes_registry[_handler]['options']['bulk_flush_interval'])
        self.assertEqual(42, await app.routes_registry[_handler]['handler']
                         (None))
    async def test_register_default_bulk_size_and_default_bulk_flush_timeout(
            self):
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"], type=RouteTypes.SSE)
        async def _handler(message):
            return 42

        routes = app.routes_registry.sse_routes
        self.assertEqual(len(routes), 1)
        self.assertEqual(DefaultValues.BULK_SIZE,
                         routes[0]["options"]["bulk_size"])
        self.assertEqual(
            DefaultValues.BULK_FLUSH_INTERVAL,
            routes[0]["options"]["bulk_flush_interval"],
        )
        self.assertEqual(42, await routes[0]["handler"](None))
Example #12
0
    async def test_register_bulk_flush_timeout(self):
        expected_bulk_flush_interval = 120
        app = SSEApplication(**self.connection_parameters)

        @app.route(["my-queue"],
                   options={
                       Options.BULK_FLUSH_INTERVAL:
                       expected_bulk_flush_interval
                   })
        async def _handler(message):
            return 42

        self.assertIsNotNone(app.routes_registry)
        self.assertEqual(
            expected_bulk_flush_interval,
            app.routes_registry[_handler]['options']['bulk_flush_interval'])
        self.assertEqual(42, await app.routes_registry[_handler]['handler']
                         (None))
    async def test_register_bulk_flush_timeout(self):
        expected_bulk_flush_interval = 120
        app = SSEApplication(**self.connection_parameters)

        @app.route(
            ["my-queue"],
            type=RouteTypes.SSE,
            options={
                Options.BULK_FLUSH_INTERVAL: expected_bulk_flush_interval
            },
        )
        async def _handler(message):
            return 42

        routes = app.routes_registry.sse_routes
        self.assertEqual(len(routes), 1)
        self.assertEqual(
            expected_bulk_flush_interval,
            routes[0]["options"]["bulk_flush_interval"],
        )
        self.assertEqual(42, await routes[0]["handler"](None))
Example #14
0
    async def test_instantiate_one_consumer_per_handler_one_handler_registered(
            self):
        """
        Para cada handler registrado, teremos um Consumer. Esse Consumer conseguirá consumir múltiplas
        filas, se necessário.
        """
        app = SSEApplication(**self.connection_parameters)

        @app.route(["/asgard/counts"])
        async def _handler(message):
            return message

        consumers = app._build_consumers()
        self.assertEqual(1, len(consumers))

        final_url = urljoin(self.connection_parameters['url'],
                            '/asgard/counts')
        self.assertEqual([final_url], consumers[0].routes)
        self.assertEqual(final_url, consumers[0].url)
        self.assertEqual(self.connection_parameters['user'],
                         consumers[0].username)
        self.assertEqual(self.connection_parameters['password'],
                         consumers[0].password)
    async def test_instantiate_one_consumer_per_handler_one_handler_registered(
            self):
        """
        Para cada handler registrado, teremos um Consumer. Esse Consumer conseguirá consumir múltiplas
        filas, se necessário.
        """
        app = SSEApplication(**self.connection_parameters)

        @app.route(["/asgard/counts"], type=RouteTypes.SSE)
        async def _handler(message):
            return message

        await app.startup()
        consumers = app[RouteTypes.SSE]["consumers"]
        self.assertEqual(1, len(consumers))
        consumer = consumers[0]

        final_url = urljoin(self.connection_parameters["url"],
                            "/asgard/counts")
        self.assertEqual([final_url], consumer.routes)
        self.assertEqual(final_url, consumer.url)
        self.assertEqual(self.connection_parameters["user"], consumer.username)
        self.assertEqual(self.connection_parameters["password"],
                         consumer.password)