Python/BLIPConnectionTest.py
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 51 de59ce19f42e
permissions -rw-r--r--
* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
     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 = 4 # send only 40 requests total
    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         asyncore.loop() # got to give it time to negotiate close; this call should exit eventually
    76 
    77 if __name__ == '__main__':
    78     logging.basicConfig(level=logging.INFO)
    79     unittest.main()