async def test_ancestors(): root_node = BusPath(name="", parent=None, client=None) child_node1 = BusPath(name="my_api", parent=root_node, client=None) child_node2 = BusPath(name="auth", parent=child_node1, client=None) assert list(child_node2.ancestors(include_self=True)) == [ child_node2, child_node1, root_node ]
async def test_fully_qualified_name(): root_node = BusPath(name="", parent=None, client=None) child_node1 = BusPath(name="my_api", parent=root_node, client=None) child_node2 = BusPath(name="auth", parent=child_node1, client=None) assert root_node.fully_qualified_name == "" assert child_node1.fully_qualified_name == "my_api" assert child_node1.fully_qualified_name == "my_api" assert str(child_node2) == "my_api.auth"
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_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_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()
def test_server_start_stop(mocker, called_hooks, dummy_bus: BusPath, add_base_plugin, dummy_api): add_base_plugin() mocker.patch.object(BusClient, "_run_forever") dummy_bus.run_forever() assert called_hooks() == ["before_server_start", "after_server_stopped"]
async def test_init_root_with_name(): with pytest.raises(InvalidBusPathConfiguration): BusPath(name="root", parent=None, client=None)