From 3fe348015c0dd738963003eb0d27a4b4b4272750 Mon Sep 17 00:00:00 2001 From: Zach Sailer Date: Sat, 30 Apr 2022 17:35:07 -0700 Subject: [PATCH] get tests working --- jupyter_server/services/events/bus.py | 11 ++++++++-- .../services/events/mockextension/__init__.py | 6 ++++- .../events/mockextension/mock_extension.py | 4 ++-- tests/services/events/test_extension.py | 22 +++++++++++++------ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/jupyter_server/services/events/bus.py b/jupyter_server/services/events/bus.py index 0a4b06d3ac..0cbdbf07e9 100644 --- a/jupyter_server/services/events/bus.py +++ b/jupyter_server/services/events/bus.py @@ -1,8 +1,15 @@ +"""An EventBus for use in the Jupyter server. + +.. versionadded:: 2.0 +""" +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. from jupyter_telemetry.eventlog import EventLog from traitlets.config import SingletonConfigurable class EventBus(EventLog, SingletonConfigurable): - """A Jupyter EventLog as a Singleton, making it easy to - access from anywhere and log events. + """A singleton eventlog that behaves as an event + bus for emitting Jupyter Server (and extension) + event data. """ diff --git a/tests/services/events/mockextension/__init__.py b/tests/services/events/mockextension/__init__.py index cf43b71980..ed7c0e9d37 100644 --- a/tests/services/events/mockextension/__init__.py +++ b/tests/services/events/mockextension/__init__.py @@ -1,6 +1,10 @@ +from .mock_extension import _load_jupyter_server_extension + # Function that makes these extensions discoverable # by the test functions. + + def _jupyter_server_extension_points(): return [ - {"module": "tests.events.mock_extension"}, + {"module": "tests.services.events.mockextension"}, ] diff --git a/tests/services/events/mockextension/mock_extension.py b/tests/services/events/mockextension/mock_extension.py index cf5af2d2d0..9d46554063 100644 --- a/tests/services/events/mockextension/mock_extension.py +++ b/tests/services/events/mockextension/mock_extension.py @@ -10,13 +10,13 @@ def get(self): self.event_bus.record_event( schema_name="event.mockextension.jupyter.com/message", version=1, - event={"message": "Hello world, from mock extension!"}, + event={"event_message": "Hello world, from mock extension!"}, ) def _load_jupyter_server_extension(serverapp): # Register a schema with the EventBus - schema_file = pathlib.Path(__file__).parent / "mock_event_schema.yaml" + schema_file = pathlib.Path(__file__).parent / "mock_extension_event.yaml" serverapp.event_bus.register_schema_file(schema_file) serverapp.web_app.add_handlers( ".*$", [(url_path_join(serverapp.base_url, "/mock/event"), MockEventHandler)] diff --git a/tests/services/events/test_extension.py b/tests/services/events/test_extension.py index ed3f9d3f4a..a47943c636 100644 --- a/tests/services/events/test_extension.py +++ b/tests/services/events/test_extension.py @@ -1,24 +1,32 @@ import json -import pathlib import pytest @pytest.fixture -def event_bus(jp_serverapp): - event_bus = jp_serverapp.event_bus - event_bus.allowed_schemas = ["event.mockextension.jupyter.com/message"] - return event_bus +def jp_server_config(): + config = { + "ServerApp": { + "jpserver_extensions": {"tests.services.events.mockextension": True}, + }, + "EventBus": {"allowed_schemas": ["event.mockextension.jupyter.com/message"]}, + } + return config -async def test_subscribe_websocket(jp_ws_fetch, jp_fetch, event_bus): - # Open a websocket connection. +async def test_subscribe_websocket(jp_ws_fetch, jp_fetch): + # Open an event listener websocket ws = await jp_ws_fetch("/api/events/subscribe") + # Hit the extension endpoint that emits an event await jp_fetch("/mock/event") + + # Check the event listener for a message message = await ws.read_message() event_data = json.loads(message) + # Close websocket ws.close() + # Verify that an event message was received. assert event_data.get("event_message") == "Hello world, from mock extension!"