-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more websocket connection tests and fix bugs (#1085)
- Loading branch information
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import asyncio | ||
import json | ||
from unittest.mock import MagicMock | ||
|
||
from jupyter_client.jsonutil import json_clean, json_default | ||
from jupyter_client.session import Session | ||
from tornado.httpserver import HTTPRequest | ||
from tornado.websocket import WebSocketHandler | ||
|
||
from jupyter_server.serverapp import ServerApp | ||
from jupyter_server.services.kernels.connection.channels import ( | ||
ZMQChannelsWebsocketConnection, | ||
) | ||
|
||
|
||
async def test_websocket_connection(jp_serverapp): | ||
app: ServerApp = jp_serverapp | ||
kernel_id = await app.kernel_manager.start_kernel() | ||
kernel = app.kernel_manager.get_kernel(kernel_id) | ||
request = HTTPRequest("foo", "GET") | ||
request.connection = MagicMock() | ||
handler = WebSocketHandler(app.web_app, request) | ||
handler.ws_connection = MagicMock() | ||
handler.ws_connection.is_closing = lambda: False | ||
conn = ZMQChannelsWebsocketConnection(parent=kernel, websocket_handler=handler) | ||
await conn.prepare() | ||
conn.connect() | ||
await asyncio.wrap_future(conn.nudge()) | ||
session: Session = kernel.session | ||
msg = session.msg("data_pub", content={"a": "b"}) | ||
data = json.dumps( | ||
json_clean(msg), | ||
default=json_default, | ||
ensure_ascii=False, | ||
allow_nan=False, | ||
) | ||
conn.handle_incoming_message(data) | ||
conn.handle_outgoing_message("iopub", session.serialize(msg)) | ||
assert ( | ||
conn.select_subprotocol(["v1.kernel.websocket.jupyter.org"]) | ||
== "v1.kernel.websocket.jupyter.org" | ||
) | ||
conn.write_stderr("test", {}) | ||
conn.on_kernel_restarted() | ||
conn.on_restart_failed() | ||
conn._on_error("shell", msg, session.serialize(msg)) |