Skip to content

Commit d6713ac

Browse files
committed
Test coroutine interruption
1 parent c5c0b80 commit d6713ac

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

jupyter_client/tests/test_client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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

12+
from ..manager import start_new_async_kernel
1013
from ..manager import start_new_kernel
1114
from .utils import test_env
1215
from jupyter_client.kernelspec import KernelSpecManager
@@ -18,6 +21,40 @@
1821
pjoin = os.path.join
1922

2023

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

0 commit comments

Comments
 (0)