Changeset 98:c3b20ad2bb70


Ignore:
Timestamp:
Mar 7, 2011, 7:40:16 PM (10 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Track change in Twisted Words, to pass a reason Failure with STREAM_END_EVENT.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/subprotocols.py

    r96 r98  
    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
     
    247248
    248249
    249     def _disconnected(self, _):
     250    def _disconnected(self, reason):
    250251        """
    251252        Called when the stream has been closed.
     
    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
  • wokkel/test/test_subprotocols.py

    r96 r98  
    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
     
    5354    def connectionLost(self, reason):
    5455        self.doneLost += 1
     56
     57
     58
     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
    5570
    5671
     
    235250    def test_disconnected(self):
    236251        """
    237         Test that protocol handlers have their connectionLost method
    238         called when the XML stream is disconnected.
    239         """
    240         sm = self.streamManager
    241         handler = DummyXMPPHandler()
    242         handler.setHandlerParent(sm)
    243         xs = xmlstream.XmlStream(xmlstream.Authenticator())
    244         sm._disconnected(xs)
     252        Protocol handlers have connectionLost called on stream disconnect.
     253        """
     254        sm = self.streamManager
     255        handler = DummyXMPPHandler()
     256        handler.setHandlerParent(sm)
     257        sm._disconnected(None)
    245258        self.assertEquals(0, handler.doneMade)
    246259        self.assertEquals(0, handler.doneInitialized)
    247260        self.assertEquals(1, handler.doneLost)
     261
     262
     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)
    248274
    249275
Note: See TracChangeset for help on using the changeset viewer.