def test_dispatch_with_return_fails(self): dispatcher = EventDispatcher() test_callback_prio_0_cb_0 = Mock(return_value='hello') test_callback_prio_0_cb_1 = Mock(side_effect=RuntimeError('boom!')) test_callback_prio_1_cb_0 = Mock(return_value='world') dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_0) dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_1) dispatcher.add_listener(TestEvent, 1, test_callback_prio_1_cb_0) event = TestEvent() d = dispatcher.dispatch(event, fail_mode=FailMode.RETURN) matcher = matchers.MatchesListwise([ matchers.MatchesListwise([ matchers.Equals(0), matchers.MatchesListwise([ matchers.Equals((True, 'hello')), matchers.MatchesListwise([ matchers.Equals(False), matchers.AfterPreprocessing(lambda f: f.value, matchers.IsInstance(HandlerError)), ]) ]), ]), matchers.MatchesListwise([ matchers.Equals(1), matchers.MatchesListwise([ matchers.Equals((True, 'world')), ]), ]), ]) assert_that(d, twistedsupport.succeeded(matcher))
def test_dispatch_with_args_kwds(self): dispatcher = EventDispatcher() callback = Mock() dispatcher.add_listener(TestEvent, 0, callback, 'hello', 'world', some='kwds', also='here') event = TestEvent() d = dispatcher.dispatch(event) self.assertTrue(d.called) callback.assert_called_once_with(event, 'hello', 'world', some='kwds', also='here')
def test_dispatch_event(self): dispatcher = EventDispatcher() test_callback_prio_0_cb_0 = Mock() test_callback_prio_0_cb_1 = Mock() test_callback_prio_1_cb_0 = Mock() other_callback_prio_2_cb_0 = Mock() dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_0) key_prio_0_cb_1 = dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_1) dispatcher.add_listener(TestEvent, 1, test_callback_prio_1_cb_0) dispatcher.add_listener(OtherEvent, 2, other_callback_prio_2_cb_0) event = TestEvent() d = dispatcher.dispatch(event) self.assertTrue(d.called) test_callback_prio_0_cb_0.assert_called_once_with(event) test_callback_prio_0_cb_1.assert_called_once_with(event) test_callback_prio_1_cb_0.assert_called_once_with(event) self.assertEqual(other_callback_prio_2_cb_0.call_count, 0) dispatcher.remove_listener(TestEvent, key_prio_0_cb_1) test_callback_prio_0_cb_0.reset_mock() test_callback_prio_0_cb_1.reset_mock() test_callback_prio_1_cb_0.reset_mock() other_callback_prio_2_cb_0.reset_mock() event = TestEvent() d = dispatcher.dispatch(event) self.assertTrue(d.called) test_callback_prio_0_cb_0.assert_called_once_with(event) self.assertEqual(test_callback_prio_0_cb_1.call_count, 0) test_callback_prio_1_cb_0.assert_called_once_with(event) self.assertEqual(other_callback_prio_2_cb_0.call_count, 0)
def test_dispatch_with_fails(self): dispatcher = EventDispatcher() test_callback_prio_0_cb_0 = Mock() test_callback_prio_0_cb_1 = Mock(side_effect=RuntimeError('boom!')) test_callback_prio_1_cb_0 = Mock() dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_0) dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_1) dispatcher.add_listener(TestEvent, 1, test_callback_prio_1_cb_0) event = TestEvent() d = dispatcher.dispatch(event) matcher = matchers.AfterPreprocessing(lambda f: f.value, matchers.IsInstance(HandlerError)) assert_that(d, twistedsupport.failed(matcher))