Python/BLIPConnectionTest.py
author Dan Preston <danpreston@codechemistry.com>
Tue May 05 15:11:02 2009 -0700 (2009-05-05)
changeset 38 f090fd705556
parent 13 84c2d38f924c
child 51 de59ce19f42e
permissions -rw-r--r--
Fixed the release of an CFDataRef object under garbage collection.
     1 #!/usr/bin/env python
     2 # encoding: utf-8
     3 """
     4 BLIPConnectionTest.py
     5 
     6 Created by Jens Alfke on 2008-06-04.
     7 This source file is test/example code, and is in the public domain.
     8 """
     9 
    10 from BLIP import Connection, OutgoingRequest, kOpening
    11 
    12 import asyncore
    13 from cStringIO import StringIO
    14 from datetime import datetime
    15 import logging
    16 import random
    17 import unittest
    18 
    19 
    20 kSendInterval = 0.2
    21 kNBatchedMessages = 10
    22 kUrgentEvery = 4
    23 
    24 def randbool():
    25     return random.randint(0,1) == 1
    26 
    27 
    28 class BLIPConnectionTest(unittest.TestCase):
    29 
    30     def setUp(self):
    31         self.connection = Connection( ('localhost',46353) )
    32         self.nRepliesPending = 0
    33    
    34     def sendRequest(self):
    35         size = random.randint(0,32767)
    36         io = StringIO()
    37         for i in xrange(0,size):
    38             io.write( chr(i % 256) )
    39         body = io.getvalue()
    40         io.close
    41     
    42         req = OutgoingRequest(self.connection, body,{'Content-Type': 'application/octet-stream',
    43                                                      'User-Agent':  'PyBLIP',
    44                                                      'Date': datetime.now(),
    45                                                      'Size': size})
    46         req.compressed = randbool()
    47         req.urgent     = (random.randint(0,kUrgentEvery-1)==0)
    48         req.response.onComplete = self.gotResponse
    49         return req.send()
    50     
    51     def gotResponse(self, response):
    52         self.nRepliesPending -= 1
    53         logging.info("Got response!: %s (%i pending)",response,self.nRepliesPending)
    54         request = response.request
    55         assert response.body == request.body
    56 
    57     def testClient(self):
    58         lastReqTime = None
    59         nIterations = 0
    60         while nIterations < 10:
    61             asyncore.loop(timeout=kSendInterval,count=1)
    62             
    63             now = datetime.now()
    64             if self.connection.status!=kOpening and (not lastReqTime or (now-lastReqTime).microseconds >= kSendInterval*1.0e6):
    65                 lastReqTime = now
    66                 for i in xrange(0,kNBatchedMessages):
    67                     if not self.sendRequest():
    68                         logging.warn("Couldn't send request (connection is probably closed)")
    69                         break;
    70                     self.nRepliesPending += 1
    71                 nIterations += 1
    72     
    73     def tearDown(self):
    74         self.connection.close()
    75 
    76 if __name__ == '__main__':
    77     logging.basicConfig(level=logging.INFO)
    78     unittest.main()