async def test_multiple_event_transports(loop, redis_server_url, redis_server_b_url, create_redis_client): """Configure a bus with two redis transports and ensure they write to the correct redis servers""" redis_url_a = redis_server_url redis_url_b = redis_server_b_url logging.warning(f"Server A URL: {redis_url_a}") logging.warning(f"Server B URL: {redis_url_b}") config = Config.load_dict({ "bus": { "schema": { "transport": { "redis": { "url": redis_url_a } } } }, "apis": { "default": { "event_transport": { "redis": { "url": redis_url_a, "stream_use": StreamUse.PER_EVENT.value } } }, "api_b": { "event_transport": { "redis": { "url": redis_url_b, "stream_use": StreamUse.PER_EVENT.value } } }, }, }) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config)) bus.client.register_api(ApiA()) bus.client.register_api(ApiB()) await asyncio.sleep(0.1) await bus.api_a.event_a.fire_async() await bus.api_b.event_b.fire_async() redis_a = await create_redis_client(address=redis_server_url) redis_b = await create_redis_client(address=redis_server_b_url) assert await redis_a.xrange("api_a.event_a:stream") assert await redis_a.xrange("api_b.event_b:stream") == [] assert await redis_b.xrange("api_a.event_a:stream") == [] assert await redis_b.xrange("api_b.event_b:stream") await bus.client.close_async()
async def test_multiple_event_transports(loop, server, redis_server_b): """Configure a bus with two redis transports and ensure they write to the correct redis servers""" registry.add(ApiA()) registry.add(ApiB()) manually_set_plugins(plugins={}) redis_server_a = server port_a = redis_server_a.tcp_address.port port_b = redis_server_b.tcp_address.port logging.warning(f"Server A port: {port_a}") logging.warning(f"Server B port: {port_b}") config = Config.load_dict( { "bus": {"schema": {"transport": {"redis": {"url": f"redis://localhost:{port_a}"}}}}, "apis": { "default": { "event_transport": { "redis": { "url": f"redis://localhost:{port_a}", "stream_use": StreamUse.PER_EVENT.value, } } }, "api_b": { "event_transport": { "redis": { "url": f"redis://localhost:{port_b}", "stream_use": StreamUse.PER_EVENT.value, } } }, }, } ) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config, loop=loop)) await asyncio.sleep(0.1) await bus.api_a.event_a.fire_async() await bus.api_b.event_b.fire_async() connection_manager_a = bus.client.transport_registry.get_event_transport( "api_a" ).connection_manager connection_manager_b = bus.client.transport_registry.get_event_transport( "api_b" ).connection_manager with await connection_manager_a() as redis: assert await redis.xrange("api_a.event_a:stream") assert await redis.xrange("api_b.event_b:stream") == [] with await connection_manager_b() as redis: assert await redis.xrange("api_a.event_a:stream") == [] assert await redis.xrange("api_b.event_b:stream")
async def test_no_transport(): # No transports configured for any relevant api config = Config.load_dict( {"apis": { "default": { "event_transport": { "redis": {} } } }}, set_defaults=False) client = lightbus.BusClient(config=config) with pytest.raises(TransportNotFound): await client.call_rpc_remote("my_api", "test", kwargs={}, options={})
async def inner(): transport = TransactionalEventTransport( child_transport=lightbus.RedisEventTransport( redis_pool=pool, consumer_group_prefix="test_cg", consumer_name="test_consumer", stream_use=StreamUse.PER_EVENT, ) ) config = dummy_bus.client.config transport_registry = TransportRegistry().load_config(config) transport_registry.set_event_transport("default", transport) client = lightbus.BusClient(config=config, transport_registry=transport_registry) bus = lightbus.path.BusPath(name="", parent=None, client=client) return bus
async def test_listen_to_multiple_events_across_multiple_transports( loop, redis_server_url, redis_server_b_url ): redis_url_a = redis_server_url redis_url_b = redis_server_b_url logging.warning(f"Server A URL: {redis_url_a}") logging.warning(f"Server B URL: {redis_url_b}") config = Config.load_dict( { "bus": {"schema": {"transport": {"redis": {"url": redis_url_a}}}}, "apis": { "default": {"event_transport": {"redis": {"url": redis_url_a}}}, "api_b": {"event_transport": {"redis": {"url": redis_url_b}}}, }, } ) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config)) await bus.client.register_api_async(ApiA()) await bus.client.register_api_async(ApiB()) await asyncio.sleep(0.1) calls = 0 def listener(*args, **kwargs): nonlocal calls calls += 1 await bus.client.listen_for_events( events=[("api_a", "event_a"), ("api_b", "event_b")], listener=listener, listener_name="test" ) await asyncio.sleep(0.1) await bus.api_a.event_a.fire_async() await bus.api_b.event_b.fire_async() await asyncio.sleep(0.1) await bus.client.close_async() assert calls == 2
async def test_listen_to_multiple_events_across_multiple_transports(loop, server, redis_server_b): registry.add(ApiA()) registry.add(ApiB()) manually_set_plugins(plugins={}) redis_server_a = server port_a = redis_server_a.tcp_address.port port_b = redis_server_b.tcp_address.port logging.warning(f"Server A port: {port_a}") logging.warning(f"Server B port: {port_b}") config = Config.load_dict( { "bus": {"schema": {"transport": {"redis": {"url": f"redis://localhost:{port_a}"}}}}, "apis": { "default": {"event_transport": {"redis": {"url": f"redis://localhost:{port_a}"}}}, "api_b": {"event_transport": {"redis": {"url": f"redis://localhost:{port_b}"}}}, }, } ) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config, loop=loop)) await asyncio.sleep(0.1) calls = 0 def listener(*args, **kwargs): nonlocal calls calls += 1 await bus.listen_multiple_async([bus.api_a.event_a, bus.api_b.event_b], listener=listener) await asyncio.sleep(0.1) await bus.api_a.event_a.fire_async() await bus.api_b.event_b.fire_async() await asyncio.sleep(0.1) assert calls == 2
async def test_no_transport_type(): # Transports configured, but the wrong type of transport config = Config.load_dict( { "apis": { "default": { "event_transport": { "redis": {} } }, "my_api": { "event_transport": { "redis": {} } }, } }, set_defaults=False, ) client = lightbus.BusClient(config=config) with pytest.raises(TransportNotFound): await client.call_rpc_remote("my_api", "test", kwargs={}, options={})
async def test_multiple_rpc_transports(loop, server, redis_server_b, consume_rpcs): """Configure a bus with two redis transports and ensure they write to the correct redis servers""" registry.add(ApiA()) registry.add(ApiB()) manually_set_plugins(plugins={}) redis_server_a = server port_a = redis_server_a.tcp_address.port port_b = redis_server_b.tcp_address.port logging.warning(f"Server A port: {port_a}") logging.warning(f"Server B port: {port_b}") config = Config.load_dict( { "bus": {"schema": {"transport": {"redis": {"url": f"redis://localhost:{port_a}"}}}}, "apis": { "default": { "rpc_transport": {"redis": {"url": f"redis://localhost:{port_a}"}}, "result_transport": {"redis": {"url": f"redis://localhost:{port_a}"}}, }, "api_b": { "rpc_transport": {"redis": {"url": f"redis://localhost:{port_b}"}}, "result_transport": {"redis": {"url": f"redis://localhost:{port_b}"}}, }, }, } ) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config, loop=loop)) asyncio.ensure_future(consume_rpcs(bus)) await asyncio.sleep(0.1) await bus.api_a.rpc_a.call_async() await bus.api_b.rpc_b.call_async()
async def test_multiple_rpc_transports(loop, redis_server_url, redis_server_b_url, consume_rpcs): """Configure a bus with two redis transports and ensure they write to the correct redis servers""" redis_url_a = redis_server_url redis_url_b = redis_server_b_url logging.warning(f"Server A url: {redis_url_a}") logging.warning(f"Server B url: {redis_url_b}") config = Config.load_dict( { "bus": {"schema": {"transport": {"redis": {"url": redis_url_a}}}}, "apis": { "default": { "rpc_transport": {"redis": {"url": redis_url_a}}, "result_transport": {"redis": {"url": redis_url_a}}, }, "api_b": { "rpc_transport": {"redis": {"url": redis_url_b}}, "result_transport": {"redis": {"url": redis_url_b}}, }, }, } ) bus = BusPath(name="", parent=None, client=lightbus.BusClient(config=config)) await bus.client.register_api_async(ApiA()) await bus.client.register_api_async(ApiB()) task = asyncio.ensure_future(consume_rpcs(bus)) await asyncio.sleep(0.1) await bus.api_a.rpc_a.call_async() await bus.api_b.rpc_b.call_async() await asyncio.sleep(0.1) await cancel(task) await bus.client.close_async()