Skip to content

Commit

Permalink
get tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsailer committed May 11, 2022
1 parent d3afa58 commit 3fe3480
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
11 changes: 9 additions & 2 deletions jupyter_server/services/events/bus.py
Original file line number Diff line number Diff line change
@@ -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.
"""
6 changes: 5 additions & 1 deletion tests/services/events/mockextension/__init__.py
Original file line number Diff line number Diff line change
@@ -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"},
]
4 changes: 2 additions & 2 deletions tests/services/events/mockextension/mock_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
22 changes: 15 additions & 7 deletions tests/services/events/test_extension.py
Original file line number Diff line number Diff line change
@@ -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!"

0 comments on commit 3fe3480

Please sign in to comment.