Python/BLIPConnectionTest.py
changeset 29 59689fbdcf77
parent 13 84c2d38f924c
child 51 de59ce19f42e
     1.1 --- a/Python/BLIPConnectionTest.py	Wed Jun 04 17:11:20 2008 -0700
     1.2 +++ b/Python/BLIPConnectionTest.py	Tue Apr 28 10:36:28 2009 -0700
     1.3 @@ -17,7 +17,9 @@
     1.4  import unittest
     1.5  
     1.6  
     1.7 -kSendInterval = 2.0
     1.8 +kSendInterval = 0.2
     1.9 +kNBatchedMessages = 10
    1.10 +kUrgentEvery = 4
    1.11  
    1.12  def randbool():
    1.13      return random.randint(0,1) == 1
    1.14 @@ -27,6 +29,7 @@
    1.15  
    1.16      def setUp(self):
    1.17          self.connection = Connection( ('localhost',46353) )
    1.18 +        self.nRepliesPending = 0
    1.19     
    1.20      def sendRequest(self):
    1.21          size = random.randint(0,32767)
    1.22 @@ -41,28 +44,31 @@
    1.23                                                       'Date': datetime.now(),
    1.24                                                       'Size': size})
    1.25          req.compressed = randbool()
    1.26 -        req.urgent     = randbool()
    1.27 +        req.urgent     = (random.randint(0,kUrgentEvery-1)==0)
    1.28          req.response.onComplete = self.gotResponse
    1.29          return req.send()
    1.30      
    1.31      def gotResponse(self, response):
    1.32 -        logging.info("Got response!: %s",response)
    1.33 +        self.nRepliesPending -= 1
    1.34 +        logging.info("Got response!: %s (%i pending)",response,self.nRepliesPending)
    1.35          request = response.request
    1.36          assert response.body == request.body
    1.37  
    1.38      def testClient(self):
    1.39          lastReqTime = None
    1.40 -        nRequests = 0
    1.41 -        while nRequests < 10:
    1.42 +        nIterations = 0
    1.43 +        while nIterations < 10:
    1.44              asyncore.loop(timeout=kSendInterval,count=1)
    1.45              
    1.46              now = datetime.now()
    1.47 -            if self.connection.status!=kOpening and not lastReqTime or (now-lastReqTime).seconds >= kSendInterval:
    1.48 +            if self.connection.status!=kOpening and (not lastReqTime or (now-lastReqTime).microseconds >= kSendInterval*1.0e6):
    1.49                  lastReqTime = now
    1.50 -                if not self.sendRequest():
    1.51 -                    logging.warn("Couldn't send request (connection is probably closed)")
    1.52 -                    break;
    1.53 -                nRequests += 1
    1.54 +                for i in xrange(0,kNBatchedMessages):
    1.55 +                    if not self.sendRequest():
    1.56 +                        logging.warn("Couldn't send request (connection is probably closed)")
    1.57 +                        break;
    1.58 +                    self.nRepliesPending += 1
    1.59 +                nIterations += 1
    1.60      
    1.61      def tearDown(self):
    1.62          self.connection.close()