Skip to content

Commit 5b43cdc

Browse files
committed
Add test case
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 514c149 commit 5b43cdc

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

launch/test/launch/test_execute_local.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
"""Tests for the ExecuteLocal Action."""
1919

20+
import asyncio
2021
import os
22+
import psutil
23+
import signal
2124
import sys
25+
import time
2226

2327
from launch import LaunchDescription
2428
from launch import LaunchService
@@ -28,6 +32,8 @@
2832
from launch.actions import TimerAction
2933
from launch.descriptions import Executable
3034

35+
import osrf_pycommon.process_utils
36+
3137
import pytest
3238

3339

@@ -138,3 +144,38 @@ def test_execute_process_with_output_dictionary():
138144
ls = LaunchService()
139145
ls.include_launch_description(ld)
140146
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

Comments
 (0)