Skip to content

Commit f84c8b4

Browse files
committed
Add a test to ensure run_sync cancels the pending task
1 parent 7aed569 commit f84c8b4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

jupyter_client/tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
from unittest import mock
3+
4+
import pytest
5+
6+
from jupyter_client.utils import run_sync
7+
8+
9+
@pytest.fixture
10+
def loop():
11+
loop = asyncio.new_event_loop()
12+
asyncio.set_event_loop(loop)
13+
return loop
14+
15+
16+
def test_run_sync_clean_up_task(loop):
17+
async def coro_never_called():
18+
pytest.fail("The call to this coroutine is not expected")
19+
20+
# Ensure that run_sync cancels the pending task
21+
with mock.patch.object(loop, "run_until_complete") as patched_loop:
22+
patched_loop.side_effect = KeyboardInterrupt
23+
with mock.patch("asyncio.ensure_future") as patched_ensure_future:
24+
mock_future = mock.Mock()
25+
patched_ensure_future.return_value = mock_future
26+
with pytest.raises(KeyboardInterrupt):
27+
run_sync(coro_never_called)()
28+
mock_future.cancel.assert_called_once()
29+
# Suppress 'coroutine ... was never awaited' warning
30+
patched_ensure_future.call_args[0][0].close()

0 commit comments

Comments
 (0)