Skip to content
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

Fixes timer global init of event handler #184

Merged
merged 4 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 3 additions & 5 deletions launch/launch/actions/timer_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from ..utilities import normalize_to_list_of_substitutions
from ..utilities import perform_substitutions

_event_handler_has_been_installed = False
_logger = logging.getLogger('launch.timer_action')


Expand Down Expand Up @@ -135,9 +134,8 @@ def execute(self, context: LaunchContext) -> Optional[List['Action']]:
self.__completed_future.set_result(None)
return None

# Once globally, install the general purpose OnTimerEvent event handler.
global _event_handler_has_been_installed
if not _event_handler_has_been_installed:
# Once per context, install the general purpose OnTimerEvent event handler.
if not hasattr(context, "_TimerAction__event_handler_has_been_installed"):
from ..actions import OpaqueFunction
context.register_event_handler(EventHandler(
matcher=lambda event: is_a_subclass(event, TimerEvent),
Expand All @@ -147,7 +145,7 @@ def execute(self, context: LaunchContext) -> Optional[List['Action']]:
)
),
))
_event_handler_has_been_installed = True
setattr(context, "_TimerAction__event_handler_has_been_installed", True)
wjwwood marked this conversation as resolved.
Show resolved Hide resolved

# Capture the current context locals so the yielded actions can make use of them too.
self.__context_locals = dict(context.get_locals_as_dict()) # Capture a copy
Expand Down
41 changes: 41 additions & 0 deletions launch/test/launch/test_timer_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2019 Apex.AI, 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 launch
import launch.actions

def test_multiple_launch_with_timers():
# Regression test for https://github.com/ros2/launch/issues/183
# Unfortunately, when things aren't working this test just hangs on the second call to
# ls.run

def generate_launch_description():
return launch.LaunchDescription([
launch.actions.TimerAction(
period="1",
actions=[
launch.actions.Shutdown(reason="Timer expired")
]
)
])

ls = launch.LaunchService()
ls.include_launch_description(generate_launch_description())
assert 0 == ls.run(shutdown_when_idle=False) # Always works

ls = launch.LaunchService()
ls.include_launch_description(generate_launch_description())
# Next line hangs forever before https://github.com/ros2/launch/issues/183 was fixed.
assert 0 == ls.run(shutdown_when_idle=False)