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