async def startup(self, app): # type: ignore app[RouteTypes.SSE] = {"consumers": []} for route_info in app.routes_registry.sse_routes: for route in route_info["routes"]: consumer = SSEConsumer( route_info=route_info, url=urljoin(app.url, route), username=app.user, password=app.password, ) app[RouteTypes.SSE]["consumers"].append(consumer) app.loop.create_task(consumer.start())
async def startup(self, app): # type: ignore app[RouteTypes.SSE]["consumers"] = [] for route_info in app.routes_registry.sse_routes: conn: SSEConnection = app.get_connection_for_route(route_info) for route in route_info["routes"]: consumer = SSEConsumer( route_info=route_info, url=urljoin(conn.url, route), username=conn.user, password=conn.password, ) app[RouteTypes.SSE]["consumers"].append(consumer) app.loop.create_task(consumer.start())
async def test_consumer_instantiate_using_bucket_class(self): class MyBucket(Bucket): pass consumer = SSEConsumer(self.one_route_fixture, *(self.consumer_params + (MyBucket, ))) self.assertTrue(isinstance(consumer.bucket, MyBucket))
async def setUp(self): self.one_route_fixture = { "routes": ["/asgard/counts/ok"], "handler": _handler, "options": { "vhost": "/", "bulk_size": 1, "bulk_flush_interval": 60, }, } self.consumer_params = ( "http://localhost:8080/v2/events", "guest", "guest", ) self.consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params) self.consumer.interval = 0 self.logger_patcher = asynctest.mock.patch.object( conf, "logger", CoroutineMock( info=CoroutineMock(), debug=CoroutineMock(), error=CoroutineMock(), ), )
async def test_on_event_calls_inner_handler(self): consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params) result = await consumer.on_event( b"deployment_info", b'{"key": "value"}' ) self.assertEqual((42, {"key": "value"}), result)
async def test_on_queue_message_bulk_size_bigger_that_one(self): """ Confere que o handler real só é chamado quando o bucket atinge o limite máximo de tamanho. E que o handler é chamado com a lista de mensagens. * Bucket deve estart vazio após o "flush" """ class MyBucket(Bucket): def pop_all(self): return self._items handler_mock = CoroutineMock() self.one_route_fixture["handler"] = handler_mock self.one_route_fixture["options"]["bulk_size"] = 2 consumer = SSEConsumer( self.one_route_fixture, *(self.consumer_params + (MyBucket,)) ) result = await consumer.on_event( b"deployment_info", b'{"key": "value"}' ) self.assertEqual(0, handler_mock.await_count) result = await consumer.on_event( b"deployment_info", b'{"key": "value"}' ) handler_mock.assert_awaited_once_with(consumer.bucket._items)
def _build_consumers(self): pass consumers = [] for _handler, route_info in self.routes_registry.items(): for route in route_info['routes']: final_url = urljoin(self.url, route) consumers.append(SSEConsumer(route_info, final_url, self.user, self.password)) return consumers
async def test_new_consumer_instance(self): consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params) self.assertEqual(consumer.url, self.consumer_params[0]) self.assertEqual(consumer.route_info, self.one_route_fixture) self.assertEqual(consumer._handler, self.one_route_fixture["handler"]) self.assertEqual(self.consumer_params[1], consumer.username) self.assertEqual(self.consumer_params[2], consumer.password) final_route = urljoin(self.consumer_params[0], self.one_route_fixture["routes"][0]) self.assertEqual([final_route], consumer.routes)
async def setUp(self): self.one_route_fixture = { "routes": ["/asgard/counts/ok"], "handler": _handler, "options": { "vhost": "/", "bulk_size": 1, "bulk_flush_interval": 60, } } self.consumer_params = ("http://localhost:8080/v2/events", "guest", "guest") self.consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params)
def test_new_consumer_instance_multiple_routes(self): self.one_route_fixture["routes"].append("/v2/events") consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params) self.assertEqual(consumer.url, self.consumer_params[0]) self.assertEqual(consumer.route_info, self.one_route_fixture) self.assertEqual(consumer._handler, self.one_route_fixture["handler"]) expected_routes = [ urljoin(self.consumer_params[0], self.one_route_fixture["routes"][0]), urljoin(self.consumer_params[0], self.one_route_fixture["routes"][1]), ] self.assertEqual(expected_routes, consumer.routes)
async def test_consumer_instantiate_correct_size_bucket(self): self.one_route_fixture["options"]["bulk_size"] = 42 consumer = SSEConsumer(self.one_route_fixture, *self.consumer_params) self.assertEqual(self.one_route_fixture["options"]["bulk_size"], consumer.bucket.size)