|
17 | 17 |
|
18 | 18 | """Tests for the ExecuteLocal Action."""
|
19 | 19 |
|
| 20 | +import asyncio |
20 | 21 | import os
|
| 22 | +import psutil |
| 23 | +import signal |
21 | 24 | import sys
|
| 25 | +import time |
22 | 26 |
|
23 | 27 | from launch import LaunchDescription
|
24 | 28 | from launch import LaunchService
|
|
28 | 32 | from launch.actions import TimerAction
|
29 | 33 | from launch.descriptions import Executable
|
30 | 34 |
|
| 35 | +import osrf_pycommon.process_utils |
| 36 | + |
31 | 37 | import pytest
|
32 | 38 |
|
33 | 39 |
|
@@ -124,3 +130,37 @@ def generate_launch_description():
|
124 | 130 | ls.include_launch_description(generate_launch_description())
|
125 | 131 | assert 0 == ls.run()
|
126 | 132 | assert expected_called_count == on_exit_callback.called_count
|
| 133 | + |
| 134 | + |
| 135 | +PYTHON_SCRIPT="""\ |
| 136 | +import time |
| 137 | +
|
| 138 | +while 1: |
| 139 | + time.sleep(0.5) |
| 140 | +""" |
| 141 | + |
| 142 | +def test_kill_subprocesses(): |
| 143 | + """Test launching a process with an environment variable.""" |
| 144 | + executable = ExecuteLocal( |
| 145 | + process_description=Executable( |
| 146 | + cmd=['python3', '-c', f'"{PYTHON_SCRIPT}"'], |
| 147 | + ), |
| 148 | + shell=True, |
| 149 | + output='screen', |
| 150 | + ) |
| 151 | + ld = LaunchDescription([executable]) |
| 152 | + ls = LaunchService() |
| 153 | + ls.include_launch_description(ld) |
| 154 | + loop = asyncio.new_event_loop() |
| 155 | + asyncio.set_event_loop(loop) |
| 156 | + run_async_task = loop.create_task(ls.run_async()) |
| 157 | + async def wait_for_subprocesses(): |
| 158 | + start = time.time() |
| 159 | + while len(psutil.Process().children(recursive=True)) != 2: |
| 160 | + await asyncio.sleep(0.5) |
| 161 | + assert time.time() < start + 5., 'timed out waiting for processes to setup' |
| 162 | + wait_for_subprocesses_task = loop.create_task(wait_for_subprocesses()) |
| 163 | + loop.run_until_complete(wait_for_subprocesses_task) |
| 164 | + os.kill(executable.process_details['pid'], signal.SIGTERM) |
| 165 | + loop.run_until_complete(run_async_task) |
| 166 | + assert len(psutil.Process().children(recursive=True)) == 0 |
0 commit comments