예제 #1
0
    def test_subscribe_multiple_events(self):
        EVENT_NAME = "event.hello.world"
        callback_tracker = ['']

        def my_callback1(event_args, *args, **kwargs):
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': EVENT_NAME
            })
            callback_tracker[0] += "event1_"
            return "hello"

        def my_callback2(event_args, *args, **kwargs):
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': "event.hello.anotherworld"
            })
            callback_tracker[0] += "event2_"
            return "another"

        def my_callback3(event_args, *args, **kwargs):
            self.assertDictEqual(
                event_args, {
                    'sender': self,
                    'event': "event.hello.anotherworld",
                    'next_handler': None
                })
            callback_tracker[0] += "event3_"
            return "world"

        dispatcher = SimpleEventDispatcher()
        dispatcher.observe(EVENT_NAME, 2000, my_callback1)
        # register to a different event with the same priority
        dispatcher.observe("event.hello.anotherworld", 2000, my_callback2)
        dispatcher.intercept("event.hello.anotherworld", 2020, my_callback3)
        result = dispatcher.dispatch(self, EVENT_NAME)
        self.assertEqual(
            callback_tracker[0], "event1_", "only `event.hello.world` handlers"
            " should have been  triggered but received {0}".format(
                callback_tracker[0]))
        self.assertEqual(
            result, None, "Result should be `helloworld` "
            "as this is the expected return value from the chain")

        result = dispatcher.dispatch(self, "event.hello.anotherworld")
        self.assertEqual(
            callback_tracker[0], "event1_event2_event3_", "only handlers for"
            "  event `event.hello.anotherworld` should have been  triggered"
            " but received {0}".format(callback_tracker[0]))
        self.assertEqual(
            result, "world", "Result should be `world` "
            "as this is the expected return value from the chain")
예제 #2
0
    def test_emit_event_observe_follows_intercept(self):
        EVENT_NAME = "event.hello.world"
        callback_tracker = ['']

        def my_callback_intcpt(event_args, *args, **kwargs):
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            self.assertEqual(event_args.get('sender'), self)
            self.assertEqual(event_args.get('next_handler').priority, 1001)
            self.assertEqual(
                event_args.get('next_handler').callback.__name__,
                "my_callback_obs")
            callback_tracker[0] += "intcpt_"
            # invoke next handler
            next_handler = event_args.get('next_handler')
            assert next_handler.priority == 1001
            assert next_handler.event_pattern == EVENT_NAME
            assert next_handler.callback == my_callback_obs
            retval = next_handler.invoke(event_args, *args, **kwargs)
            self.assertIsNone(
                retval, "Return values of observable handlers"
                " should not be propagated.")
            return "world"

        def my_callback_obs(event_args, *args, **kwargs):
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': EVENT_NAME
            })
            callback_tracker[0] += "obs_"
            return "hello"

        dispatcher = SimpleEventDispatcher()
        # register priorities out of order to test that too
        dispatcher.observe(EVENT_NAME, 1001, my_callback_obs)
        dispatcher.intercept(EVENT_NAME, 1000, my_callback_intcpt)
        result = dispatcher.dispatch(self,
                                     EVENT_NAME,
                                     'first_pos_arg',
                                     a_keyword_arg='another_thing')
        self.assertEqual(
            callback_tracker[0], "intcpt_obs_", "callback was not invoked in "
            "expected order. Should have been intcpt_obs_ but is {0}".format(
                callback_tracker[0]))
        self.assertEqual(
            result, "world", "Result should be `world` as this"
            " is the return value of the intercepting handler")
예제 #3
0
    def test_emit_event_observing_handler(self):
        EVENT_NAME = "event.hello.world"
        callback_tracker = ['']

        def my_callback(event_args, *args, **kwargs):
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': EVENT_NAME
            })
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += 'obs'
            return "hello"

        dispatcher = SimpleEventDispatcher()
        handler = dispatcher.observe(event_pattern=EVENT_NAME,
                                     priority=1000,
                                     callback=my_callback)
        assert handler.event_pattern == EVENT_NAME
        assert handler.priority == 1000
        assert handler.callback == my_callback
        self.assertIsInstance(handler, EventHandler)
        result = dispatcher.dispatch(self,
                                     EVENT_NAME,
                                     'first_pos_arg',
                                     a_keyword_arg='another_thing')
        self.assertEqual(
            callback_tracker[0], "obs", "callback should have been invoked"
            "once and contain value `obs` but tracker value is {0}".format(
                callback_tracker[0]))
        self.assertIsNone(
            result, "Result should be none as this is an"
            " observing handler")
예제 #4
0
    def test_emit_event_observe_precedes_intercept(self):
        EVENT_NAME = "event.hello.world"
        callback_tracker = ['']

        def my_callback_obs(event_args, *args, **kwargs):
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': EVENT_NAME
            })
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "obs_"
            return "hello"

        def my_callback_intcpt(event_args, *args, **kwargs):
            self.assertDictEqual(event_args, {
                'sender': self,
                'event': EVENT_NAME,
                'next_handler': None
            })
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "intcpt_"
            return "world"

        dispatcher = SimpleEventDispatcher()
        dispatcher.observe(EVENT_NAME, 1000, my_callback_obs)
        dispatcher.intercept(EVENT_NAME, 1001, my_callback_intcpt)
        result = dispatcher.dispatch(self,
                                     EVENT_NAME,
                                     'first_pos_arg',
                                     a_keyword_arg='another_thing')
        self.assertEqual(
            callback_tracker[0], "obs_intcpt_", "callback was not invoked in "
            "expected order. Should have been obs_intcpt_ but is {0}".format(
                callback_tracker[0]))
        self.assertEqual(
            result, "world", "Result should be `world` as this"
            " is the return value of the intercepting handler")