Beispiel #1
0
def test_launch_service_emit_event():
    """
    Test the emitting of events in the LaunchService class.

    Also covers basic tests for include_launch_description(), run(), and
    shutdown().
    """
    ls = LaunchService(debug=True)

    assert ls._LaunchService__context._event_queue.qsize() == 0

    from launch.actions import OpaqueFunction
    from launch.actions import RegisterEventHandler
    from launch.event_handler import EventHandler

    handled_events = queue.Queue()
    ld = LaunchDescription([
        RegisterEventHandler(EventHandler(
            matcher=lambda event: True,
            entities=OpaqueFunction(
                function=lambda context: handled_events.put(context.locals.event),
            ),
        ))
    ])
    ls.include_launch_description(ld)
    assert ls._LaunchService__context._event_queue.qsize() == 1

    class MockEvent:
        name = 'Event'

    ls.emit_event(MockEvent())
    assert ls._LaunchService__context._event_queue.qsize() == 2
    assert handled_events.qsize() == 0

    t = threading.Thread(target=ls.run, kwargs={'shutdown_when_idle': False})
    t.start()

    # First event (after including description of event handler).
    handled_events.get(block=True, timeout=5.0)

    # Emit and then check for a second event.
    ls.emit_event(MockEvent())
    handled_events.get(block=True, timeout=5.0)

    # Shutdown (generates a third event) and join the thread.
    ls.shutdown()
    t.join()
    # Check that the shutdown event was handled.
    handled_events.get(block=False)

    assert handled_events.qsize() == 0
    ls.emit_event(MockEvent())
    assert handled_events.qsize() == 0

    assert ls.run(shutdown_when_idle=True) == 0
    handled_events.get(block=False)
Beispiel #2
0
def test_launch_service_emit_event():
    """
    Test the emitting of events in the LaunchService class.

    Also covers basic tests for include_launch_description(), run(), and
    shutdown().
    """
    ls = LaunchService(debug=True)

    assert ls._LaunchService__context._event_queue.qsize() == 0

    from launch.actions import OpaqueFunction
    from launch.actions import RegisterEventHandler
    from launch.event_handler import EventHandler

    handled_events = queue.Queue()
    ld = LaunchDescription([
        RegisterEventHandler(
            EventHandler(
                matcher=lambda event: not isinstance(event, ExecutionComplete),
                entities=OpaqueFunction(function=lambda context: handled_events
                                        .put(context.locals.event), ),
            ))
    ])
    ls.include_launch_description(ld)
    assert ls._LaunchService__context._event_queue.qsize() == 1

    class MockEvent:
        name = 'Event'

    ls.emit_event(MockEvent())
    assert ls._LaunchService__context._event_queue.qsize() == 2
    assert handled_events.qsize() == 0

    # Spin up a background thread for testing purposes.
    def perform_test_sequence():
        # First event (after including description of event handler).
        handled_events.get(block=True, timeout=5.0)
        # Emit and then check for a second event.
        ls.emit_event(MockEvent())
        handled_events.get(block=True, timeout=5.0)
        # Shutdown (generates a third event).
        ls.shutdown()

    t = threading.Thread(target=perform_test_sequence)
    t.start()

    # Run the launch service.
    assert ls.run(shutdown_when_idle=False) == 0

    # Join background thread if still running.
    t.join()

    # Check that the shutdown event was handled.
    handled_events.get(block=False)

    assert handled_events.qsize() == 0
    ls.emit_event(MockEvent())
    assert handled_events.qsize() == 0

    assert ls.run(shutdown_when_idle=True) == 0
    # Check that the mock and shutdown events were handled.
    assert handled_events.qsize() == 2
    handled_events.get(block=False)
    handled_events.get(block=False)

    loop = osrf_pycommon.process_utils.get_loop()
    assert loop.run_until_complete(ls.run_async(shutdown_when_idle=True)) == 0
    # Check that the shutdown events was handled.
    assert handled_events.qsize() == 1
    handled_events.get(block=False)