Example #1
0
def test_handle_once():
    context = Mock(spec=LaunchContext)
    unregister_event_handler_mock = context.unregister_event_handler

    handler = OnProcessStart(on_start=Mock(), handle_once=True)
    handler.handle(phony_process_started, context)
    unregister_event_handler_mock.assert_called_once_with(handler)
Example #2
0
def test_event_added_to_context():
    context = Mock(spec=LaunchContext)
    extend_locals_mock = context.extend_locals
    unregister_event_handler_mock = context.unregister_event_handler

    handler = OnProcessStart(on_start=Mock())
    handler.handle(phony_process_started, context)
    extend_locals_mock.assert_called_once_with(
        {'event': phony_process_started})
    unregister_event_handler_mock.assert_not_called()
Example #3
0
def test_matches_callable():
    target_action = Mock(spec=ExecuteProcess)
    handler = OnProcessStart(target_action=target_action, on_start=Mock())
    assert handler.matches(
        ProcessStarted(action=target_action,
                       name='foo',
                       cmd=['ls'],
                       cwd=None,
                       env=None,
                       pid=3))
    assert handler.matches(phony_process_started)
    assert not handler.matches(phony_process_exited)
Example #4
0
    def execute(self, context: LaunchContext) -> Optional[List[Action]]:
        """
        Execute the action.

        Most work is delegated to :meth:`launch_ros.actions.Node.execute`, except for the
        composable nodes load action if it applies.
        """
        load_actions = None  # type: Optional[List[Action]]
        if self.__composable_node_descriptions is not None:
            from .load_composable_nodes import LoadComposableNodes
            # Perform load action once the container has started.
            load_actions = [
                RegisterEventHandler(
                    event_handler=OnProcessStart(
                        target_action=self,
                        on_start=[
                            LoadComposableNodes(
                                composable_node_descriptions=self.__composable_node_descriptions,
                                target_container=self
                            )
                        ]
                    )
                )
            ]
        container_actions = super().execute(context)  # type: Optional[List[Action]]
        if container_actions is not None and load_actions is not None:
            return container_actions + load_actions
        if container_actions is not None:
            return container_actions
        if load_actions is not None:
            return load_actions
        return None
Example #5
0
 def generate_launch_description():
     process_action = ExecuteProcess(
         cmd=[sys.executable, '-c', 'import signal; signal.pause()'],
         sigterm_timeout='1',  # shorten timeouts
         on_exit=on_exit)
     # Launch process and emit shutdown event as if
     # launch had received a SIGINT
     return LaunchDescription([
         process_action,
         RegisterEventHandler(event_handler=OnProcessStart(
             target_action=process_action,
             on_start=[
                 EmitEvent(
                     event=ShutdownEvent(reason='none', due_to_sigint=True))
             ]))
     ])
Example #6
0
def test_handle_action():
    mock_action = NonCallableMock(spec=Action)
    handler = OnProcessStart(on_start=mock_action)
    assert [mock_action] == handler.handle(phony_process_started,
                                           phony_context)
Example #7
0
def test_handle_callable():
    mock_callable = Mock()
    handler = OnProcessStart(on_start=mock_callable)
    handler.handle(phony_process_started, phony_context)
    mock_callable.assert_called_once_with(phony_process_started, phony_context)
Example #8
0
def test_matches_process_started():
    handler = OnProcessStart(on_start=Mock())
    assert handler.matches(phony_process_started)
    assert not handler.matches(phony_process_exited)
Example #9
0
def test_non_action_on_start():
    with pytest.raises(TypeError):
        OnProcessStart(target_action=NonCallableMock(),
                       on_start=NonCallableMock())
Example #10
0
def test_non_execute_process_target():
    with pytest.raises(TypeError):
        OnProcessStart(target_action=NonCallableMock(),
                       on_start=NonCallableMock(spec=Action))
Example #11
0
def test_handle_list_of_actions():
    mock_actions = [NonCallableMock(spec=Action), NonCallableMock(spec=Action)]
    handler = OnProcessStart(on_start=mock_actions)
    assert mock_actions == handler.handle(phony_process_started, phony_context)