Skip to content

Commit bb630cc

Browse files
committed
Test coroutine interruption
1 parent c5c0b80 commit bb630cc

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

jupyter_client/tests/test_client.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Tests for the KernelClient"""
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
4+
import asyncio
45
import os
6+
from textwrap import dedent
57
from unittest import TestCase
68

79
import pytest
810
from IPython.utils.capture import capture_output
911

10-
from ..manager import start_new_kernel
12+
from ..manager import start_new_kernel, start_new_async_kernel
1113
from .utils import test_env
1214
from jupyter_client.kernelspec import KernelSpecManager
1315
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
@@ -18,6 +20,38 @@
1820
pjoin = os.path.join
1921

2022

23+
@pytest.mark.asyncio
24+
async def test_interrupt_coroutine():
25+
try:
26+
KernelSpecManager().get_kernel_spec(NATIVE_KERNEL_NAME)
27+
except NoSuchKernel:
28+
pytest.skip()
29+
30+
km, kc = await start_new_async_kernel(kernel_name=NATIVE_KERNEL_NAME)
31+
32+
code = dedent("""
33+
import asyncio
34+
35+
async def main():
36+
print("sleeping")
37+
await asyncio.sleep(0.5)
38+
print("done")
39+
40+
await main()
41+
""")
42+
with capture_output() as io:
43+
task = asyncio.create_task(kc.execute_interactive(code, timeout=TIMEOUT))
44+
# wait for coroutine to start, this should print "sleeping"
45+
await asyncio.sleep(0.2)
46+
# interrupt kernel before coroutine completes execution, "done" should not be printed
47+
await km.interrupt_kernel()
48+
49+
assert "sleeping" in io.stdout
50+
assert "done" not in io.stdout
51+
kc.stop_channels()
52+
await km.shutdown_kernel()
53+
54+
2155
class TestKernelClient(TestCase):
2256
def setUp(self):
2357
self.env_patch = test_env()

0 commit comments

Comments
 (0)