Skip to content

Commit

Permalink
Test coroutine interruption
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Mar 14, 2022
1 parent c5c0b80 commit d6713ac
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions jupyter_client/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Tests for the KernelClient"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import asyncio
import os
from textwrap import dedent
from unittest import TestCase

import pytest
from IPython.utils.capture import capture_output

from ..manager import start_new_async_kernel
from ..manager import start_new_kernel
from .utils import test_env
from jupyter_client.kernelspec import KernelSpecManager
Expand All @@ -18,6 +21,40 @@
pjoin = os.path.join


@pytest.mark.asyncio
async def test_interrupt_coroutine():
try:
KernelSpecManager().get_kernel_spec(NATIVE_KERNEL_NAME)
except NoSuchKernel:
pytest.skip()

km, kc = await start_new_async_kernel(kernel_name=NATIVE_KERNEL_NAME)

code = dedent(
"""
import asyncio
async def main():
print("sleeping")
await asyncio.sleep(0.5)
print("done")
await main()
"""
)
with capture_output() as io:
asyncio.create_task(kc.execute_interactive(code, timeout=TIMEOUT))
# wait for coroutine to start, this should print "sleeping"
await asyncio.sleep(0.2)
# interrupt kernel before coroutine completes execution, "done" should not be printed
await km.interrupt_kernel()

assert "sleeping" in io.stdout
assert "done" not in io.stdout
kc.stop_channels()
await km.shutdown_kernel()


class TestKernelClient(TestCase):
def setUp(self):
self.env_patch = test_env()
Expand Down

0 comments on commit d6713ac

Please sign in to comment.