|
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 |
|
@@ -138,3 +144,38 @@ def test_execute_process_with_output_dictionary(): |
138 | 144 | ls = LaunchService() |
139 | 145 | ls.include_launch_description(ld) |
140 | 146 | assert 0 == ls.run() |
| 147 | + |
| 148 | + |
| 149 | +PYTHON_SCRIPT="""\ |
| 150 | +import time |
| 151 | +
|
| 152 | +while 1: |
| 153 | + time.sleep(0.5) |
| 154 | +""" |
| 155 | + |
| 156 | + |
| 157 | +def test_kill_subprocesses(): |
| 158 | + """Test launching a process with an environment variable.""" |
| 159 | + executable = ExecuteLocal( |
| 160 | + process_description=Executable( |
| 161 | + cmd=['python3', '-c', f'"{PYTHON_SCRIPT}"'], |
| 162 | + ), |
| 163 | + shell=True, |
| 164 | + output='screen', |
| 165 | + ) |
| 166 | + ld = LaunchDescription([executable]) |
| 167 | + ls = LaunchService() |
| 168 | + ls.include_launch_description(ld) |
| 169 | + loop = asyncio.new_event_loop() |
| 170 | + asyncio.set_event_loop(loop) |
| 171 | + run_async_task = loop.create_task(ls.run_async()) |
| 172 | + async def wait_for_subprocesses(): |
| 173 | + start = time.time() |
| 174 | + while len(psutil.Process().children(recursive=True)) != 2: |
| 175 | + await asyncio.sleep(0.5) |
| 176 | + assert time.time() < start + 5., 'timed out waiting for processes to setup' |
| 177 | + wait_for_subprocesses_task = loop.create_task(wait_for_subprocesses()) |
| 178 | + loop.run_until_complete(wait_for_subprocesses_task) |
| 179 | + os.kill(executable.process_details['pid'], signal.SIGTERM) |
| 180 | + loop.run_until_complete(run_async_task) |
| 181 | + assert len(psutil.Process().children(recursive=True)) == 0 |
0 commit comments