Skip to content

Commit

Permalink
Add tests which demonstrate issue #4
Browse files Browse the repository at this point in the history
Control-C and/or signalling with SIGINT doesn't result in clean
cancellation and teardown of a program that has spawned subproceses.
These tests demonstrate this issue as is documented in
#4.
  • Loading branch information
goodboy committed Feb 14, 2020
1 parent b1f366f commit cd7583e
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions tests/core/test_open_in_process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pickle
import signal
import os

import pytest
import trio
Expand All @@ -14,12 +15,26 @@ async def do_sleep_forever():

await trio.sleep_forever()

with trio.fail_after(2):
async with open_in_process(do_sleep_forever) as proc:
proc.terminate()
async with open_in_process(do_sleep_forever) as proc:
proc.terminate()
assert proc.returncode == 15


@pytest.mark.trio
async def test_open_in_proc_cancel_via_fail_after():
"""Test that a ``trio`` initated timeout cancellation fails as expected.
"""
async def do_sleep_forever():
import trio

await trio.sleep_forever()

with pytest.raises(trio.TooSlowError):
with trio.fail_after(0.5):
async with open_in_process(do_sleep_forever):
pass


@pytest.mark.trio
async def test_open_in_proc_kill_while_running():
async def do_sleep_forever():
Expand Down Expand Up @@ -47,6 +62,42 @@ async def monitor_for_interrupt():
assert proc.returncode == 2


@pytest.mark.trio
async def test_open_in_proc_cancel_via_SIGINT():
pid = os.getpid()

async def do_sleep_forever():
import trio

await trio.sleep_forever()

with trio.fail_after(2):
async with open_in_process(do_sleep_forever):
os.kill(pid, signal.SIGINT)
await trio.sleep_forever()


@pytest.mark.trio
async def test_open_in_proc_cancel_via_SIGINT_other_task():
pid = os.getpid()

async def do_sleep_forever():
import trio

await trio.sleep_forever()

async def spawn_and_sleep_forever(task_status=trio.TASK_STATUS_IGNORED):
async with open_in_process(do_sleep_forever):
task_status.started()
await trio.sleep_forever()

with trio.fail_after(2):
# should never timeout since SIGINT should cancel the current program
async with trio.open_nursery() as n:
await n.start(spawn_and_sleep_forever)
os.kill(pid, signal.SIGINT)


@pytest.mark.trio
async def test_open_proc_invalid_function_call():
async def takes_no_args():
Expand Down

0 comments on commit cd7583e

Please sign in to comment.