Esempio n. 1
0
    def testDispatcherResult(self):
        d = EventDispatcher()
        msg = Element(("ns", "message"))
        pres = Element(("ns", "presence"))
        cb = CallbackTracker()

        d.addObserver("/presence", cb.call)
        result = d.dispatch(msg)
        self.assertEquals(False, result)

        result = d.dispatch(pres)
        self.assertEquals(True, result)
Esempio n. 2
0
    def testDispatcherResult(self):
        d = EventDispatcher()
        msg = Element(("ns", "message"))
        pres = Element(("ns", "presence"))
        cb = CallbackTracker()

        d.addObserver("/presence", cb.call)
        result = d.dispatch(msg)
        self.assertEquals(False, result)

        result = d.dispatch(pres)
        self.assertEquals(True, result)
 def testAddObserverInDispatch(self):
     # Test for registration of events during dispatch
     d = EventDispatcher()
     msg = Element(("ns", "message"))
     pres = Element(("ns", "presence"))
     cb = CallbackTracker2(d)
     
     d.addObserver("/message", cb.call2)
     d.dispatch(msg)
     self.assertEquals(cb.called, 0)
     d.dispatch(pres)
     self.assertEquals(cb.called, 1)
Esempio n. 4
0
    def testAddObserverInDispatch(self):
        # Test for registration of events during dispatch
        d = EventDispatcher()
        msg = Element(("ns", "message"))
        pres = Element(("ns", "presence"))
        cb = CallbackTracker2(d)

        d.addObserver("/message", cb.call2)
        d.dispatch(msg)
        self.assertEquals(cb.called, 0)
        d.dispatch(pres)
        self.assertEquals(cb.called, 1)
Esempio n. 5
0
    def test_cleanUpRemoveEventObserver(self):
        """
        Test observer clean-up after removeObserver for named events.
        """

        d = EventDispatcher()
        cb = CallbackTracker()

        d.addObserver('//event/test', cb.call)
        d.dispatch(None, '//event/test')
        self.assertEqual(1, cb.called)
        d.removeObserver('//event/test', cb.call)
        self.assertEqual(0, len(d._eventObservers.pop(0)))
Esempio n. 6
0
    def test_cleanUpRemoveEventObserver(self):
        """
        Test observer clean-up after removeObserver for named events.
        """

        d = EventDispatcher()
        cb = CallbackTracker()

        d.addObserver('//event/test', cb.call)
        d.dispatch(None, '//event/test')
        self.assertEqual(1, cb.called)
        d.removeObserver('//event/test', cb.call)
        self.assertEqual(0, len(d._eventObservers.pop(0)))
Esempio n. 7
0
    def testStuff(self):
        d = EventDispatcher()
        cb1 = CallbackTracker()
        cb2 = CallbackTracker()
        cb3 = CallbackTracker()

        d.addObserver("/message/body", cb1.call)
        d.addObserver("/message", cb1.call)
        d.addObserver("/presence", cb2.call)
        d.addObserver("//event/testevent", cb3.call)

        msg = Element(("ns", "message"))
        msg.addElement("body")

        pres = Element(("ns", "presence"))
        pres.addElement("presence")

        d.dispatch(msg)
        self.assertEquals(cb1.called, 2)
        self.assertEquals(cb1.obj, msg)
        self.assertEquals(cb2.called, 0)

        d.dispatch(pres)
        self.assertEquals(cb1.called, 2)
        self.assertEquals(cb2.called, 1)
        self.assertEquals(cb2.obj, pres)
        self.assertEquals(cb3.called, 0)

        d.dispatch(d, "//event/testevent")
        self.assertEquals(cb3.called, 1)
        self.assertEquals(cb3.obj, d)

        d.removeObserver("/presence", cb2.call)
        d.dispatch(pres)
        self.assertEquals(cb2.called, 1)
Esempio n. 8
0
    def test_cleanUpRemoveXPathObserver(self):
        """
        Test observer clean-up after removeObserver for XPath events.
        """

        d = EventDispatcher()
        cb = CallbackTracker()
        msg = Element((None, "message"))

        d.addObserver('/message', cb.call)
        d.dispatch(msg)
        self.assertEqual(1, cb.called)
        d.removeObserver('/message', cb.call)
        self.assertEqual(0, len(d._xpathObservers.pop(0)))
Esempio n. 9
0
    def testStuff(self):
        d = EventDispatcher()
        cb1 = CallbackTracker()
        cb2 = CallbackTracker()
        cb3 = CallbackTracker()

        d.addObserver("/message/body", cb1.call)
        d.addObserver("/message", cb1.call)
        d.addObserver("/presence", cb2.call)
        d.addObserver("//event/testevent", cb3.call)

        msg = Element(("ns", "message"))
        msg.addElement("body")

        pres = Element(("ns", "presence"))
        pres.addElement("presence")

        d.dispatch(msg)
        self.assertEquals(cb1.called, 2)
        self.assertEquals(cb1.obj, msg)
        self.assertEquals(cb2.called, 0)

        d.dispatch(pres)
        self.assertEquals(cb1.called, 2)
        self.assertEquals(cb2.called, 1)
        self.assertEquals(cb2.obj, pres)
        self.assertEquals(cb3.called, 0)

        d.dispatch(d, "//event/testevent")
        self.assertEquals(cb3.called, 1)
        self.assertEquals(cb3.obj, d)

        d.removeObserver("/presence", cb2.call)
        d.dispatch(pres)
        self.assertEquals(cb2.called, 1)
Esempio n. 10
0
    def test_cleanUpRemoveXPathObserver(self):
        """
        Test observer clean-up after removeObserver for XPath events.
        """

        d = EventDispatcher()
        cb = CallbackTracker()
        msg = Element((None, "message"))

        d.addObserver('/message', cb.call)
        d.dispatch(msg)
        self.assertEqual(1, cb.called)
        d.removeObserver('/message', cb.call)
        self.assertEqual(0, len(d._xpathObservers.pop(0)))
Esempio n. 11
0
    def test_addObserverTwice(self):
        """
        Test adding two observers for the same query.

        When the event is dispath both of the observers need to be called.
        """
        d = EventDispatcher()
        cb1 = CallbackTracker()
        cb2 = CallbackTracker()

        d.addObserver("//event/testevent", cb1.call)
        d.addObserver("//event/testevent", cb2.call)
        d.dispatch(d, "//event/testevent")

        self.assertEquals(cb1.called, 1)
        self.assertEquals(cb1.obj, d)
        self.assertEquals(cb2.called, 1)
        self.assertEquals(cb2.obj, d)
Esempio n. 12
0
    def test_addObserverTwice(self):
        """
        Test adding two observers for the same query.

        When the event is dispath both of the observers need to be called.
        """
        d = EventDispatcher()
        cb1 = CallbackTracker()
        cb2 = CallbackTracker()

        d.addObserver("//event/testevent", cb1.call)
        d.addObserver("//event/testevent", cb2.call)
        d.dispatch(d, "//event/testevent")

        self.assertEquals(cb1.called, 1)
        self.assertEquals(cb1.obj, d)
        self.assertEquals(cb2.called, 1)
        self.assertEquals(cb2.obj, d)
Esempio n. 13
0
    def test_observerRaisingException(self):
        """
        Test that exceptions in observers do not bubble up to dispatch.

        The exceptions raised in observers should be logged and other
        observers should be called as if nothing happened.
        """

        class OrderedCallbackList(utility.CallbackList):
            def __init__(self):
                self.callbacks = OrderedDict()

        class TestError(Exception):
            pass

        def raiseError(_):
            raise TestError()

        d = EventDispatcher()
        cb = CallbackTracker()

        originalCallbackList = utility.CallbackList

        try:
            utility.CallbackList = OrderedCallbackList

            d.addObserver('//event/test', raiseError)
            d.addObserver('//event/test', cb.call)
            try:
                d.dispatch(None, '//event/test')
            except TestError:
                self.fail("TestError raised. Should have been logged instead.")

            self.assertEqual(1, len(self.flushLoggedErrors(TestError)))
            self.assertEqual(1, cb.called)
        finally:
            utility.CallbackList = originalCallbackList
Esempio n. 14
0
    def test_observerRaisingException(self):
        """
        Test that exceptions in observers do not bubble up to dispatch.

        The exceptions raised in observers should be logged and other
        observers should be called as if nothing happened.
        """

        class OrderedCallbackList(utility.CallbackList):
            def __init__(self):
                self.callbacks = OrderedDict()

        class TestError(Exception):
            pass

        def raiseError(_):
            raise TestError()

        d = EventDispatcher()
        cb = CallbackTracker()

        originalCallbackList = utility.CallbackList

        try:
            utility.CallbackList = OrderedCallbackList

            d.addObserver('//event/test', raiseError)
            d.addObserver('//event/test', cb.call)
            try:
                d.dispatch(None, '//event/test')
            except TestError:
                self.fail("TestError raised. Should have been logged instead.")

            self.assertEqual(1, len(self.flushLoggedErrors(TestError)))
            self.assertEqual(1, cb.called)
        finally:
            utility.CallbackList = originalCallbackList
Esempio n. 15
0
    def testOrderedXPathDispatch(self):
        d = EventDispatcher()
        cb = OrderedCallbackTracker()
        d.addObserver("/message/body", cb.call2)
        d.addObserver("/message", cb.call3, -1)
        d.addObserver("/message/body", cb.call1, 1)

        msg = Element(("ns", "message"))
        msg.addElement("body")
        d.dispatch(msg)
        self.assertEquals(
            cb.callList, [cb.call1, cb.call2, cb.call3],
            "Calls out of order: %s" % repr([c.__name__ for c in cb.callList]))
Esempio n. 16
0
    def testOrderedXPathDispatch(self):
        d = EventDispatcher()
        cb = OrderedCallbackTracker()
        d.addObserver("/message/body", cb.call2)
        d.addObserver("/message", cb.call3, -1)
        d.addObserver("/message/body", cb.call1, 1)

        msg = Element(("ns", "message"))
        msg.addElement("body")
        d.dispatch(msg)
        self.assertEquals(cb.callList, [cb.call1, cb.call2, cb.call3],
                          "Calls out of order: %s" %
                          repr([c.__name__ for c in cb.callList]))