How to use the cloudbridge.cloud.base.events.SimpleEventDispatcher function in cloudbridge

To help you get started, we’ve selected a few cloudbridge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CloudVE / cloudbridge / test / test_event_system.py View on Github external
self.assertEqual(event_args.get('sender'), self)
            next_handler = event_args.get('next_handler')
            self.assertEqual(next_handler.priority, 2020)
            self.assertEqual(next_handler.callback.__name__,
                             "my_callback_impl")
            callback_tracker[0] += "intcpt_"
            # invoke next handler but ignore return value
            return "hello" + next_handler.invoke(event_args, *args, **kwargs)

        def my_callback_impl(*args, **kwargs):
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "impl_"
            return "world"

        dispatcher = SimpleEventDispatcher()
        dispatcher.intercept(EVENT_NAME, 2000, my_callback_intcpt)
        dispatcher.implement(EVENT_NAME, 2020, my_callback_impl)
        result = dispatcher.dispatch(self, EVENT_NAME, 'first_pos_arg',
                                     a_keyword_arg='another_thing')
        self.assertEqual(
            callback_tracker[0], "intcpt_impl_", "callback was not invoked"
            " in expected order. Should have been intcpt_impl_ but is"
            " {0}".format(callback_tracker[0]))
        self.assertEqual(result, "helloworld", "Result should be `helloworld` "
                         "as this is the expected return value from the chain")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
{'sender': self,
                                  'event': EVENT_NAME,
                                  'result': 'world',
                                  'next_handler': None})
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "intcpt_"
            return "hello"

        def my_callback_impl(*args, **kwargs):
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "impl_"
            return "world"

        dispatcher = SimpleEventDispatcher()
        dispatcher.implement(EVENT_NAME, 2000, my_callback_impl)
        dispatcher.intercept(EVENT_NAME, 2020, my_callback_intcpt)
        result = dispatcher.dispatch(self, EVENT_NAME, 'first_pos_arg',
                                     a_keyword_arg='another_thing')
        self.assertEqual(
            callback_tracker[0], "impl_intcpt_", "callback was not invoked"
            " in expected order. Should have been intcpt_intcpt_ but is"
            " {0}".format(callback_tracker[0]))
        self.assertEqual(result, "world", "Result should be `world` "
                         "as this is the expected return value from the chain")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
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"
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
def test_subscribe_event_duplicate_wildcard_priority_allowed(self):
        # duplicate priorities for different wildcard namespaces allowed
        def my_callback(event_args, *args, **kwargs):
            pass

        dispatcher = SimpleEventDispatcher()
        dispatcher.intercept("event.hello.world", 1000, my_callback)
        dispatcher.intercept("someevent.hello.*", 1000, my_callback)
        # emit should work fine in this case with no exceptions
        dispatcher.dispatch(self, "event.hello.world")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
def test_emit_event_intercepting_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,
                                  '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()
        handler = dispatcher.intercept(event_pattern=EVENT_NAME, priority=1000,
                                       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], "intcpt", "callback should have been invoked"
            "once and contain value `intcpt` but tracker value is {0}".format(
                callback_tracker[0]))
        self.assertEqual(result, "world", "Result should be `world` as this"
                         " is an intercepting handler")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
callback_tracker = ['']

        def my_callback1(event_args, *args, **kwargs):
            callback_tracker[0] += "event1_"
            next_handler = event_args.get('next_handler')
            if next_handler:
                return "hello" + next_handler.invoke(
                    event_args, *args, **kwargs)
            else:
                return "hello"

        def my_callback2(*args, **kwargs):
            callback_tracker[0] += "event2_"
            return "some"

        dispatcher = SimpleEventDispatcher()
        dispatcher.intercept("event.hello.world", 1000, my_callback1)
        dispatcher.dispatch(self, "event.hello.world")
        dispatcher.intercept("event.hello.*", 1001, my_callback2)
        result = dispatcher.dispatch(self, "event.hello.world")

        self.assertEqual(
            callback_tracker[0], "event1_event1_event2_",
            "Event handlers executed in unexpected order {0}".format(
                callback_tracker[0]))
        self.assertEqual(result, "hellosome")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
def test_emit_event_implementing_handler(self):
        EVENT_NAME = "event.hello.world"
        callback_tracker = ['']

        def my_callback(*args, **kwargs):
            self.assertSequenceEqual(args, ['first_pos_arg'])
            self.assertDictEqual(kwargs, {'a_keyword_arg': 'another_thing'})
            callback_tracker[0] += "impl"
            return "world"

        dispatcher = SimpleEventDispatcher()
        handler = dispatcher.implement(event_pattern=EVENT_NAME, priority=1000,
                                       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], "impl", "callback should have been invoked"
            "once and contain value `intcpt` but tracker value is {0}".format(
                callback_tracker[0]))
        self.assertEqual(result, "world", "Result should be `world` as this"
                         " is an implementing handler")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
callback_tracker = ['']

        def my_callback1(event_args, *args, **kwargs):
            callback_tracker[0] += "event1_"
            next_handler = event_args.get('next_handler')
            if next_handler:
                return "hello" + next_handler.invoke(
                    event_args, *args, **kwargs)
            else:
                return "hello"

        def my_callback2(*args, **kwargs):
            callback_tracker[0] += "event2_"
            return "some"

        dispatcher = SimpleEventDispatcher()
        hndlr1 = dispatcher.intercept(EVENT_NAME, 1000, my_callback1)
        dispatcher.dispatch(self, EVENT_NAME)
        hndlr2 = dispatcher.intercept("event.hello.*", 1001, my_callback2)
        # Both handlers should be registered
        self.assertListEqual(
            [my_callback1, my_callback2],
            [handler.callback for handler in
             dispatcher.get_handlers_for_event(EVENT_NAME)])
        hndlr1.unsubscribe()

        # Only my_callback2 should be registered after unsubscribe
        self.assertListEqual(
            [my_callback2],
            [handler.callback for handler in
             dispatcher.get_handlers_for_event(EVENT_NAME)])
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
def test_subscribe_event_duplicate_wildcard_priority(self):

        def my_callback(event_args, *args, **kwargs):
            pass

        dispatcher = SimpleEventDispatcher()
        dispatcher.intercept("event.hello.world", 1000, my_callback)
        dispatcher.intercept("event.hello.*", 1000, my_callback)
        with self.assertRaises(HandlerException):
            dispatcher.dispatch(self, "event.hello.world")
github CloudVE / cloudbridge / test / test_event_system.py View on Github external
self.assertEqual(next_handler.priority, 2020)
            self.assertEqual(next_handler.callback.__name__,
                             "my_callback_intcpt2")
            callback_tracker[0] += "intcpt1_"
            # invoke next handler but ignore return value
            return "hello" + next_handler.invoke(event_args, *args, **kwargs)

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

        dispatcher = SimpleEventDispatcher()
        dispatcher.intercept(EVENT_NAME, 2000, my_callback_intcpt1)
        dispatcher.intercept(EVENT_NAME, 2020, my_callback_intcpt2)
        result = dispatcher.dispatch(self, EVENT_NAME)
        self.assertEqual(
            callback_tracker[0], "intcpt1_intcpt2_", "callback was not invoked"
            " in expected order. Should have been intcpt1_intcpt2_ but is"
            " {0}".format(callback_tracker[0]))
        self.assertEqual(result, "helloworld", "Result should be `helloworld` "
                         "as this is the expected return value from the chain")