Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
  • Loading branch information
ivanpauno committed Dec 5, 2022
1 parent ff285e0 commit ead94fc
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions launch/test/launch/test_execute_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

"""Tests for the ExecuteLocal Action."""

import asyncio
import os
import psutil
import signal
import sys
import time

from launch import LaunchDescription
from launch import LaunchService
Expand All @@ -28,6 +32,8 @@
from launch.actions import TimerAction
from launch.descriptions import Executable

import osrf_pycommon.process_utils

import pytest


Expand Down Expand Up @@ -124,3 +130,37 @@ def generate_launch_description():
ls.include_launch_description(generate_launch_description())
assert 0 == ls.run()
assert expected_called_count == on_exit_callback.called_count


PYTHON_SCRIPT="""\
import time
while 1:
time.sleep(0.5)
"""

def test_kill_subprocesses():
"""Test launching a process with an environment variable."""
executable = ExecuteLocal(
process_description=Executable(
cmd=['python3', '-c', f'"{PYTHON_SCRIPT}"'],
),
shell=True,
output='screen',
)
ld = LaunchDescription([executable])
ls = LaunchService()
ls.include_launch_description(ld)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
run_async_task = loop.create_task(ls.run_async())
async def wait_for_subprocesses():
start = time.time()
while len(psutil.Process().children(recursive=True)) != 2:
await asyncio.sleep(0.5)
assert time.time() < start + 5., 'timed out waiting for processes to setup'
wait_for_subprocesses_task = loop.create_task(wait_for_subprocesses())
loop.run_until_complete(wait_for_subprocesses_task)
os.kill(executable.process_details['pid'], signal.SIGTERM)
loop.run_until_complete(run_async_task)
assert len(psutil.Process().children(recursive=True)) == 0

0 comments on commit ead94fc

Please sign in to comment.