Beispiel #1
0
def test_remove_listener():
    dispatcher = EventDispatcher()

    for event_id in (
            STRING_EVENT_ID,
            OBJECT_EVENT_ID,
            NUMERIC_EVENT_ID,
    ):
        listener = mock.MagicMock()
        assert not dispatcher.has_listeners(event_id)
        dispatcher.add_listener(event_id, listener)
        assert dispatcher.has_listeners(event_id)

        assert listener.call_count == 0

        e = dispatcher.dispatch(event_id)

        assert listener.call_count == 1
        assert not e.propagation_stopped

        assert dispatcher.has_listeners(event_id)
        dispatcher.remove_listener(event_id, listener)
        assert not dispatcher.has_listeners(event_id)

        e = dispatcher.dispatch(event_id)

        assert listener.call_count == 1
        assert not e.propagation_stopped
Beispiel #2
0
def test_dispatcher_custom_event(EventType, event_id):
    dispatcher = EventDispatcher()
    listener = mock.MagicMock()
    event = EventType()

    dispatcher.add_listener(event_id, listener)
    e = dispatcher.dispatch(event_id, event)

    assert listener.call_count == 1
    assert e == event
Beispiel #3
0
def test_propagation():
    dispatcher = EventDispatcher()

    for event_id in (
            STRING_EVENT_ID,
            OBJECT_EVENT_ID,
            NUMERIC_EVENT_ID,
    ):
        listener1 = mock.MagicMock()

        def listener2(event):
            event.stop_propagation()

        listener3 = mock.MagicMock()

        dispatcher.add_listener(event_id, listener1)
        dispatcher.add_listener(event_id, listener2)
        dispatcher.add_listener(event_id, listener3)

        assert listener1.call_count == 0
        assert listener3.call_count == 0

        e = dispatcher.dispatch(event_id)

        assert listener1.call_count == 1
        assert listener3.call_count == 0
        assert e.propagation_stopped

        e = dispatcher.dispatch(event_id)

        assert listener1.call_count == 2
        assert listener3.call_count == 0
        assert e.propagation_stopped

        assert dispatcher.get_listeners(event_id) == [
            listener1, listener2, listener3
        ]

    listeners = dispatcher.get_listeners()
    assert len(listeners) == 3
    for event_id in (
            STRING_EVENT_ID,
            OBJECT_EVENT_ID,
            NUMERIC_EVENT_ID,
    ):
        assert len(listeners[event_id]) == 3
Beispiel #4
0
def test_dispatcher(event_id):
    dispatcher = EventDispatcher()

    listener = mock.MagicMock()
    assert not dispatcher.has_listeners(event_id)
    dispatcher.add_listener(event_id, listener)
    assert dispatcher.has_listeners(event_id)

    assert listener.call_count == 0

    e = dispatcher.dispatch(event_id)

    assert listener.call_count == 1
    assert not e.propagation_stopped

    e = dispatcher.dispatch(event_id)

    assert listener.call_count == 2
    assert not e.propagation_stopped
Beispiel #5
0
    def __init__(self, dispatcher: EventDispatcher):
        self._configs = {}
        self._features = {}
        self._pipelines = {}
        self._variables = OrderedDict()

        self.dispatcher = dispatcher
        self.resources = OrderedDict()

        def register_feature(ext):
            self._features[ext.name] = ext.plugin

        def on_load_feature_failure(mgr, entrypoint, err):
            logger.exception(
                "Exception caught while loading {}.".format(entrypoint), err)

        mgr = ExtensionManager(
            namespace="medikit.feature",
            on_load_failure_callback=on_load_feature_failure)
        mgr.map(register_feature)

        dispatcher.add_listener(medikit.on_end, self.write_resources)