source: ralphm-patches/stream-manager-logging.patch @ 83:255aae0cf8c5

Last change on this file since 83:255aae0cf8c5 was 72:727b4d29c48e, checked in by Ralph Meijer <ralphm@…>, 9 years ago

Major reworking of avatars, session manager and stanza handlers.

File size: 5.1 KB
  • wokkel/subprotocols.py

    # HG changeset patch
    # Parent 0e3216e1183f92874e0b1a0af185a9ce773522c8
    Improve traffic logging and add tests.
    
    diff --git a/wokkel/subprotocols.py b/wokkel/subprotocols.py
    a b  
    142142    @type timeout: C{int}
    143143
    144144    @ivar _reactor: A provider of L{IReactorTime} to track timeouts.
     145
     146    @cvar __streamCount: Global stream count for distinguishing streams.
    145147    """
    146148
    147149    timeout = None
    148150    _reactor = None
     151    __streamCount = 0
    149152
    150153    logTraffic = False
    151154
     
    195198        and call each handler's C{makeConnection} method with the L{XmlStream}
    196199        instance.
    197200        """
    198         def logDataIn(buf):
    199             log.msg("RECV: %r" % buf)
    200 
    201         def logDataOut(buf):
    202             log.msg("SEND: %r" % buf)
     201        xs.serial = self.__streamCount
     202        BaseStreamManager.__streamCount += 1
    203203
    204204        if self.logTraffic:
    205             xs.rawDataInFn = logDataIn
    206             xs.rawDataOutFn = logDataOut
     205            def logData(direction, data):
     206                log.msg(format="%(direction)s (%(streamID)s): %(data)r",
     207                        direction=direction, streamID=xs.serial, data=data)
     208
     209            log.msg(format="Connection %(streamID)s made", streamID=xs.serial)
     210            xs.rawDataInFn = lambda data: logData("RECV", data)
     211            xs.rawDataOutFn = lambda data: logData("SEND", data)
    207212
    208213        xs.addObserver(xmlstream.STREAM_AUTHD_EVENT,
    209214                       self.connectionInitialized)
    210215        xs.addObserver(xmlstream.STREAM_END_EVENT,
    211216                       self.connectionLost)
     217
    212218        self.xmlstream = xs
    213219
    214220        for e in list(self):
     
    222228        Send out cached stanzas and call each handler's
    223229        C{connectionInitialized} method.
    224230        """
     231        if self.logTraffic:
     232            log.msg(format="Connection %(streamID)s initialized",
     233                    streamID=xs.serial)
    225234
    226235        xs.addObserver('/iq[@type="result"]', self._onIQResponse)
    227236        xs.addObserver('/iq[@type="error"]', self._onIQResponse)
     
    247256        L{XmlStream} anymore and notifies each handler that the connection
    248257        was lost by calling its C{connectionLost} method.
    249258        """
     259        if self.logTraffic:
     260            log.msg(format="Connection %(streamID)s lost",
     261                    streamID=self.xmlstream.serial)
     262
    250263        self.xmlstream = None
    251264        self._initialized = False
    252265
  • wokkel/test/test_subprotocols.py

    diff --git a/wokkel/test/test_subprotocols.py b/wokkel/test/test_subprotocols.py
    a b  
    1111from twisted.test import proto_helpers
    1212from twisted.internet import defer, task
    1313from twisted.internet.error import ConnectionDone
    14 from twisted.python import failure
     14from twisted.python import failure, log
    1515from twisted.words.xish import domish
    1616from twisted.words.protocols.jabber import error, ijabber, xmlstream
    1717
     
    230230        Test raw data functions unset when logTraffic is set to False.
    231231        """
    232232        sm = self.streamManager
     233        sm.logTraffic = False
    233234        handler = DummyXMPPHandler()
    234235        handler.setHandlerParent(sm)
    235236        xs = self.xmlstream
     
    240241
    241242    def test_makeConnectionLogTrafficTrue(self):
    242243        """
    243         Test raw data functions set when logTraffic is set to True.
     244        Test raw data functions when logTraffic is set to True.
    244245        """
    245246        sm = self.streamManager
    246247        sm.logTraffic = True
    247248        handler = DummyXMPPHandler()
    248249        handler.setHandlerParent(sm)
    249250        xs = self.xmlstream
     251
     252        # Set up log observer
     253        logged = []
     254        log.addObserver(logged.append)
     255
    250256        sm.makeConnection(xs)
    251257        self.assertNotIdentical(None, xs.rawDataInFn)
    252258        self.assertNotIdentical(None, xs.rawDataOutFn)
    253259
     260        xs.rawDataInFn("<presence/>")
     261        self.assertEqual(2, len(logged))
     262
     263        eventDict = logged[-1]
     264        self.assertEqual('RECV', eventDict['direction'])
     265        self.assertEqual(xs.serial, eventDict['streamID'])
     266        self.assertEqual('<presence/>', eventDict['data'])
     267
     268        xs.rawDataOutFn("<presence/>")
     269        eventDict = logged[-1]
     270        self.assertEqual('SEND', eventDict['direction'])
     271
    254272
    255273    def test_connectionInitialized(self):
    256274        """
     
    258276        called when the XML stream is initialized.
    259277        """
    260278        sm = self.streamManager
     279        sm.logTraffic = True
    261280        handler = DummyXMPPHandler()
    262281        handler.setHandlerParent(sm)
    263282        xs = self.xmlstream
     283        xs.serial = 0
    264284        sm.connectionInitialized(xs)
    265285        self.assertEquals(0, handler.doneMade)
    266286        self.assertEquals(1, handler.doneInitialized)
     
    272292        Protocol handlers have connectionLost called on stream disconnect.
    273293        """
    274294        sm = self.streamManager
     295        sm.logTraffic = True
    275296        handler = DummyXMPPHandler()
    276297        handler.setHandlerParent(sm)
     298        sm.xmlstream = xmlstream.XmlStream(xmlstream.Authenticator())
     299        sm.xmlstream.serial = 0
    277300        sm.connectionLost(None)
    278301        self.assertEquals(0, handler.doneMade)
    279302        self.assertEquals(0, handler.doneInitialized)
Note: See TracBrowser for help on using the repository browser.