Skip to content

Commit

Permalink
Make IQ timeouts work for InternalComponents.
Browse files Browse the repository at this point in the history
This introduces wokkel.compat.IQ to work with older Twisted versions, too.

Author: ralphm.
Fixes #53.

--HG--
extra : convert_revision : svn%3Ab33ecbfc-034c-dc11-8662-000475d9059e/trunk%40175
  • Loading branch information
ralphm committed Jul 6, 2009
1 parent 7f72b6d commit a1714d3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
25 changes: 23 additions & 2 deletions wokkel/compat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- test-case-name: wokkel.test.test_compat -*-
#
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
# See LICENSE for details.

from twisted.internet import protocol
from twisted.words.protocols.jabber import xmlstream
from twisted.words.xish import domish

class BootstrapMixin(object):
"""
Expand Down Expand Up @@ -94,3 +93,25 @@ def buildProtocol(self, addr):
xs.factory = self
self.installBootstraps(xs)
return xs



class IQ(xmlstream.IQ):
def __init__(self, *args, **kwargs):
# Make sure we have a reactor parameter
try:
reactor = kwargs['reactor']
except KeyError:
from twisted.internet import reactor
kwargs['reactor'] = reactor

# Check if IQ's init accepts the reactor parameter
try:
xmlstream.IQ.__init__(self, *args, **kwargs)
except TypeError:
# Guess not. Remove the reactor parameter and try again.
del kwargs['reactor']
xmlstream.IQ.__init__(self, *args, **kwargs)

# Patch the XmlStream instance so that it has a _callLater
self._xmlstream._callLater = reactor.callLater
7 changes: 4 additions & 3 deletions wokkel/disco.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"""

from twisted.internet import defer
from twisted.words.protocols.jabber import error, jid, xmlstream
from twisted.words.protocols.jabber import error, jid
from twisted.words.xish import domish

from wokkel import data_form
from wokkel.compat import IQ
from wokkel.iwokkel import IDisco
from wokkel.subprotocols import IQHandlerMixin, XMPPHandler

Expand Down Expand Up @@ -345,7 +346,7 @@ def fromElement(element):



class _DiscoRequest(xmlstream.IQ):
class _DiscoRequest(IQ):
"""
Element representing an XMPP service discovery request.
"""
Expand All @@ -361,7 +362,7 @@ def __init__(self, xs, namespace, nodeIdentifier=''):
@param nodeIdentifier: Node to request info from.
@type nodeIdentifier: C{unicode}
"""
xmlstream.IQ.__init__(self, xs, "get")
IQ.__init__(self, xs, "get")
query = self.addElement((namespace, 'query'))
if nodeIdentifier:
query['node'] = nodeIdentifier
Expand Down
11 changes: 6 additions & 5 deletions wokkel/pubsub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- test-case-name: wokkel.test.test_pubsub -*-
#
# Copyright (c) 2003-2008 Ralph Meijer
# Copyright (c) 2003-2009 Ralph Meijer
# See LICENSE for details.

"""
Expand All @@ -14,10 +14,11 @@

from twisted.internet import defer
from twisted.python import log
from twisted.words.protocols.jabber import jid, error, xmlstream
from twisted.words.protocols.jabber import jid, error
from twisted.words.xish import domish

from wokkel import disco, data_form, generic, shim
from wokkel.compat import IQ
from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
from wokkel.iwokkel import IPubSubClient, IPubSubService, IPubSubResource

Expand Down Expand Up @@ -475,9 +476,9 @@ def send(self, xs):
Send this request to its recipient.
This renders all of the relevant parameters for this specific
requests into an L{xmlstream.IQ}, and invoke its C{send} method.
requests into an L{IQ}, and invoke its C{send} method.
This returns a deferred that fires upon reception of a response. See
L{xmlstream.IQ} for details.
L{IQ} for details.
@param xs: The XML stream to send the request on.
@type xs: L{xmlstream.XmlStream}
Expand All @@ -491,7 +492,7 @@ def send(self, xs):
except KeyError:
raise NotImplementedError()

iq = xmlstream.IQ(xs, self.stanzaType)
iq = IQ(xs, self.stanzaType)
iq.addElement((childURI, 'pubsub'))
verbElement = iq.pubsub.addElement(childName)

Expand Down
50 changes: 46 additions & 4 deletions wokkel/test/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
Tests for L{wokkel.compat}.
"""

from zope.interface import implements
from zope.interface.verify import verifyObject
from twisted.internet import defer, protocol
from twisted.internet.interfaces import IProtocolFactory
from twisted.internet import protocol, task
from twisted.internet.interfaces import IProtocolFactory, IReactorTime
from twisted.trial import unittest
from twisted.words.xish import domish, utility
from twisted.words.xish import utility
from twisted.words.protocols.jabber import xmlstream
from wokkel.compat import BootstrapMixin, XmlStreamServerFactory
from wokkel.compat import BootstrapMixin, IQ, XmlStreamServerFactory

class DummyProtocol(protocol.Protocol, utility.EventDispatcher):
"""
Expand Down Expand Up @@ -165,3 +166,44 @@ def test_buildProtocolStoresFactory(self):
"""
xs = self.factory.buildProtocol(None)
self.assertIdentical(self.factory, xs.factory)



class FakeReactor(object):

implements(IReactorTime)
def __init__(self):
self.clock = task.Clock()
self.callLater = self.clock.callLater
self.getDelayedCalls = self.clock.getDelayedCalls



class IQTest(unittest.TestCase):
"""
Tests for L{IQ}.
"""

def setUp(self):
self.reactor = FakeReactor()
self.clock = self.reactor.clock


def testRequestTimingOutEventDispatcher(self):
"""
Test that an iq request with a defined timeout times out.
"""
from twisted.words.xish import utility
output = []
xs = utility.EventDispatcher()
xs.send = output.append

self.iq = IQ(xs, reactor=self.reactor)
self.iq.timeout = 60
d = self.iq.send()
self.assertFailure(d, xmlstream.TimeoutError)

self.clock.pump([1, 60])
self.assertFalse(self.reactor.getDelayedCalls())
self.assertFalse(xs.iqDeferreds)
return d
4 changes: 2 additions & 2 deletions wokkel/test/test_disco.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def test_requestItemsFrom(self):
self.assertEqual(u'test.example.org', iq.getAttribute(u'from'))

response = toResponse(iq, u'result')
query = response.addElement((NS_DISCO_ITEMS, u'query'))
response.addElement((NS_DISCO_ITEMS, u'query'))
self.stub.send(response)

return d
Expand Down Expand Up @@ -581,7 +581,7 @@ def test_requestInfoFrom(self):
self.assertEqual(u'test.example.org', iq.getAttribute(u'from'))

response = toResponse(iq, u'result')
query = response.addElement((NS_DISCO_INFO, u'query'))
response.addElement((NS_DISCO_INFO, u'query'))
self.stub.send(response)

return d
Expand Down
2 changes: 1 addition & 1 deletion wokkel/xmppim.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"""

from twisted.words.protocols.jabber.jid import JID
from twisted.words.protocols.jabber.xmlstream import IQ
from twisted.words.xish import domish

from wokkel.compat import IQ
from wokkel.subprotocols import XMPPHandler

NS_XML = 'http://www.w3.org/XML/1998/namespace'
Expand Down

0 comments on commit a1714d3

Please sign in to comment.