async def test_consume_one_message_app_with_multiple_connections(self): conn = AMQPConnection(hostname="127.0.0.1", username="******", password="******", prefetch=1) conn_2 = AMQPConnection( hostname="127.0.0.1:5673", username="******", password="******", prefetch=1, ) await conn["/"].connection._connect() await conn["/"].connection.channel.queue_declare("queue") await conn["/"].put(data={"num": 42}, routing_key="queue") app = App(connections=[conn, conn_2]) @app.amqp.consume(["queue"], connection=conn) async def handler(msgs): global message_processed_multiple_connections message_processed_multiple_connections = True await app.startup() await asyncio.sleep(1) await app.shutdown() self.assertTrue(message_processed_multiple_connections)
async def setUp(self): self.username = "******" self.password = "******" self.hostname = "127.0.0.1" self.rabbitmq_connection = AMQPConnection( hostname=self.hostname, username=self.username, password=self.password, ) self.body = asynctest.Mock() self.routing_key = asynctest.Mock() self.exchange = asynctest.Mock()
async def test_it_uses_the_connection_provided_by_the_route_if_one_exists( self): conn = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) app = App(connections=[]) @app.route( routes=["a_queue_name"], type=RouteTypes.AMQP_RABBITMQ, options={"connection": conn}, ) async def mock_handler(*args, **kwargs): pass MockedConsumer = Mock(return_value=Mock(spec=Consumer, queue=Mock())) with patch("asyncworker.signals.handlers.rabbitmq.Consumer", MockedConsumer): await self.signal_handler.startup(app) MockedConsumer.assert_called_once_with( route_info=ANY, host=conn.hostname, username=conn.username, password=conn.password, prefetch_count=conn.prefetch, )
def setUp(self): self.connection = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, )
async def setUp(self): self.queue_name = "test" self.connection = AMQPConnection(hostname="127.0.0.1", username="******", password="******", prefetch=1) self.app = App(connections=[self.connection])
def setUp(self): self.queue_mock = Mock( connection=Mock(has_channel_ready=Mock(return_value=True)), spec=JsonQueue, ) self.logger_mock = CoroutineMock(info=CoroutineMock(), debug=CoroutineMock(), error=CoroutineMock()) self.connection_parameters = ("127.0.0.1", "guest", "guest", 1024) self.one_route_fixture = { "routes": ["/asgard/counts/ok"], "handler": _handler, "vhost": "/", "options": { "bulk_size": 1, "bulk_flush_interval": 1, Events.ON_SUCCESS: Actions.ACK, Events.ON_EXCEPTION: Actions.REQUEUE, }, } self.connection = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) self.app = App(connections=[self.connection]) self.mock_message = self._make_msg() mock.patch.object(conf, "settings", mock.Mock(FLUSH_TIMEOUT=0.1)).start()
async def test_register_handler_with_second_connection(self): """ Em uma app com múltiplas conexões, a conexão que é passada para o decorator é a que vai para o objeto AMQPRoute. """ conn = AMQPConnection(hostname="127.0.0.1", username="******", password="******") conn_2 = AMQPConnection(hostname="127.0.0.1", username="******", password="******") app = App(connections=[conn, conn_2]) @app.amqp.consume(["/"], connection=conn_2) async def handler(): pass self.assertEqual(1, len(app.routes_registry.amqp_routes)) self.assertEqual(handler, app.routes_registry.amqp_routes[0].handler) self.assertEqual(conn_2, app.routes_registry.amqp_routes[0].connection)
def setUp(self): super(AppConnectionsTests, self).setUp() self.connections = [ AMQPConnection( name="conn1", hostname="localhost", username="******", password="******", ), AMQPConnection( name="conn2", hostname="localhost", username="******", password="******", ), AMQPConnection(hostname="localhost", username="******", password="******"), ]
async def test_it_raises_an_error_if_theres_multiple_connections_and_route_doesnt_define_a_connection( self): conn1 = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) conn2 = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) app = App(connections=[conn1, conn2]) @app.route(routes=["a_queue_name"], type=RouteTypes.AMQP_RABBITMQ) async def mock_handler(*args, **kwargs): pass with self.assertRaises(InvalidRoute): await self.signal_handler.startup(app)
async def test_initialize_with_connections(self): connection_a = asynctest.Mock( virtual_host=settings.AMQP_DEFAULT_VHOST, spec=JsonQueue ) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) conn = AMQPConnection( hostname="localhost", username="******", password="******", connections={"a": connection_a, "b": connection_b}, ) self.assertEqual( {"a": connection_a, "b": connection_b}, conn.connections )
def __init__( # nosec self, hostname: str = "127.0.0.1", username: str = "guest", password: str = "guest", prefetch: int = 10, connection_name: str = "default", ): self.__connection = AMQPConnection( name=connection_name, hostname=hostname, username=username, password=password, prefetch=prefetch ) self.__app = App(connections=[self.__connection])
async def test_startup_registers_one_connection_per_vhost_into_app_state( self, register): conn = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) app = App(connections=[conn]) app.routes_registry = self.routes_registry await self.signal_handler.startup(app) self.assertIn(conn, app.connections) register.assert_has_calls([ call(consumer.queue) for consumer in app[RouteTypes.AMQP_RABBITMQ]["consumers"] ])
async def test_startup_initializes_and_starts_one_consumer_per_route( self, Consumer): connection = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024, ) app = App(connections=[connection]) app.routes_registry = self.routes_registry app.loop = Mock(create_task=CoroutineMock()) # asynctest.MagicMock( # routes_registry=, # loop=Mock(create_task=CoroutineMock()), # ) app[RouteTypes.AMQP_RABBITMQ] = {"connections": [connection]} await self.signal_handler.startup(app) Consumer.assert_has_calls( [ call( route_info=self.routes_registry.amqp_routes[0], host=connection.hostname, username=connection.username, password=connection.password, prefetch_count=connection.prefetch, ), call( route_info=self.routes_registry.amqp_routes[1], host=connection.hostname, username=connection.username, password=connection.password, prefetch_count=connection.prefetch, ), ], any_order=True, ) Consumer.return_value.start.assert_has_calls([call(), call()]) self.assertEqual( app[RouteTypes.AMQP_RABBITMQ]["consumers"], [Consumer.return_value, Consumer.return_value], )
async def test_consume_from_other_vhost(self): conn = AMQPConnection(hostname="127.0.0.1", username="******", password="******", prefetch=1) await conn["logs"].connection._connect() await conn["logs"].connection.channel.queue_declare("queue") await conn["logs"].put(data={"num": 42}, routing_key="queue") app = App(connections=[conn]) @app.amqp.consume(["queue"], vhost="logs") async def handler(msgs): global message_processed_other_vhost message_processed_other_vhost = True await app.startup() await asyncio.sleep(1) await app.shutdown() self.assertTrue(message_processed_other_vhost)
async def setUp(self): self.input_queue = "test" self.output_exchange = "test_exchange" self.output_queue = "test_output" self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1") self.barterdude_host = os.environ.get("BARTERDUDE_HOST", "127.0.0.1") self.connection = AMQPConnection( # nosec name="barterdude-test", hostname=self.rabbitmq_host, username="******", password="******", prefetch=1 ) self.queue_manager = self.connection["/"] await self.queue_manager.connection._connect() await self.queue_manager.connection.channel.queue_declare( self.input_queue ) await self.queue_manager.connection.channel.exchange_declare( self.output_exchange, "direct" ) await self.queue_manager.connection.channel.queue_declare( self.output_queue ) await self.queue_manager.connection.channel.queue_bind( self.output_queue, self.output_exchange, "" ) self.messages = [] for i in range(10): message = {"key": "".join(choices(ascii_uppercase, k=16))} self.messages.append(message) self.schema = load_fixture("schema.json") self.app = BarterDude(hostname=self.rabbitmq_host)
async def test_values(self): self.maxDiff = None conn = AMQPConnection(hostname="localhost", username="******", password="******") self.assertEqual(str(ValuesView(conn)), str(conn.values()))
async def test_items(self): conn = AMQPConnection(hostname="localhost", username="******", password="******") self.assertEqual(ItemsView(conn), conn.items())
async def setUp(self): self.connection = AMQPConnection(hostname="localhost", username="******", password="******") self.mapping = ConnectionsMapping()
class AMQPConnectionTests(asynctest.TestCase): async def setUp(self): self.username = "******" self.password = "******" self.hostname = "127.0.0.1" self.rabbitmq_connection = AMQPConnection( hostname=self.hostname, username=self.username, password=self.password, ) self.body = asynctest.Mock() self.routing_key = asynctest.Mock() self.exchange = asynctest.Mock() async def tearDown(self): asynctest.patch.stopall() def test_len_returns_the_number_of_registered_connections(self): self.assertEqual(len(self.rabbitmq_connection), 0) self.rabbitmq_connection.register(asynctest.Mock()) self.assertEqual(len(self.rabbitmq_connection), 1) def test_rabbitmq_connection_is_iterable(self): connection_a = asynctest.Mock(virtual_host="a", spec=JsonQueue) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) self.rabbitmq_connection.register(connection_a) self.rabbitmq_connection.register(connection_b) as_dict = dict(self.rabbitmq_connection) self.assertEqual(as_dict, {"a": connection_a, "b": connection_b}) def test_register_registers_a_new_unique_connection_for_a_given_vhost( self): connection_a = asynctest.Mock(virtual_host="a", spec=JsonQueue) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) self.rabbitmq_connection.register(connection_a) self.rabbitmq_connection.register(connection_b) self.assertEqual(self.rabbitmq_connection["a"], connection_a) self.assertEqual(self.rabbitmq_connection["b"], connection_b) async def test_put_uses_the_right_connection_for_a_given_vhost(self): connection_a = asynctest.Mock(virtual_host="a", spec=JsonQueue) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) self.rabbitmq_connection.register(connection_a) self.rabbitmq_connection.register(connection_b) await self.rabbitmq_connection.put( serialized_data=self.body, routing_key=self.routing_key, exchange=self.exchange, vhost="a", ) connection_b.put.assert_not_awaited() connection_a.put.assert_awaited_once_with( data=None, routing_key=self.routing_key, exchange=self.exchange, serialized_data=self.body, ) async def test_put_uses_the_default_vhost_if_none_is_provided(self): connection_a = asynctest.Mock(virtual_host=settings.AMQP_DEFAULT_VHOST, spec=JsonQueue) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) self.rabbitmq_connection.register(connection_a) self.rabbitmq_connection.register(connection_b) await self.rabbitmq_connection.put(data=self.body, routing_key=self.routing_key, exchange=self.exchange) connection_b.put.assert_not_awaited() connection_a.put.assert_awaited_once_with( data=self.body, routing_key=self.routing_key, exchange=self.exchange, serialized_data=None, ) async def test_put_initializes_a_new_connection_if_a_connection_wasnt_initialized_for_a_given_vhost( self): connection_a = asynctest.Mock(virtual_host="a", spec=JsonQueue) with asynctest.patch("asyncworker.connections.JsonQueue", return_value=connection_a): await self.rabbitmq_connection.put( data=self.body, routing_key=self.routing_key, exchange=self.exchange, vhost="a", ) connection_a.put.assert_awaited_once_with( data=self.body, routing_key=self.routing_key, exchange=self.exchange, serialized_data=None, ) async def test_initialize_with_connections(self): connection_a = asynctest.Mock(virtual_host=settings.AMQP_DEFAULT_VHOST, spec=JsonQueue) connection_b = asynctest.Mock(virtual_host="b", spec=JsonQueue) conn = AMQPConnection( hostname="localhost", username="******", password="******", connections={ "a": connection_a, "b": connection_b }, ) self.assertEqual({ "a": connection_a, "b": connection_b }, conn.connections) async def test_items(self): conn = AMQPConnection(hostname="localhost", username="******", password="******") self.assertEqual(ItemsView(conn), conn.items()) async def test_values(self): self.maxDiff = None conn = AMQPConnection(hostname="localhost", username="******", password="******") self.assertEqual(str(ValuesView(conn)), str(conn.values()))
import aiohttp_cors from aiohttp import web from asyncworker import App, RouteTypes from asyncworker.conf import settings from asyncworker.connections import AMQPConnection from asgard import conf conn = AMQPConnection( hostname=conf.ASGARD_RABBITMQ_HOST, username=conf.ASGARD_RABBITMQ_USER, password=conf.ASGARD_RABBITMQ_PASS, prefetch=conf.ASGARD_RABBITMQ_PREFETCH, ) app = App(connections=[conn]) async def patched_startup(app): app[RouteTypes.HTTP] = {} routes = app.routes_registry.http_routes app[RouteTypes.HTTP]["app"] = http_app = web.Application() for route in routes: for route_def in route.aiohttp_routes(): route_def.register(http_app.router) cors = aiohttp_cors.setup( http_app, defaults={
from datetime import datetime from typing import List from asyncworker import App from asyncworker.connections import AMQPConnection from asyncworker.rabbitmq import RabbitMQMessage, AMQPRouteOptions amqp_conn = AMQPConnection( hostname="127.0.0.1", username="******", password="******", prefetch=1024 ) app = App(connections=[amqp_conn]) @app.amqp.consume( ["queue"], options=AMQPRouteOptions(bulk_size=1024 * 8, bulk_flush_interval=10), ) async def _handler(msgs: List[RabbitMQMessage]): print(f"Recv {len(msgs)} {datetime.now().isoformat()}") @app.run_every(1) async def produce(app: App): await amqp_conn.put(data={"msg": "ok"}, routing_key="queue") app.run()
from typing import List from asyncworker import App from asyncworker.connections import AMQPConnection from asyncworker.http.methods import HTTPMethods from asyncworker.options import Actions, Events, Options from asyncworker.rabbitmq import RabbitMQMessage, AMQPRouteOptions """ Para rodar esse exemplo precisamos de dois RabbitMQ rodando, ambos na porta default. Basta ajustar os endereços abaixo apontando cada conexão para o RabbitMQ correspondente. Em ambos os Rabbits deve existe uma fila com nome "queue" no vhost "/". """ amqp_conn = AMQPConnection(hostname="127.0.0.1", username="******", password="******") amqp_conn_2 = AMQPConnection(hostname="172.17.0.2", username="******", password="******") app = App(connections=[amqp_conn, amqp_conn_2]) @app.amqp.consume(["queue"], connection=amqp_conn, options=AMQPRouteOptions(bulk_size=64)) async def _handler_broker_1(msgs: List[RabbitMQMessage]): print(f"Broker 1 ({amqp_conn.hostname}): Recv: {len(msgs)}") for m in msgs: