Merged revisions 9,17-20 via svnmerge from
authorJack Moffitt <jack@metajack.im>
Fri, 19 Sep 2008 15:52:58 +0000 (15:52 +0000)
committerJack Moffitt <jack@metajack.im>
Fri, 19 Sep 2008 15:52:58 +0000 (15:52 +0000)
https://code.stanziq.com/svn/punjab/branches/timeout-test-5

........
  r9 | tofu | 2008-09-18 07:48:00 -0600 (Thu, 18 Sep 2008) | 1 line

  test is not complete, just saving
........
  r17 | tofu | 2008-09-18 15:26:16 -0600 (Thu, 18 Sep 2008) | 1 line

  fix inactivity and add a timeout test
........
  r18 | tofu | 2008-09-18 16:54:57 -0600 (Thu, 18 Sep 2008) | 1 line

  test for terminate too
........
  r19 | tofu | 2008-09-19 08:05:55 -0600 (Fri, 19 Sep 2008) | 1 line

  alter the tests a bit
........
  r20 | tofu | 2008-09-19 08:44:19 -0600 (Fri, 19 Sep 2008) | 1 line

  dont use jabber
........

punjab/session.py
tests/xep124.py

index d93a467..b824e71 100644 (file)
@@ -67,12 +67,9 @@ def make_session(pint, attrs, session_type='BOSH'):
     s.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, s.connectEvent)
     s.addBootstrap(xmlstream.STREAM_ERROR_EVENT, s.streamError)
     s.addBootstrap(xmlstream.STREAM_END_EVENT, s.connectError)    
-
-    if attrs.has_key('inactivity'):
-        s.inactivity = attrs['inactivity']
-    else:
-        s.inactivity = 900 # 15 mins
-        
+    
+    s.inactivity = int(attrs.get('inactivity', 900)) # default inactivity 15 mins
+    
     s.secure = 0
     s.use_raw = getattr(pint, 'use_raw', False) # use raw buffers
     
@@ -310,6 +307,7 @@ class Session(jabber.JabberClientFactory, server.Session):
         self.terminated = True
         if self.verbose:
             log.msg('SESSION -> Terminate')
+        
         # if there are any elements hanging around and waiting
         # requests, send those off
         self.returnWaitingRequests()
@@ -341,10 +339,10 @@ class Session(jabber.JabberClientFactory, server.Session):
         if not rid:
             rid = self.rid - 1
         self.appendWaitingRequest(d, rid)
-            
+
         # check if there is any data to send back to a request
         self.returnWaitingRequests()
-                
+
         # make sure we aren't queueing too many requests
         self.clearWaitingRequests(self.hold)
         return d
@@ -355,7 +353,6 @@ class Session(jabber.JabberClientFactory, server.Session):
         Since the timeout function is called, we must return an empty
         reply as there is no data to send back.
         """
-        
         # find the request that timed out and reply
         pop_eye = []
         for i in range(len(self.waiting_requests)):
index 17bcdfb..240a152 100644 (file)
@@ -133,6 +133,75 @@ class XEP0124TestCase(unittest.TestCase):
 
         return d
 
+
+    def testSessionTimeout(self):
+        """Test if we timeout correctly
+        """
+        d = defer.Deferred()
+
+        def testTimeout(res):
+            passed = True
+            
+            if res.value[0]!='404':
+                passed = False
+                d.errback((Exception, 'Wrong Value %s '% (str(res.value),)))
+            if passed:
+                d.callback(True)
+            else:
+                log.err(res)
+
+        def testCBTimeout(res):
+            # check for terminate if we expire 
+            terminate = res[0].getAttribute('type',False)
+            
+            if str(terminate) != 'terminate':
+                d.errback((Exception, 'Was not terminate'))
+                return
+            d.callback(True)
+
+        def sendTest():
+            sd = self.send()
+            sd.addCallback(testCBTimeout)
+            sd.addErrback(testTimeout)
+            
+
+        def testResend(res):
+            self.failUnless(res[0].name=='body', 'Wrong element')
+            s = self.b.service.sessions[self.sid]
+            self.failUnless(s.inactivity==10,'Wrong inactivity value')
+            self.failUnless(s.wait==10, 'Wrong wait value')
+            reactor.callLater(s.wait+s.inactivity+1, sendTest)
+            
+
+        def testSessionCreate(res):
+            self.failUnless(res[0].name=='body', 'Wrong element')            
+            self.failUnless(res[0].hasAttribute('sid'),'Not session id')
+            self.sid = res[0]['sid']
+
+            # send and wait 
+            sd = self.send()
+            
+            sd.addCallback(testResend)
+            
+
+
+        BOSH_XML = """<body content='text/xml; charset=utf-8'
+      hold='1'
+      rid='%d'
+      to='localhost'
+      route='xmpp:127.0.0.1:5222'
+      ver='1.6'
+      wait='10'
+      ack='1'
+      inactivity='10'
+      xml:lang='en'
+      xmlns='http://jabber.org/protocol/httpbind'/>
+ """% (self.rid,)
+
+        self.proxy.connect(BOSH_XML).addCallback(testSessionCreate)
+        d.addErrback(self.fail)
+        return d
+
     def testStreamError(self):
         """
         This is to test if we get stream errors when there are no waiting requests.
@@ -176,6 +245,7 @@ class XEP0124TestCase(unittest.TestCase):
         return d
 
 
+
     def testFeaturesError(self):
         """
         This is to test if we get stream features and NOT twice