async def test_send_event(dummy_bus: BusPath, get_dummy_events): manually_set_plugins( plugins={ "metrics": MetricsPlugin(service_name="foo", process_name="bar") }) registry.add(TestApi()) await dummy_bus.example.test.my_event.fire_async(f=123) # What events were fired? event_messages = get_dummy_events() assert len( event_messages ) == 2 # First is the actual event, followed by the metrics event # rpc_response_received assert event_messages[1].api_name == "internal.metrics" assert event_messages[1].event_name == "event_fired" assert event_messages[1].kwargs.pop("timestamp") assert event_messages[1].kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", }
async def test_execute_events(dummy_bus: BusPath, dummy_listener, get_dummy_events, mocker): event_transport = dummy_bus.client.transport_registry.get_event_transport( "default") mocker.patch.object( event_transport, "_get_fake_message", return_value=EventMessage(api_name="example.test", event_name="my_event", kwargs={"f": 123}), ) await dummy_listener("example.test", "my_event") # Setup the bus and do the call manually_set_plugins( plugins={ "metrics": MetricsPlugin(service_name="foo", process_name="bar") }) registry.add(TestApi()) # The dummy transport will fire an every every 0.1 seconds await asyncio.sleep(0.15) event_messages = get_dummy_events() assert len(event_messages) == 2 # before_rpc_execution assert event_messages[0].api_name == "internal.metrics" assert event_messages[0].event_name == "event_received" assert event_messages[0].kwargs.pop("timestamp") assert event_messages[0].kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", } # after_rpc_execution assert event_messages[1].api_name == "internal.metrics" assert event_messages[1].event_name == "event_processed" assert event_messages[1].kwargs.pop("timestamp") assert event_messages[1].kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", }
async def test_local_rpc_call(loop, dummy_bus: BusPath, consume_rpcs, get_dummy_events, mocker): rpc_transport = dummy_bus.client.transport_registry.get_rpc_transport( "default") mocker.patch.object( rpc_transport, "_get_fake_messages", return_value=[ RpcMessage(id="123abc", api_name="example.test", procedure_name="my_method", kwargs={"f": 123}) ], ) # Setup the bus and do the call manually_set_plugins( plugins={ "metrics": MetricsPlugin(service_name="foo", process_name="bar") }) registry.add(TestApi()) task = asyncio.ensure_future(consume_rpcs(dummy_bus), loop=loop) # The dummy transport will fire an every every 0.1 seconds await asyncio.sleep(0.15) await cancel(task) event_messages = get_dummy_events() assert len(event_messages) == 2, event_messages # before_rpc_execution assert event_messages[0].api_name == "internal.metrics" assert event_messages[0].event_name == "rpc_call_received" assert event_messages[0].kwargs.pop("timestamp") assert event_messages[0].kwargs == { "api_name": "example.test", "procedure_name": "my_method", "id": "123abc", "service_name": "foo", "process_name": "bar", } # after_rpc_execution assert event_messages[1].api_name == "internal.metrics" assert event_messages[1].event_name == "rpc_response_sent" assert event_messages[1].kwargs.pop("timestamp") assert event_messages[1].kwargs == { "api_name": "example.test", "procedure_name": "my_method", "id": "123abc", "result": "value", "service_name": "foo", "process_name": "bar", }
async def test_execute_events(dummy_bus: BusNode, event_consumer, get_dummy_events, mocker): mocker.patch.object(dummy_bus.bus_client.event_transport, '_get_fake_messages', return_value=[ EventMessage(api_name='example.test', event_name='my_event', kwargs={'f': 123}) ]) # Setup the bus and do the call manually_set_plugins(plugins={'metrics': MetricsPlugin()}) registry.add(TestApi()) # The dummy transport will fire an every every 0.1 seconds await asyncio.sleep(0.15) event_messages = get_dummy_events() assert len(event_messages) == 2 # before_rpc_execution assert event_messages[0].api_name == 'internal.metrics' assert event_messages[0].event_name == 'event_received' assert event_messages[0].kwargs.pop('timestamp') assert event_messages[0].kwargs.pop('process_name') assert event_messages[0].kwargs == { 'api_name': 'example.test', 'event_name': 'my_event', 'event_id': 'event_id', 'kwargs': { 'f': 123 } } # after_rpc_execution assert event_messages[1].api_name == 'internal.metrics' assert event_messages[1].event_name == 'event_processed' assert event_messages[1].kwargs.pop('timestamp') assert event_messages[1].kwargs.pop('process_name') assert event_messages[1].kwargs == { 'api_name': 'example.test', 'event_name': 'my_event', 'event_id': 'event_id', 'kwargs': { 'f': 123 } }
async def test_remote_rpc_call(dummy_bus: BusPath, get_dummy_events): # Setup the bus and do the call manually_set_plugins( plugins={ "metrics": MetricsPlugin(service_name="foo", process_name="bar") }) registry.add(TestApi()) await dummy_bus.example.test.my_method.call_async(f=123) # What events were fired? event_messages = get_dummy_events() assert len(event_messages) == 2 # rpc_call_sent assert event_messages[0].api_name == "internal.metrics" assert event_messages[0].event_name == "rpc_call_sent" # Pop these next two as the values are variable assert event_messages[0].kwargs.pop("timestamp") assert event_messages[0].kwargs.pop("id") assert event_messages[0].kwargs == { "api_name": "example.test", "procedure_name": "my_method", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", } # rpc_response_received assert event_messages[1].api_name == "internal.metrics" assert event_messages[1].event_name == "rpc_response_received" # Pop these next two as the values are variable assert event_messages[1].kwargs.pop("timestamp") assert event_messages[1].kwargs.pop("id") assert event_messages[1].kwargs == { "api_name": "example.test", "procedure_name": "my_method", "service_name": "foo", "process_name": "bar", }
async def test_remote_rpc_call(dummy_bus: BusNode, get_dummy_events): # Setup the bus and do the call manually_set_plugins(plugins={'metrics': MetricsPlugin()}) registry.add(TestApi()) await dummy_bus.example.test.my_method.call_async(f=123) # What events were fired? event_messages = get_dummy_events() assert len(event_messages) == 2 # rpc_call_sent assert event_messages[0].api_name == 'internal.metrics' assert event_messages[0].event_name == 'rpc_call_sent' # Pop these next two as the values are variable assert event_messages[0].kwargs.pop('timestamp') assert event_messages[0].kwargs.pop('rpc_id') assert event_messages[0].kwargs.pop('process_name') assert event_messages[0].kwargs == { 'api_name': 'example.test', 'procedure_name': 'my_method', 'kwargs': { 'f': 123 }, } # rpc_response_received assert event_messages[1].api_name == 'internal.metrics' assert event_messages[1].event_name == 'rpc_response_received' # Pop these next two as the values are variable assert event_messages[1].kwargs.pop('timestamp') assert event_messages[1].kwargs.pop('rpc_id') assert event_messages[1].kwargs.pop('process_name') assert event_messages[1].kwargs == { 'api_name': 'example.test', 'procedure_name': 'my_method', }
async def test_send_event(dummy_bus: BusNode, get_dummy_events): manually_set_plugins(plugins={'metrics': MetricsPlugin()}) registry.add(TestApi()) await dummy_bus.example.test.my_event.fire_async(f=123) # What events were fired? event_messages = get_dummy_events() assert len( event_messages ) == 2 # First is the actual event, followed by the metrics event # rpc_response_received assert event_messages[1].api_name == 'internal.metrics' assert event_messages[1].event_name == 'event_fired' assert event_messages[1].kwargs.pop('timestamp') assert event_messages[1].kwargs.pop('process_name') assert event_messages[1].kwargs == { 'api_name': 'example.test', 'event_name': 'my_event', 'event_id': 'event_id', 'kwargs': { 'f': 123 } }
async def test_send_event(dummy_bus: BusPath, queue_mocker: Type[BusQueueMockerContext]): dummy_bus.client.plugin_registry.set_plugins( plugins=[MetricsPlugin(service_name="foo", process_name="bar")]) dummy_bus.client.register_api(TestApi()) with queue_mocker(dummy_bus.client) as q: await dummy_bus.my_company.auth.my_event.fire_async(f=123) event_commands = q.event.to_transport.commands.get_all(SendEventCommand) # First is the actual event, followed by the metrics event assert len(event_commands) == 2, event_commands # rpc_response_received assert event_commands[1].message.api_name == "internal.metrics" assert event_commands[1].message.event_name == "event_fired" assert event_commands[1].message.kwargs["timestamp"] assert event_commands[1].message.kwargs["api_name"] == "my_company.auth" assert event_commands[1].message.kwargs["event_name"] == "my_event" assert event_commands[1].message.kwargs["event_id"] == "event_id" assert event_commands[1].message.kwargs["kwargs"] == {"f": 123} assert event_commands[1].message.kwargs["service_name"] == "foo" assert event_commands[1].message.kwargs["process_name"] == "bar"
async def test_execute_events(dummy_bus: BusPath, dummy_listener, get_dummy_events, mocker): event_transport = dummy_bus.client.transport_registry.get_event_transport( "default") mocker.patch.object( event_transport, "_get_fake_message", return_value=EventMessage(api_name="example.test", event_name="my_event", kwargs={"f": 123}), ) await dummy_listener("example.test", "my_event") await dummy_bus.client._setup_server() # Setup the bus and do the call dummy_bus.client.plugin_registry.set_plugins( plugins=[MetricsPlugin(service_name="foo", process_name="bar")]) dummy_bus.client.register_api(TestApi()) # The dummy transport will fire an every every 0.1 seconds await asyncio.sleep(0.15) # Arrange messages in a dict indexed by name (makes checking results) event_messages = {} event_message: EventMessage for event_message in get_dummy_events(): event_messages[event_message.canonical_name] = event_message assert "internal.metrics.event_received" in event_messages assert "internal.metrics.event_processed" in event_messages # before_rpc_execution event = event_messages["internal.metrics.event_received"] assert event.api_name == "internal.metrics" assert event.event_name == "event_received" assert event.kwargs.pop("timestamp") assert event.kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", } # after_rpc_execution event = event_messages["internal.metrics.event_processed"] assert event.api_name == "internal.metrics" assert event.event_name == "event_processed" assert event.kwargs.pop("timestamp") assert event.kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", }
async def test_rpc_call(dummy_bus: BusPath, worker: Worker, queue_mocker: Type[BusQueueMockerContext]): # Setup the bus and do the call dummy_bus.client.plugin_registry.set_plugins( plugins=[MetricsPlugin(service_name="foo", process_name="bar")]) dummy_bus.client.register_api(TestApi()) async with worker(dummy_bus): with queue_mocker(dummy_bus.client) as q: await dummy_bus.my_company.auth.check_password.call_async( username="******", password="******") event_commands = q.event.to_transport.commands.get_all(SendEventCommand) assert len(event_commands) == 4, event_commands # Arrange messages in a dict indexed by name (makes checking results) event_messages = {} for event_command in q.event.to_transport.commands.get_all( SendEventCommand): event_messages[ event_command.message.event_name] = event_command.message # rpc_call_sent assert event_messages["rpc_call_sent"].api_name == "internal.metrics" assert event_messages["rpc_call_sent"].event_name == "rpc_call_sent" assert event_messages["rpc_call_sent"].kwargs["timestamp"] assert event_messages["rpc_call_sent"].kwargs[ "api_name"] == "my_company.auth" assert event_messages["rpc_call_sent"].kwargs[ "procedure_name"] == "check_password" assert event_messages["rpc_call_sent"].kwargs["id"] assert event_messages["rpc_call_sent"].kwargs["kwargs"] == { "username": "******", "password": "******" } assert event_messages["rpc_call_sent"].kwargs["service_name"] == "foo" assert event_messages["rpc_call_sent"].kwargs["process_name"] == "bar" # rpc_call_received assert event_messages["rpc_call_received"].api_name == "internal.metrics" assert event_messages[ "rpc_call_received"].event_name == "rpc_call_received" assert event_messages["rpc_call_received"].kwargs["timestamp"] assert event_messages["rpc_call_received"].kwargs[ "api_name"] == "my_company.auth" assert event_messages["rpc_call_received"].kwargs[ "procedure_name"] == "check_password" assert event_messages["rpc_call_received"].kwargs["id"] assert event_messages["rpc_call_received"].kwargs["service_name"] == "foo" assert event_messages["rpc_call_received"].kwargs["process_name"] == "bar" # rpc_response_sent assert event_messages["rpc_response_sent"].api_name == "internal.metrics" assert event_messages[ "rpc_response_sent"].event_name == "rpc_response_sent" assert event_messages["rpc_response_sent"].kwargs["timestamp"] assert event_messages["rpc_response_sent"].kwargs[ "api_name"] == "my_company.auth" assert event_messages["rpc_response_sent"].kwargs[ "procedure_name"] == "check_password" assert event_messages["rpc_response_sent"].kwargs["id"] assert event_messages["rpc_response_sent"].kwargs["service_name"] == "foo" assert event_messages["rpc_response_sent"].kwargs["process_name"] == "bar" assert event_messages["rpc_response_sent"].kwargs["result"] == True # rpc_response_received assert event_messages[ "rpc_response_received"].api_name == "internal.metrics" assert event_messages[ "rpc_response_received"].event_name == "rpc_response_received" assert event_messages["rpc_response_received"].kwargs["timestamp"] assert event_messages["rpc_response_received"].kwargs[ "api_name"] == "my_company.auth" assert event_messages["rpc_response_received"].kwargs[ "procedure_name"] == "check_password" assert event_messages["rpc_response_received"].kwargs["id"] assert event_messages["rpc_response_received"].kwargs[ "service_name"] == "foo" assert event_messages["rpc_response_received"].kwargs[ "process_name"] == "bar"
async def test_execute_events(dummy_bus: BusPath, worker: Worker, queue_mocker: Type[BusQueueMockerContext]): dummy_bus.client.register_api(TestApi()) dummy_bus.client.plugin_registry.set_plugins( plugins=[MetricsPlugin(service_name="foo", process_name="bar")]) event_message = EventMessage(api_name="example.test", event_name="my_event", kwargs={"f": 123}) async with worker(dummy_bus): with queue_mocker(dummy_bus.client) as q: await dummy_bus.client.event_client._on_message( event_message=event_message, listener=lambda *a, **kw: None, options={}, on_error=OnError.SHUTDOWN, ) # Arrange messages in a dict indexed by name (makes checking results) event_messages = {} event_message: EventMessage for event_command in q.event.to_transport.commands.get_all( SendEventCommand): event_messages[ event_command.message.canonical_name] = event_command.message assert "internal.metrics.event_received" in event_messages assert "internal.metrics.event_processed" in event_messages # before_rpc_execution event = event_messages["internal.metrics.event_received"] assert event.api_name == "internal.metrics" assert event.event_name == "event_received" assert event.kwargs.pop("timestamp") assert event.kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", } # after_rpc_execution event = event_messages["internal.metrics.event_processed"] assert event.api_name == "internal.metrics" assert event.event_name == "event_processed" assert event.kwargs.pop("timestamp") assert event.kwargs == { "api_name": "example.test", "event_name": "my_event", "event_id": "event_id", "kwargs": { "f": 123 }, "service_name": "foo", "process_name": "bar", }