source: ralphm-patches/connectionLost-reason.patch @ 47:f6d222b68f1c

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

Many new patches, upstreamed affiliations and worked on pubsub item.
Upstreamed pubsub_manage_affiliations

File size: 3.5 KB
  • wokkel/subprotocols.py

    # HG changeset patch
    # Parent 28a4dc8ad569c665b609533feb1823aa5241f7e4
    Track change in Twisted Words, to pass a reason Failure with STREAM_END_EVENT.
    
    diff -r 28a4dc8ad569 wokkel/subprotocols.py
    a b  
    1010from zope.interface import implements
    1111
    1212from twisted.internet import defer
    13 from twisted.python import log
     13from twisted.internet.error import ConnectionDone
     14from twisted.python import failure, log
    1415from twisted.words.protocols.jabber import error, xmlstream
    1516from twisted.words.protocols.jabber.xmlstream import toResponse
    1617from twisted.words.xish import xpath
     
    246247        """
    247248
    248249
    249     def _disconnected(self, _):
     250    def _disconnected(self, reason):
    250251        """
    251252        Called when the stream has been closed.
    252253
     
    257258        self.xmlstream = None
    258259        self._initialized = False
    259260
     261        # Twisted versions before 11.0 passed an XmlStream here.
     262        if not hasattr(reason, 'trap'):
     263            reason = failure.Failure(ConnectionDone())
     264
    260265        # Notify all child services which implement
    261266        # the IService interface
    262267        for e in self:
    263             e.connectionLost(None)
     268            e.connectionLost(reason)
    264269
    265270
    266271    def send(self, obj):
  • wokkel/test/test_subprotocols.py

    diff -r 28a4dc8ad569 wokkel/test/test_subprotocols.py
    a b  
    1010from twisted.trial import unittest
    1111from twisted.test import proto_helpers
    1212from twisted.internet import defer
     13from twisted.python import failure
    1314from twisted.words.xish import domish
    1415from twisted.words.protocols.jabber import error, xmlstream
    1516
     
    5556
    5657
    5758
     59class FailureReasonXMPPHandler(subprotocols.XMPPHandler):
     60    """
     61    Dummy handler specifically for failure Reason tests.
     62    """
     63    def __init__(self):
     64        self.gotFailureReason = False
     65
     66
     67    def connectionLost(self, reason):
     68        if isinstance(reason, failure.Failure):
     69            self.gotFailureReason = True
     70
     71
     72
    5873class XMPPHandlerTest(unittest.TestCase):
    5974    """
    6075    Tests for L{subprotocols.XMPPHandler}.
     
    234249
    235250    def test_disconnected(self):
    236251        """
    237         Test that protocol handlers have their connectionLost method
    238         called when the XML stream is disconnected.
     252        Protocol handlers have connectionLost called on stream disconnect.
    239253        """
    240254        sm = self.streamManager
    241255        handler = DummyXMPPHandler()
    242256        handler.setHandlerParent(sm)
    243         xs = xmlstream.XmlStream(xmlstream.Authenticator())
    244         sm._disconnected(xs)
     257        sm._disconnected(None)
    245258        self.assertEquals(0, handler.doneMade)
    246259        self.assertEquals(0, handler.doneInitialized)
    247260        self.assertEquals(1, handler.doneLost)
    248261
    249262
     263    def test_disconnectedReason(self):
     264        """
     265        A L{STREAM_END_EVENT} results in L{StreamManager} firing the handlers
     266        L{connectionLost} methods, passing a L{failure.Failure} reason.
     267        """
     268        sm = self.streamManager
     269        handler = FailureReasonXMPPHandler()
     270        handler.setHandlerParent(sm)
     271        xs = xmlstream.XmlStream(xmlstream.Authenticator())
     272        sm._disconnected(failure.Failure(Exception("no reason")))
     273        self.assertEquals(True, handler.gotFailureReason)
     274
     275
    250276    def test_addHandler(self):
    251277        """
    252278        Test the addition of a protocol handler while not connected.
Note: See TracBrowser for help on using the repository browser.