Skip to content

Commit

Permalink
Add more gateway tests (jupyter-server#1078)
Browse files Browse the repository at this point in the history
* add more gateway tests

* fix mock gateway
  • Loading branch information
blink1073 authored Nov 20, 2022
1 parent 884b72e commit f583a11
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jupyter_server/gateway/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class GatewayWebSocketClient(LoggingConfigurable):
"""Proxy web socket connection to a kernel/enterprise gateway."""

def __init__(self, **kwargs):
super().__init__(**kwargs)
super().__init__()
self.kernel_id = None
self.ws = None
self.ws_future: Future = Future()
Expand Down
20 changes: 18 additions & 2 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ async def mock_gateway_request(url, **kwargs):
else:
raise HTTPError(404, message="Kernelspec does not exist: %s" % requested_kernelspec)

# Fetch kernelspec asset
if endpoint.rfind("/kernelspecs/") >= 0 and method == "GET":
response_buf = BytesIO(b"foo")
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response

# Create kernel
if endpoint.endswith("/api/kernels") and method == "POST":
json_body = json.loads(kwargs["body"])
Expand Down Expand Up @@ -393,7 +399,7 @@ async def test_gateway_class_mappings(init_gateway, jp_serverapp):
assert jp_serverapp.kernel_spec_manager_class.__name__ == "GatewayKernelSpecManager"


async def test_gateway_get_kernelspecs(init_gateway, jp_fetch):
async def test_gateway_get_kernelspecs(init_gateway, jp_fetch, jp_serverapp):
# Validate that kernelspecs come from gateway.
with mocked_gateway:
r = await jp_fetch("api", "kernelspecs", method="GET")
Expand All @@ -412,6 +418,10 @@ async def test_gateway_get_named_kernelspec(init_gateway, jp_fetch):
kspec_foo = json.loads(r.body.decode("utf-8"))
assert kspec_foo.get("name") == "kspec_foo"

r = await jp_fetch("kernelspecs", "kspec_foo", "hi", method="GET")
assert r.code == 200
assert r.body == b"foo"

with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "kernelspecs", "no_such_spec", method="GET")
assert expected_http_error(e, 404)
Expand Down Expand Up @@ -443,7 +453,7 @@ async def test_gateway_session_lifecycle(init_gateway, jp_root_dir, jp_fetch):
assert await is_kernel_running(jp_fetch, kernel_id) is False


async def test_gateway_kernel_lifecycle(init_gateway, jp_fetch):
async def test_gateway_kernel_lifecycle(init_gateway, jp_serverapp, jp_ws_fetch, jp_fetch):
# Validate kernel lifecycle functions; create, interrupt, restart and delete.

# create
Expand All @@ -452,6 +462,12 @@ async def test_gateway_kernel_lifecycle(init_gateway, jp_fetch):
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True

ws = await jp_ws_fetch("api", "kernels", kernel_id, "channels")
ws.ping()
ws.write_message(b"hi")
ws.on_message(b"hi")
ws.close()

# interrupt
await interrupt_kernel(jp_fetch, kernel_id)

Expand Down

0 comments on commit f583a11

Please sign in to comment.