-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support LaunchService injection into pre-shutdown tests. #308
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,4 @@ if __name__ == "__main__": | |
|
||
print("Shutting Down") | ||
|
||
sys.exit(0) | ||
sys.exit(0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright 2019 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import sys | ||
import unittest | ||
|
||
import ament_index_python | ||
|
||
import launch | ||
import launch.actions | ||
|
||
import launch_testing | ||
import launch_testing.asserts | ||
import launch_testing.tools | ||
|
||
|
||
def get_test_process_action(*, args=[]): | ||
test_proc_path = os.path.join( | ||
ament_index_python.get_package_prefix('launch_testing'), | ||
'lib/launch_testing', | ||
'terminating_proc' | ||
) | ||
return launch.actions.ExecuteProcess( | ||
cmd=[sys.executable, test_proc_path, *args], | ||
name='terminating_proc', | ||
# This is necessary to get unbuffered output from the process under test | ||
additional_env={'PYTHONUNBUFFERED': '1'}, | ||
) | ||
|
||
|
||
def generate_test_description(ready_fn): | ||
return launch.LaunchDescription([ | ||
launch_testing.util.KeepAliveProc(), | ||
launch.actions.OpaqueFunction(function=lambda context: ready_fn()), | ||
]) | ||
|
||
|
||
class TestTerminatingProc(unittest.TestCase): | ||
|
||
def test_no_args(self, launch_service, proc_output, proc_info): | ||
"""Test terminating_proc without command line arguments.""" | ||
proc_action = get_test_process_action() | ||
with launch_testing.tools.launch_process(launch_service, proc_action) as dut: | ||
proc_info.assertWaitForStartup(process=dut, timeout=2) | ||
proc_output.assertWaitFor('Starting Up', process=dut, timeout=2) | ||
proc_output.assertWaitFor('Emulating Work', process=dut, timeout=2) | ||
proc_output.assertWaitFor('Done', process=dut, timeout=2) | ||
proc_output.assertWaitFor('Shutting Down', process=dut, timeout=2) | ||
proc_info.assertWaitForShutdown(process=dut, timeout=2) | ||
launch_testing.asserts.assertExitCodes(proc_info, process=dut) | ||
|
||
def test_with_args(self, launch_service, proc_output, proc_info): | ||
"""Test terminating_proc with some command line arguments.""" | ||
proc_action = get_test_process_action(args=['--foo', 'bar']) | ||
with launch_testing.tools.launch_process(launch_service, proc_action) as dut: | ||
proc_info.assertWaitForStartup(process=dut, timeout=2) | ||
proc_output.assertWaitFor('Starting Up', process=dut, timeout=2) | ||
proc_output.assertWaitFor( | ||
"Called with arguments ['--foo', 'bar']", process=dut, timeout=2 | ||
) | ||
proc_output.assertWaitFor('Emulating Work', process=dut, timeout=2) | ||
proc_output.assertWaitFor('Done', process=dut, timeout=2) | ||
proc_output.assertWaitFor('Shutting Down', process=dut, timeout=2) | ||
proc_info.assertWaitForShutdown(process=dut, timeout=2) | ||
launch_testing.asserts.assertExitCodes(proc_info, process=dut) | ||
|
||
def test_unhandled_exception(self, launch_service, proc_output, proc_info): | ||
"""Test terminating_proc forcing an unhandled exception.""" | ||
proc_action = get_test_process_action(args=['--exception']) | ||
with launch_testing.tools.launch_process(launch_service, proc_action) as dut: | ||
proc_info.assertWaitForStartup(process=dut, timeout=2) | ||
proc_output.assertWaitFor('Starting Up', process=dut, timeout=2) | ||
proc_output.assertWaitFor( | ||
"Called with arguments ['--exception']", process=dut, timeout=2 | ||
) | ||
proc_info.assertWaitForShutdown(process=dut, timeout=2) | ||
launch_testing.asserts.assertExitCodes( | ||
proc_info, process=dut, allowable_exit_codes=[1] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ def run(self): | |
self._test_run.bind( | ||
self._test_run.pre_shutdown_tests, | ||
injected_attributes={ | ||
'launch_service': self._launch_service, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hidmic Not directly related to this PR, but do you have any guidance for a process for deprecating 'injected_attributes' from launch_testing? Something like 'put a warning in the next release when someone uses an injected attribute' then in the release after that remove injected attributes? I think injected_args are better and less confusing in just about every way. They still require the user to know ahead of time what 'magic words' can be added as arguments to a test function, but beyond that they're way less mysterious There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, you could wrap them using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hidmic Yeah - I know how to do the implementation side - my question is more about the administrative side. Do we need a full release of "WARNING THIS WILL BE DEPRECATED" or is it sufficient to add the warning, them actually deprecate once all of OSRF's stuff is updated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, forgot to reply. And sorry for the noise, I clearly half read what you wrote before. I actually don't know what's the deprecation cycle for Python APIs here. What you describe makes sense to me. We'd be looking forward to issuing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a deprecation cycle is technically feasible it is certainly preferred to give users time to migrate. |
||
'proc_info': proc_info, | ||
'proc_output': proc_output, | ||
'test_args': test_args, | ||
|
@@ -95,6 +96,7 @@ def run(self): | |
full_context, | ||
# Add a few more things to the args dictionary: | ||
**{ | ||
'launch_service': self._launch_service, | ||
'proc_info': proc_info, | ||
'proc_output': proc_output, | ||
'test_args': test_args | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2019 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import contextlib | ||
|
||
import launch | ||
import launch.events | ||
|
||
|
||
@contextlib.contextmanager | ||
def launch_process(launch_service, process_action): | ||
""" | ||
Launch a process. | ||
|
||
Start execution of a ``process_action`` using the given | ||
``launch_service`` upon context entering and shut it down | ||
upon context exiting. | ||
""" | ||
assert isinstance(process_action, launch.actions.ExecuteProcess) | ||
launch_service.emit_event( | ||
event=launch.events.IncludeLaunchDescription( | ||
launch_description=launch.LaunchDescription([process_action]) | ||
) | ||
) | ||
try: | ||
yield process_action | ||
finally: | ||
launch_service.emit_event( | ||
event=launch.events.process.ShutdownProcess( | ||
process_matcher=launch.events.matches_action(process_action) | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling.
Also that
this
is used twice but references different entities is a bit hard to read.