source: ralphm-patches/iq-calllater.patch @ 15:3fd4aaba4a99

Last change on this file since 15:3fd4aaba4a99 was 15:3fd4aaba4a99, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Save recent work.

File size: 3.0 KB
  • wokkel/compat.py

    diff -r 914145a9b24a -r 8e839bdd351f wokkel/compat.py
    a b  
    9494        xs.factory = self
    9595        self.installBootstraps(xs)
    9696        return xs
     97
     98
     99
     100class IQ(xmlstream.IQ):
     101    def __init__(self, *args, **kwargs):
     102        # Make sure we have a reactor parameter
     103        try:
     104            reactor = kwargs['reactor']
     105        except KeyError:
     106            from twisted.internet import reactor
     107        kwargs['reactor'] = reactor
     108
     109        # Check if IQ's init accepts the reactor parameter
     110        try:
     111            xmlstream.IQ.__init__(self, *args, **kwargs)
     112        except TypeError:
     113            # Guess not. Remove the reactor parameter and try again.
     114            del kwargs['reactor']
     115            xmlstream.IQ.__init__(self, *args, **kwargs)
     116
     117            # Patch the XmlStream instance so that it has a _callLater
     118            self._xmlstream._callLater = reactor.callLater
  • wokkel/test/test_compat.py

    diff -r 914145a9b24a -r 8e839bdd351f wokkel/test/test_compat.py
    a b  
    66Tests for L{wokkel.compat}.
    77"""
    88
     9from zope.interface import implements
    910from zope.interface.verify import verifyObject
    10 from twisted.internet import defer, protocol
    11 from twisted.internet.interfaces import IProtocolFactory
     11from twisted.internet import protocol, task
     12from twisted.internet.interfaces import IProtocolFactory, IReactorTime
    1213from twisted.trial import unittest
    13 from twisted.words.xish import domish, utility
     14from twisted.words.xish import utility
    1415from twisted.words.protocols.jabber import xmlstream
    15 from wokkel.compat import BootstrapMixin, XmlStreamServerFactory
     16from wokkel.compat import BootstrapMixin, IQ, XmlStreamServerFactory
    1617
    1718class DummyProtocol(protocol.Protocol, utility.EventDispatcher):
    1819    """
     
    165166        """
    166167        xs = self.factory.buildProtocol(None)
    167168        self.assertIdentical(self.factory, xs.factory)
     169
     170
     171
     172class FakeReactor(object):
     173
     174    implements(IReactorTime)
     175    def __init__(self):
     176        self.clock = task.Clock()
     177        self.callLater = self.clock.callLater
     178        self.getDelayedCalls = self.clock.getDelayedCalls
     179
     180
     181
     182class IQTest(unittest.TestCase):
     183
     184    def setUp(self):
     185        self.reactor = FakeReactor()
     186        self.clock = self.reactor.clock
     187
     188
     189    def testRequestTimingOutEventDispatcher(self):
     190        """
     191        Test that an iq request with a defined timeout times out.
     192        """
     193        from twisted.words.xish import utility
     194        output = []
     195        xs = utility.EventDispatcher()
     196        xs.send = output.append
     197
     198        self.iq = IQ(xs, reactor=self.reactor)
     199        self.iq.timeout = 60
     200        d = self.iq.send()
     201        self.assertFailure(d, xmlstream.TimeoutError)
     202
     203        self.clock.pump([1, 60])
     204        self.assertFalse(self.reactor.getDelayedCalls())
     205        self.assertFalse(xs.iqDeferreds)
     206        return d
     207
     208
Note: See TracBrowser for help on using the repository browser.