示例#1
0
    def test_dispatch(self):
        events = list(range(3))
        handler = mock.Mock()
        dispatcher = h_dis.Dispatcher()
        dispatcher.register(lambda e: True, True, handler)

        for event in events:
            dispatcher(event)

        handler.assert_has_calls([mock.call(e) for e in events])
示例#2
0
    def test_dispatch_broadcast(self):
        handlers = [mock.Mock() for _ in range(3)]
        dispatcher = h_dis.Dispatcher()

        for handler in handlers:
            dispatcher.register(lambda e: True, True, handler)

        dispatcher(mock.sentinel.event)

        for handler in handlers:
            handler.assert_called_once_with(mock.sentinel.event)
示例#3
0
    def test_dispatch_by_key(self):
        def key_fn(event):
            return str(event)

        events = {key_fn(i): i for i in range(3)}
        handlers = {key: mock.Mock() for key in events}
        dispatcher = h_dis.Dispatcher()
        for key, handler in handlers.items():
            dispatcher.register(key_fn, key, handler)

        for event in events.values():
            dispatcher(event)

        for key, handler in handlers.items():
            handler.assert_called_once_with(events[key])
    def test_dispatch_by_key(self):
        def key_fn(event):
            return event['object']['metadata']['name']

        events = {}
        for i in range(3):
            e = make_event(i)
            events[key_fn(e)] = e
        handlers = {key: mock.Mock() for key in events}
        dispatcher = h_dis.Dispatcher()
        for key, handler in handlers.items():
            dispatcher.register(key_fn, key, handler)

        for event in events.values():
            dispatcher(event)

        for key, handler in handlers.items():
            handler.assert_called_once_with(events[key])