|
1 | 1 | """Tests for the KernelClient"""
|
2 | 2 | # Copyright (c) Jupyter Development Team.
|
3 | 3 | # Distributed under the terms of the Modified BSD License.
|
| 4 | +import asyncio |
4 | 5 | import os
|
| 6 | +from textwrap import dedent |
5 | 7 | from unittest import TestCase
|
6 | 8 |
|
7 | 9 | import pytest
|
8 | 10 | from IPython.utils.capture import capture_output
|
9 | 11 |
|
10 |
| -from ..manager import start_new_kernel |
| 12 | +from ..manager import start_new_kernel, start_new_async_kernel |
11 | 13 | from .utils import test_env
|
12 | 14 | from jupyter_client.kernelspec import KernelSpecManager
|
13 | 15 | from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
|
|
18 | 20 | pjoin = os.path.join
|
19 | 21 |
|
20 | 22 |
|
| 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 | + |
21 | 55 | class TestKernelClient(TestCase):
|
22 | 56 | def setUp(self):
|
23 | 57 | self.env_patch = test_env()
|
|
0 commit comments