Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions launch_pytest/launch_pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,17 @@ def pytest_fixture_setup(fixturedef, request):
))
ready = get_ready_to_test_action(ld)
asyncio.set_event_loop(event_loop)
event = asyncio.Event()
ready._add_callback(lambda: event.set())
run_until_complete(event_loop, event.wait())
ready_future = event_loop.create_future()

def wakeup(is_ready):
if not ready_future.done():
ready_future.set_result(is_ready)

ready._add_callback(functools.partial(wakeup, True))
run_async_task.add_done_callback(lambda _: wakeup(False))
run_until_complete(event_loop, ready_future)
if not ready_future.result():
fail(f'{eprefix} launch service stopped before ReadyToTest')
# this is guaranteed by the current run_async() implementation, let's check it just in case
# it changes in the future
assert ls.event_loop is event_loop
Expand Down
49 changes: 49 additions & 0 deletions launch_pytest/test/launch_pytest/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,55 @@
from pathlib import Path
import shutil

import pytest


@pytest.mark.parametrize('action', [
"IncludeLaunchDescription(PythonLaunchDescriptionSource('missing.launch.py'))",
"GroupAction(actions=[ReadyToTest()], condition=IfCondition('false'))",
])
def test_launch_service_stops_before_ready(testdir, action):
testdir.makepyfile(f"""\
from launch import LaunchDescription
from launch.actions import GroupAction, IncludeLaunchDescription
from launch.conditions import IfCondition
from launch.launch_description_sources import PythonLaunchDescriptionSource
import launch_pytest
from launch_pytest.actions import ReadyToTest
import pytest

@launch_pytest.fixture
def launch_description():
return LaunchDescription([{action}])

@pytest.mark.launch(fixture=launch_description)
def test_case():
pytest.fail('test body must not run before ReadyToTest')
""")
result = testdir.runpytest_subprocess(timeout=10)
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(['*launch service stopped before ReadyToTest*'])


def test_launch_fixture_waits_for_delayed_ready(testdir):
testdir.makepyfile("""\
from launch import LaunchDescription
from launch.actions import TimerAction
import launch_pytest
from launch_pytest.actions import ReadyToTest
import pytest

@launch_pytest.fixture
def launch_description():
return LaunchDescription([TimerAction(period=0.01, actions=[ReadyToTest()])])

@pytest.mark.launch(fixture=launch_description)
def test_case():
pass
""")
result = testdir.runpytest_subprocess(timeout=10)
result.assert_outcomes(passed=1)


def test_launch_fixture_is_not_a_launch_description(testdir):
testdir.makepyfile("""\
Expand Down