Skip to content

Commit 6ba7e44

Browse files
committed
Add auto_shutdown, shutdown_when_idle options
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent bf8c790 commit 6ba7e44

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

launch_testing/launch_testing/pytest/fixture.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def scope_gt(scope1, scope2):
2727
from _pytest.fixtures import scopemismatch as scope_gt
2828

2929

30-
def finalize_launch_service(launch_service, eprefix=''):
31-
launch_service.shutdown(force_sync=True)
30+
def finalize_launch_service(launch_service, eprefix='', auto_shutdown=True):
31+
if auto_shutdown:
32+
launch_service.shutdown(force_sync=True)
3233
loop = launch_service.event_loop
3334
if loop is not None and not loop.is_closed():
3435
rc = loop.run_until_complete(launch_service.task)
@@ -65,12 +66,26 @@ def event_loop():
6566
return event_loop
6667

6768

68-
def fixture(decorated = None, *args, **kwargs):
69+
def fixture(
70+
decorated = None,
71+
*args,
72+
shutdown_when_idle = True,
73+
auto_shutdown = True,
74+
**kwargs
75+
):
6976
"""
7077
Decorate launch_test fixtures.
7178
72-
For documentation on the supported arguments, see
79+
See also
7380
https://docs.pytest.org/en/latest/reference/reference.html#pytest-fixture.
81+
82+
:param decorated: object to be decorated.
83+
:param \*args: extra posicional arguments to be passed to pytest.fixture().
84+
:param shutdown_when_idle: when true, the launch service will shutdown when idle.
85+
:param auto_shutdown: when true, the launch service will be shutdown automatically
86+
after all pre-shutdown tests get run. If false, shutdown needs to be signaled in a
87+
different way or the launch fixture should be self terminating.
88+
:param \**kwargs: extra keyword arguments to be passed to pytest.fixture().
7489
"""
7590
# Automagically override the event_loop and launch_testing fixtures
7691
# with a fixture of the correct scope.
@@ -97,6 +112,10 @@ def fixture(decorated = None, *args, **kwargs):
97112

98113
def decorator(fixture_function):
99114
fixture_function._launch_pytest_fixture = True
115+
fixture_function._launch_pytest_fixture_options = {
116+
'shutdown_when_idle': shutdown_when_idle,
117+
'auto_shutdown': auto_shutdown,
118+
}
100119
return pytest.fixture(fixture_function, *args, **kwargs)
101120
if decorated is None:
102121
return decorator

launch_testing/launch_testing/pytest/plugin.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def get_ready_to_test_action(launch_description):
9696
def pytest_fixture_setup(fixturedef, request):
9797
"""Set up launch service for all launch_pytest fixtures."""
9898
if getattr(fixturedef.func, '_launch_pytest_fixture', False):
99+
options = fixturedef.func._launch_pytest_fixture_options
99100
eprefix = f"When running launch_pytest fixture '{fixturedef.func.__name__}':"
100101
ls = request.getfixturevalue('launch_service')
101102
event_loop = request.getfixturevalue('event_loop')
@@ -114,14 +115,14 @@ def pytest_fixture_setup(fixturedef, request):
114115
ls.include_launch_description(ld)
115116
run_async_task = event_loop.create_task(ls.run_async(
116117
# TODO(ivanpauno): maybe this could be configurable (?)
117-
shutdown_when_idle=True
118+
shutdown_when_idle=options['shutdown_when_idle']
118119
))
119120
ready = get_ready_to_test_action(ld)
120121
asyncio.set_event_loop(event_loop)
121122
event = asyncio.Event()
122123
ready._add_callback(lambda: event.set())
123-
124-
fixturedef.addfinalizer(functools.partial(finalize_launch_service, ls, eprefix=eprefix))
124+
fixturedef.addfinalizer(functools.partial(
125+
finalize_launch_service, ls, eprefix=eprefix, auto_shutdown=options['auto_shutdown']))
125126
run_until_complete(event_loop, event.wait())
126127
# this is guaranteed by the current run_async() implementation, let's check it just in case
127128
# it changes in the future
@@ -343,8 +344,12 @@ def pytest_pyfunc_call(pyfuncitem):
343344
scope = fixture._pytestfixturefunction.scope
344345
event_loop = pyfuncitem.funcargs['event_loop']
345346
ls = pyfuncitem.funcargs['launch_service']
347+
auto_shutdown = fixture._launch_pytest_fixture_options['auto_shutdown']
346348
on_shutdown = functools.partial(
347-
finalize_launch_service, ls, eprefix=f'When running test {func.__name__}')
349+
finalize_launch_service,
350+
ls,
351+
eprefix=f'When running test {func.__name__}',
352+
auto_shutdown=auto_shutdown)
348353
before_test = on_shutdown if shutdown_test else None
349354
if inspect.iscoroutinefunction(func):
350355
pyfuncitem.obj = wrap_coroutine(func, event_loop, before_test)

0 commit comments

Comments
 (0)