Ejemplo n.º 1
0
    def test_dispatch_child_dupe(self):
        # ensure that a child handler cannot redeliver an event to a sibling
        # child object
        parent = event.Dispatcher(loop=self.loop)

        class Child(event.Dispatcher):
            def __init__(self, **kw):
                super().__init__(**kw)
                self.count = 0

            def recv_evt_one(self, evt, *a, **kw):
                # attempt to deliver to the other child
                self.count += 1
                self.other_child.dispatch_event(evt)

        child1 = Child(loop=self.loop)
        child2 = Child(loop=self.loop)
        child1.other_child = child2
        child2.other_child = child1

        parent._add_child_dispatcher(child1)
        parent._add_child_dispatcher(child2)

        parent.dispatch_event(self.evt_one, param1=False, param2=123)
        for i in range(4):
            test_utils.run_briefly(self.loop)

        self.assertEqual(child1.count, 1)
        self.assertEqual(child2.count, 1)
Ejemplo n.º 2
0
    def test_dispatch_child_loops(self):
        # ensure that a child event handler cannot create a dispatch loop
        parent = event.Dispatcher(loop=self.loop)
        child = event.Dispatcher(loop=self.loop)
        parent._add_child_dispatcher(child)

        count = 0

        def handler(evt, *a, **kw):
            nonlocal count
            count += 1
            parent.dispatch_event(evt)

        child.add_event_handler(self.evt_one, handler)
        parent.dispatch_event(self.evt_one, param1=False, param2=123)

        test_utils.run_briefly(self.loop)

        # run loop twice to allow a second dispatched event to be delivered
        # (or hopefully not)
        test_utils.run_briefly(self.loop)

        self.assertEqual(count, 1)
Ejemplo n.º 3
0
    def test_stop_dispatcher(self):
        count = 0

        def handler(evt, *a, **kw):
            nonlocal count
            count += 1

        recv = event.Dispatcher(loop=self.loop)
        recv.add_event_handler(self.evt_one, handler)
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        recv._stop_dispatcher()
        recv.dispatch_event(self.evt_one, param1=False, param2=123)

        test_utils.run_briefly(self.loop)
        self.assertEqual(count, 2)
Ejemplo n.º 4
0
    def test_dispatch_oneshot(self):
        count = 0

        @event.oneshot
        def handler(evt, **kw):
            nonlocal count
            count += 1

        recv = event.Dispatcher(loop=self.loop)
        hnd = recv.add_event_handler(self.evt_one, handler)
        self.assertTrue(hnd.oneshot)

        # dispatch twice on the same loop run; should still only be called once
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        test_utils.run_briefly(self.loop)

        self.assertEqual(count, 1)
Ejemplo n.º 5
0
    def test_handler_disable_implicit(self):
        count = 0

        def handler(evt, **kw):
            nonlocal count
            count += 1

        recv = event.Dispatcher(loop=self.loop)
        hnd = recv.add_event_handler(self.evt_one, handler)
        # call twice
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        recv.dispatch_event(self.evt_one, param1=False, param2=123)

        # should no longer be dispatched
        hnd.disable()
        recv.dispatch_event(self.evt_one, param1=False, param2=123)
        test_utils.run_briefly(self.loop)

        self.assertEqual(count, 2)
Ejemplo n.º 6
0
    def test_obj_abort_futures(self):
        recv = event.Dispatcher(loop=self.loop)
        fut1 = asyncio.Future(loop=self.loop)
        fut2 = asyncio.Future(loop=self.loop)
        fut3 = asyncio.Future(loop=self.loop)
        fut3.set_result('result')  # should not be aborted

        exc = ValueError('test exception')

        recv.add_event_handler(self.evt_one, fut1)
        recv.add_event_handler(self.evt_one, fut2)
        recv.add_event_handler(self.evt_one, fut3)
        print(recv._dispatch_handlers)

        recv._abort_event_futures(exc)
        self.assertTrue(fut1.done())
        self.assertTrue(fut2.done())
        self.assertEqual(fut1.exception(), exc)
        self.assertEqual(fut2.exception(), exc)

        # futures should of been removed
        handlers = recv._dispatch_handlers['EvtOne']
        self.assertEqual(len(handlers), 0)