示例#1
0
class BLIPConnectionTest(unittest.TestCase):

    def setUp(self):
        self.connection = Connection( ('localhost',46353) )
        self.nRepliesPending = 0
   
    def sendRequest(self):
        size = random.randint(0,32767)
        io = StringIO()
        for i in xrange(0,size):
            io.write( chr(i % 256) )
        body = io.getvalue()
        io.close
    
        req = OutgoingRequest(self.connection, body,{'Content-Type': 'application/octet-stream',
                                                     'User-Agent':  'PyBLIP',
                                                     'Date': datetime.now(),
                                                     'Size': size})
        req.compressed = randbool()
        req.urgent     = (random.randint(0,kUrgentEvery-1)==0)
        req.response.onComplete = self.gotResponse
        return req.send()
    
    def gotResponse(self, response):
        self.nRepliesPending -= 1
        logging.info("Got response!: %s (%i pending)",response,self.nRepliesPending)
        request = response.request
        assert response.body == request.body

    def testClient(self):
        lastReqTime = None
        nIterations = 0
        while nIterations < 10:
            asyncore.loop(timeout=kSendInterval,count=1)
            
            now = datetime.now()
            if self.connection.status!=kOpening and (not lastReqTime or (now-lastReqTime).microseconds >= kSendInterval*1.0e6):
                lastReqTime = now
                for i in xrange(0,kNBatchedMessages):
                    if not self.sendRequest():
                        logging.warn("Couldn't send request (connection is probably closed)")
                        break;
                    self.nRepliesPending += 1
                nIterations += 1
    
    def tearDown(self):
        self.connection.close()
        asyncore.loop() # got to give it time to negotiate close; this call should exit eventually
示例#2
0
class CloseTestPing(unittest.TestCase):
    def handleCloseRefusal(self, resp):
        logging.info("Close request was refused!")

    def setUp(self):
        self.connection = Connection(('localhost', 1337))
        self.connection.onCloseRefused = self.handleCloseRefusal

    def handleResponse(self, resp):
        logging.info("Got response...")

    def testClose(self):
        req = OutgoingRequest(self.connection, "Ping")
        req.response.onComplete = self.handleResponse
        req.send()

        asyncore.loop(timeout=0, count=5)  # give things time to send

        self.connection.close()

        asyncore.loop()
示例#3
0
class CloseTestPing(unittest.TestCase):
    
    def handleCloseRefusal(self, resp):
        logging.info("Close request was refused!")
    
    def setUp(self):
        self.connection = Connection( ('localhost', 1337) )
        self.connection.onCloseRefused = self.handleCloseRefusal
    
    def handleResponse(self, resp):
        logging.info("Got response...")
    
    def testClose(self):
        req = OutgoingRequest(self.connection, "Ping")
        req.response.onComplete = self.handleResponse
        req.send()
        
        asyncore.loop(timeout=0, count=5) # give things time to send
        
        self.connection.close()
        
        asyncore.loop()
示例#4
0
 def setUp(self):
     self.connection = Connection( ('localhost',46353) )
     self.nRepliesPending = 0
示例#5
0
 def setUp(self):
     self.connection = Connection(('localhost', 1337))
     self.connection.onCloseRefused = self.handleCloseRefusal
示例#6
0
 def setUp(self):
     self.connection = Connection( ('localhost', 1337) )
     self.connection.onCloseRefused = self.handleCloseRefusal