source:
ralphm-patches/stream-manager-logging.patch
@
72:727b4d29c48e
Last change on this file since 72:727b4d29c48e was 72:727b4d29c48e, checked in by Ralph Meijer <ralphm@…>, 9 years ago | |
---|---|
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 142 142 @type timeout: C{int} 143 143 144 144 @ivar _reactor: A provider of L{IReactorTime} to track timeouts. 145 146 @cvar __streamCount: Global stream count for distinguishing streams. 145 147 """ 146 148 147 149 timeout = None 148 150 _reactor = None 151 __streamCount = 0 149 152 150 153 logTraffic = False 151 154 … … 195 198 and call each handler's C{makeConnection} method with the L{XmlStream} 196 199 instance. 197 200 """ 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 203 203 204 204 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) 207 212 208 213 xs.addObserver(xmlstream.STREAM_AUTHD_EVENT, 209 214 self.connectionInitialized) 210 215 xs.addObserver(xmlstream.STREAM_END_EVENT, 211 216 self.connectionLost) 217 212 218 self.xmlstream = xs 213 219 214 220 for e in list(self): … … 222 228 Send out cached stanzas and call each handler's 223 229 C{connectionInitialized} method. 224 230 """ 231 if self.logTraffic: 232 log.msg(format="Connection %(streamID)s initialized", 233 streamID=xs.serial) 225 234 226 235 xs.addObserver('/iq[@type="result"]', self._onIQResponse) 227 236 xs.addObserver('/iq[@type="error"]', self._onIQResponse) … … 247 256 L{XmlStream} anymore and notifies each handler that the connection 248 257 was lost by calling its C{connectionLost} method. 249 258 """ 259 if self.logTraffic: 260 log.msg(format="Connection %(streamID)s lost", 261 streamID=self.xmlstream.serial) 262 250 263 self.xmlstream = None 251 264 self._initialized = False 252 265 -
wokkel/test/test_subprotocols.py
diff --git a/wokkel/test/test_subprotocols.py b/wokkel/test/test_subprotocols.py
a b 11 11 from twisted.test import proto_helpers 12 12 from twisted.internet import defer, task 13 13 from twisted.internet.error import ConnectionDone 14 from twisted.python import failure 14 from twisted.python import failure, log 15 15 from twisted.words.xish import domish 16 16 from twisted.words.protocols.jabber import error, ijabber, xmlstream 17 17 … … 230 230 Test raw data functions unset when logTraffic is set to False. 231 231 """ 232 232 sm = self.streamManager 233 sm.logTraffic = False 233 234 handler = DummyXMPPHandler() 234 235 handler.setHandlerParent(sm) 235 236 xs = self.xmlstream … … 240 241 241 242 def test_makeConnectionLogTrafficTrue(self): 242 243 """ 243 Test raw data functions setwhen logTraffic is set to True.244 Test raw data functions when logTraffic is set to True. 244 245 """ 245 246 sm = self.streamManager 246 247 sm.logTraffic = True 247 248 handler = DummyXMPPHandler() 248 249 handler.setHandlerParent(sm) 249 250 xs = self.xmlstream 251 252 # Set up log observer 253 logged = [] 254 log.addObserver(logged.append) 255 250 256 sm.makeConnection(xs) 251 257 self.assertNotIdentical(None, xs.rawDataInFn) 252 258 self.assertNotIdentical(None, xs.rawDataOutFn) 253 259 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 254 272 255 273 def test_connectionInitialized(self): 256 274 """ … … 258 276 called when the XML stream is initialized. 259 277 """ 260 278 sm = self.streamManager 279 sm.logTraffic = True 261 280 handler = DummyXMPPHandler() 262 281 handler.setHandlerParent(sm) 263 282 xs = self.xmlstream 283 xs.serial = 0 264 284 sm.connectionInitialized(xs) 265 285 self.assertEquals(0, handler.doneMade) 266 286 self.assertEquals(1, handler.doneInitialized) … … 272 292 Protocol handlers have connectionLost called on stream disconnect. 273 293 """ 274 294 sm = self.streamManager 295 sm.logTraffic = True 275 296 handler = DummyXMPPHandler() 276 297 handler.setHandlerParent(sm) 298 sm.xmlstream = xmlstream.XmlStream(xmlstream.Authenticator()) 299 sm.xmlstream.serial = 0 277 300 sm.connectionLost(None) 278 301 self.assertEquals(0, handler.doneMade) 279 302 self.assertEquals(0, handler.doneInitialized)
Note: See TracBrowser
for help on using the repository browser.