Changeset 63:b8139f79276f


Ignore:
Timestamp:
Jul 6, 2009, 9:26:47 AM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@175
Message:

Make IQ timeouts work for InternalComponents?.

This introduces wokkel.compat.IQ to work with older Twisted versions, too.

Author: ralphm.
Fixes #53.

Location:
wokkel
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • wokkel/compat.py

    r53 r63  
    11# -*- test-case-name: wokkel.test.test_compat -*-
    22#
    3 # Copyright (c) 2001-2008 Twisted Matrix Laboratories.
     3# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
    44# See LICENSE for details.
    55
    66from twisted.internet import protocol
    77from twisted.words.protocols.jabber import xmlstream
    8 from twisted.words.xish import domish
    98
    109class BootstrapMixin(object):
     
    9594        self.installBootstraps(xs)
    9695        return xs
     96
     97
     98
     99class IQ(xmlstream.IQ):
     100    def __init__(self, *args, **kwargs):
     101        # Make sure we have a reactor parameter
     102        try:
     103            reactor = kwargs['reactor']
     104        except KeyError:
     105            from twisted.internet import reactor
     106        kwargs['reactor'] = reactor
     107
     108        # Check if IQ's init accepts the reactor parameter
     109        try:
     110            xmlstream.IQ.__init__(self, *args, **kwargs)
     111        except TypeError:
     112            # Guess not. Remove the reactor parameter and try again.
     113            del kwargs['reactor']
     114            xmlstream.IQ.__init__(self, *args, **kwargs)
     115
     116            # Patch the XmlStream instance so that it has a _callLater
     117            self._xmlstream._callLater = reactor.callLater
  • wokkel/disco.py

    r62 r63  
    1212
    1313from twisted.internet import defer
    14 from twisted.words.protocols.jabber import error, jid, xmlstream
     14from twisted.words.protocols.jabber import error, jid
    1515from twisted.words.xish import domish
    1616
    1717from wokkel import data_form
     18from wokkel.compat import IQ
    1819from wokkel.iwokkel import IDisco
    1920from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
     
    346347
    347348
    348 class _DiscoRequest(xmlstream.IQ):
     349class _DiscoRequest(IQ):
    349350    """
    350351    Element representing an XMPP service discovery request.
     
    362363        @type nodeIdentifier: C{unicode}
    363364        """
    364         xmlstream.IQ.__init__(self, xs, "get")
     365        IQ.__init__(self, xs, "get")
    365366        query = self.addElement((namespace, 'query'))
    366367        if nodeIdentifier:
  • wokkel/pubsub.py

    r59 r63  
    11# -*- test-case-name: wokkel.test.test_pubsub -*-
    22#
    3 # Copyright (c) 2003-2008 Ralph Meijer
     3# Copyright (c) 2003-2009 Ralph Meijer
    44# See LICENSE for details.
    55
     
    1515from twisted.internet import defer
    1616from twisted.python import log
    17 from twisted.words.protocols.jabber import jid, error, xmlstream
     17from twisted.words.protocols.jabber import jid, error
    1818from twisted.words.xish import domish
    1919
    2020from wokkel import disco, data_form, generic, shim
     21from wokkel.compat import IQ
    2122from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
    2223from wokkel.iwokkel import IPubSubClient, IPubSubService, IPubSubResource
     
    476477
    477478        This renders all of the relevant parameters for this specific
    478         requests into an L{xmlstream.IQ}, and invoke its C{send} method.
     479        requests into an L{IQ}, and invoke its C{send} method.
    479480        This returns a deferred that fires upon reception of a response. See
    480         L{xmlstream.IQ} for details.
     481        L{IQ} for details.
    481482
    482483        @param xs: The XML stream to send the request on.
     
    492493            raise NotImplementedError()
    493494
    494         iq = xmlstream.IQ(xs, self.stanzaType)
     495        iq = IQ(xs, self.stanzaType)
    495496        iq.addElement((childURI, 'pubsub'))
    496497        verbElement = iq.pubsub.addElement(childName)
  • wokkel/test/test_compat.py

    r53 r63  
    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):
     
    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    Tests for L{IQ}.
     185    """
     186
     187    def setUp(self):
     188        self.reactor = FakeReactor()
     189        self.clock = self.reactor.clock
     190
     191
     192    def testRequestTimingOutEventDispatcher(self):
     193        """
     194        Test that an iq request with a defined timeout times out.
     195        """
     196        from twisted.words.xish import utility
     197        output = []
     198        xs = utility.EventDispatcher()
     199        xs.send = output.append
     200
     201        self.iq = IQ(xs, reactor=self.reactor)
     202        self.iq.timeout = 60
     203        d = self.iq.send()
     204        self.assertFailure(d, xmlstream.TimeoutError)
     205
     206        self.clock.pump([1, 60])
     207        self.assertFalse(self.reactor.getDelayedCalls())
     208        self.assertFalse(xs.iqDeferreds)
     209        return d
  • wokkel/test/test_disco.py

    r62 r63  
    526526
    527527        response = toResponse(iq, u'result')
    528         query = response.addElement((NS_DISCO_ITEMS, u'query'))
     528        response.addElement((NS_DISCO_ITEMS, u'query'))
    529529        self.stub.send(response)
    530530
     
    582582
    583583        response = toResponse(iq, u'result')
    584         query = response.addElement((NS_DISCO_INFO, u'query'))
     584        response.addElement((NS_DISCO_INFO, u'query'))
    585585        self.stub.send(response)
    586586
  • wokkel/xmppim.py

    r28 r63  
    1414
    1515from twisted.words.protocols.jabber.jid import JID
    16 from twisted.words.protocols.jabber.xmlstream import IQ
    1716from twisted.words.xish import domish
    1817
     18from wokkel.compat import IQ
    1919from wokkel.subprotocols import XMPPHandler
    2020
Note: See TracChangeset for help on using the changeset viewer.