import repository from arizona
[raven.git] / 2.0 / python / test / arizonacommTest.py
1 #! /usr/bin/env python\r
2 """\r
3 Stork Project (http://www.cs.arizona.edu/stork/)\r
4 Module: arizonacommTest.py\r
5 Description:   test module for arizonacomm.py\r
6 \r
7 Notes:\r
8    See arizonacomm.py for more details.\r
9 """\r
10 \r
11 import arizonacomm as commlib\r
12 import arizonaunittest as unittestlib\r
13 import os,sys,time,socket,random\r
14 from threading import Thread\r
15 \r
16 class async_run(Thread):\r
17         def __init__(self,method,arg1=None,arg2=None,arg3=None,arg4=None):\r
18                 self.method=method\r
19                 self.arg1,self.arg2,self.arg3,self.arg4=arg1,arg2,arg3,arg4\r
20                 self.ret=None\r
21                 self.done=False\r
22                 self.exception=None\r
23                 Thread.__init__(self)\r
24                 self.start()\r
25         def run(self):\r
26                 self.done=False\r
27                 arg1,arg2,arg3,arg4=self.arg1,self.arg2,self.arg3,self.arg4 
28                 #commented out to see the exceptions: 
29                 if True:#try:\r
30                         if arg4:self.ret=self.method(arg1,arg2,arg3,arg4)\r
31                         elif arg3:self.ret=self.method(arg1,arg2,arg3)\r
32                         elif arg2:self.ret=self.method(arg1,arg2)\r
33                         elif arg1:self.ret=self.method(arg1)\r
34                         else:self.ret=self.method()\r
35                 #except Exception,e:self.exception=str(e)+"\n"+str(sys.exc_info()[0])\r
36                 self.done=True\r
37         def wait(self,timeout=30):\r
38                 #wait till done\r
39                 tick=0\r
40                 while not self.done:\r
41                         time.sleep(1)\r
42                         tick+=1\r
43                         if tick>=timeout:break\r
44                 return self.done\r
45 test_instance=None\r
46 \r
47 def function_listentesthandler(addr1,addr2):\r
48         global test_instance\r
49         test_instance.listenreturn=(addr1,addr2)\r
50         \r
51 def function_handle_session_handler(data):\r
52         global test_instance\r
53         test_instance.handle_session_data=data\r
54 \r
55 class test(unittestlib.TestCase):\r
56         EOL='\r\n'\r
57         #test cases assume the type checking of arguments is correct\r
58         def test_connect(self):\r
59                 #try connecting to a hopefully nonexistant port\r
60                 self.assertException(IOError,commlib.connect,'127.0.0.1',6666)\r
61                 #test a non-existant host\r
62                 self.assertException(IOError,commlib.connect,'this.should.never.exist.edu',80)\r
63                 #test with a real socket\r
64                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
65                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
66                 hostport=random.randint(4000,60000)\r
67                 sock.bind(('127.0.0.1', hostport))\r
68                 sock.listen(5)\r
69                 sockaccept=async_run(sock.accept)\r
70                 try:commlib.connect('127.0.0.1',hostport)\r
71                 except:pass\r
72                 #wait for connection:\r
73                 sockaccept.wait(60)\r
74                 #if it's connected, then the accept should be finished as well\r
75                 self.assertFalse(commlib.glo_comm is None)\r
76                 self.assertTrue(type(sockaccept.ret)==tuple)\r
77                 #just to be sure\r
78                 print "Accepted Socket:",sockaccept.ret\r
79                 #close the socket\r
80                 try:commlib.disconnect()\r
81                 except:pass\r
82                 sock.close()\r
83         \r
84         def listentesthandler(self,addr1,addr2):\r
85                 self.listenreturn=(addr1,addr2)\r
86         \r
87         def test_listen(self):\r
88                 """NOTICE: the arizonacomm.listen method cannot be stopped, so this will leave threads running and require that\r
89                 the test be killed once completed.\r
90                 """\r
91                 global test_instance\r
92                 test_instance=self\r
93                 #test if it checks the min/max of the max_pending argument?\r
94                 # skip it - just passed through and the user is assumed to be smart enough to pick a valid value to send or use the default\r
95                 # also, using negative values does not seem to cause any exceptions, so below zero might be treated as zero\r
96                 \r
97                 #only test reasonably possible - test if it handles correct arguments as assumed to\r
98                 # done twice to test if accepting both types of 'functions'\r
99                 hostport=random.randint(4000,60000)\r
100                 self.listenreturn=None\r
101                 listen1 = async_run(commlib.listen,'127.0.0.1',hostport,function_listentesthandler)\r
102                 #connect:\r
103                 time.sleep(3) #ensure the listener started 
104                 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r
105                 sock.connect(('127.0.0.1',hostport))\r
106                 time.sleep(3) #to ensure the handler finished\r
107                 #assumed to have connected and closed itself\r
108                 self.assertFalse(listen1.done) #should still be going\r
109                 #check the return value\r
110                 self.assertTrue(type(self.listenreturn)==tuple)\r
111                 self.assertFalse(commlib.glo_comm is None)\r
112                 #second test\r
113                 self.listenreturn=None\r
114                 sock.close() 
115                 listen2 = async_run(commlib.listen,'127.0.0.1',hostport+1,self.listentesthandler)\r
116                 time.sleep(3) 
117                 #assume it accepted the method instead of function type\r
118                 #connect:\r
119                 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r
120                 sock.connect(('127.0.0.1',hostport+1))\r
121                 time.sleep(3) #to ensure the handler finished\r
122                 #assumed to have connected and closed itself\r
123                 self.assertFalse(listen2.done) #should still be going\r
124                 #check the return value\r
125                 self.assertTrue(type(self.listenreturn)==tuple)\r
126                 self.assertFalse(commlib.glo_comm is None)\r
127                 sock.close()\r
128         \r
129         def handle_session_handler(self,data):\r
130                 self.handle_session_data=data+"1"\r
131         def handle_session_handler2(self,data):\r
132                 self.handle_session_data=data+"2"\r
133         def handle_session_handler3(self,data):\r
134                 self.handle_session_data=data+'3'\r
135         \r
136         def socket_send(self,sock,txt):\r
137                 while len(txt)>0:\r
138                         sent=sock.send(txt)\r
139                         txt=txt[sent:]\r
140         \r
141         def test_handle_session(self):\r
142                 #this could be slightly complicated...\r
143                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
144                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
145                 hostport=random.randint(4000,60000)\r
146                 sock.bind(('127.0.0.1', hostport))\r
147                 sock.listen(5)\r
148                 host= async_run(sock.accept)\r
149                 commlib.connect('127.0.0.1',hostport)\r
150                 #wait for connection:\r
151                 host.wait(60)\r
152                 #check the length check of handle session (and assume it passes so it can continue)\r
153                 self.assertException(TypeError,commlib.handle_session,{'[Update':function_handle_session_handler},'[[')\r
154                 #assume that passing it methods instead of functions won't raise an exception\r
155                 handlesession=async_run(commlib.handle_session,{'[Test1':self.handle_session_handler,'[test1':self.handle_session_handler2,'[Test[3]':self.handle_session_handler3},'[')#using a different escape char here for testing\r
156                 #test 1\r
157                 self.handle_session_data=None\r
158                 self.socket_send(host.ret[0],'BigData[Test1'+self.EOL)\r
159                 time.sleep(3)
160                 self.assertEquals(self.handle_session_data,'BigData1')\r
161                 self.assertFalse(handlesession.done)\r
162                 #try sending the check_connection message
163                 commlib.check_connection()
164                 host.ret[0].sendall("####"+self.EOL) 
165                 host.ret[0].sendall("####"+self.EOL) 
166                 time.sleep(5)
167                 #ensure the connection is still live
168                 self.assertFalse(handlesession.done) 
169                 self.handle_session_data=None\r
170                 commlib.check_connection() 
171                 host.ret[0].sendall("####"+self.EOL) 
172                 host.ret[0].sendall("####"+self.EOL) 
173                 self.socket_send(host.ret[0],'  Big[[Data]\n[Test1'+self.EOL)\r
174                 time.sleep(3)\r
175                 self.assertFalse(handlesession.done)\r
176                 self.assertEquals(self.handle_session_data,'  Big[Data]\n1')\r
177                 #test 2\r
178                 self.handle_session_data=None\r
179                 self.socket_send(host.ret[0],'NiceTest[test1'+self.EOL)\r
180                 time.sleep(3)\r
181                 self.assertFalse(handlesession.done)\r
182                 self.assertEquals(self.handle_session_data,'NiceTest2')\r
183                 #test 3 # was a test for commands not starting with escape sequences.\r
184                 #self.handle_session_data=None\r
185                 #self.socket_send(host.ret[0],'TestData[Test3')\r
186                 #time.sleep(3)\r
187                 #self.assertFalse(handlesession.done)\r
188                 #self.assertTrue(handlesession.exception is None)\r
189                 #self.assertEquals(self.handle_session_data,'TestData3')\r
190                 #test 3.2 #test for commands containing escape sequences\r
191                 self.handle_session_data=None\r
192                 self.socket_send(host.ret[0],'TestData[Test[3]'+self.EOL)\r
193                 time.sleep(3)\r
194                 self.assertFalse(handlesession.done)\r
195                 self.assertTrue(handlesession.exception is None)\r
196                 self.assertEquals(self.handle_session_data,'TestData3')\r
197                 #test 4 #test for disconnecting when receiving a non-existant command.\r
198                 self.handle_session_data=None\r
199                 self.socket_send(host.ret[0],'TestData[Test89'+self.EOL)\r
200                 time.sleep(3)\r
201                 self.assertTrue(handlesession.done)\r
202                 self.assertTrue(self.handle_session_data is None)\r
203                 sock.close()\r
204 \r
205         def test_send(self):\r
206                 #\r
207                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
208                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
209                 hostport=random.randint(4000,60000)\r
210                 sock.bind(('127.0.0.1', hostport))\r
211                 sock.listen(5)\r
212                 sockaccept=async_run(sock.accept)\r
213                 commlib.connect('127.0.0.1',hostport)\r
214                 #wait for connection\r
215                 sockaccept.wait(60)\r
216                 #should be connected now.\r
217                 self.assertTrue(sockaccept.done) 
218                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
219                 commlib.send('[you','spin me','[')\r
220                 #wait for completion:\r
221                 receiver.wait(60)\r
222                 self.assertEqual(receiver.ret,'spin me[[you'+self.EOL)\r
223                 \r
224                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
225                 commlib.send('[right','round [baby]','[')\r
226                 #wait for completion:\r
227                 receiver.wait(60)\r
228                 self.assertEqual(receiver.ret,'round [[baby][[right'+self.EOL)\r
229                 \r
230                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
231                 commlib.send('[right',' round [like] [a] record[baby]\n','[')\r
232                 #wait for completion:\r
233                 receiver.wait(60)\r
234                 self.assertEqual(receiver.ret,' round [[like] [[a] record[[baby]\n[[right'+self.EOL)\r
235                 \r
236                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
237                 commlib.send('[right [round]','round round','[')\r
238                 #wait for completion:\r
239                 receiver.wait(60)\r
240                 self.assertEqual(receiver.ret,'round round[[right [round]'+self.EOL)\r
241                 \r
242                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
243                 commlib.send('[you [spin]\n','me [right] round [baby]','[')\r
244                 #wait for completion:\r
245                 receiver.wait(60)\r
246                 self.assertEqual(receiver.ret,'me [[right] round [[baby][[you [spin]\n'+self.EOL)\r
247                 \r
248         def test_disconnect(self):\r
249                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
250                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
251                 hostport=random.randint(4000,60000) #assumedly with a large range, this won't conflict with anything that might still be listening\r
252                 sock.bind(('127.0.0.1', hostport))\r
253                 sock.listen(5)\r
254                 sockaccept=async_run(sock.accept)\r
255                 commlib.connect('127.0.0.1',hostport)\r
256                 #wait for connection:\r
257                 sockaccept.wait(60)\r
258                 self.assertFalse(commlib.glo_comm is None)\r
259                 commlib.send("nothing","nothing","n")\r
260                 commlib.disconnect()\r
261                 self.assertTrue(commlib.glo_stop)\r
262 \r
263         ## check_connection test ##\r
264         \r
265         def test_check_connection(self):\r
266                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
267                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
268                 hostport=random.randint(4000,60000) #assumedly with a large range, this won't conflict with anything that might still be listening\r
269                 sock.bind(('127.0.0.1', hostport))\r
270                 sock.listen(5)\r
271                 #test 1:\r
272                 sockaccept=async_run(sock.accept)\r
273                 commlib.connect('127.0.0.1',hostport)\r
274                 #wait for connection:\r
275                 sockaccept.wait(60)\r
276                 self.assertTrue(commlib.check_connection())\r
277                 #test 1.2:\r
278                 sockaccept.ret[0].close()\r
279                 self.assertFalse(commlib.check_connection())\r
280                 #test 1.3:\r
281                 commlib.disconnect()\r
282                 self.assertFalse(commlib.check_connection())\r
283                 #test 2:\r
284                 sockaccept=async_run(sock.accept)\r
285                 sock2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r
286                 sock2.connect(('127.0.0.1',hostport))\r
287                 #wait for connection:\r
288                 sockaccept.wait(60)\r
289                 sock2.setblocking(0)\r
290                 self.assertTrue(commlib.check_connection(sock2,True))\r
291                 #test 2.1:\r
292                 sockaccept.ret[0].close()\r
293                 self.assertFalse(commlib.check_connection(sock2,True))\r
294                 #test 2.2:\r
295                 sock2.close()\r
296                 self.assertFalse(commlib.check_connection(sock2,True))\r
297                 #tes 2.3:\r
298                 self.assertFalse(commlib.check_connection(None,True))\r
299                 \r
300         ## single_conn tests ##\r
301         \r
302         def test_single_conn_connect(self):\r
303                 #create a single_conn\r
304                 sconn=commlib.single_conn('127.0.0.1',6666,{'[Update':self.handle_session_handler},None,'[',False,True)\r
305                 #try connecting to a hopefully nonexistant port\r
306                 self.assertFalse(sconn.connect('127.0.0.1',6666))\r
307                 #test a non-existant host\r
308                 self.assertFalse(sconn.connect('this.should.never.exist.edu',80))\r
309                 #test with a real socket\r
310                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
311                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
312                 hostport=random.randint(4000,60000)\r
313                 sock.bind(('127.0.0.1', hostport))\r
314                 sock.listen(5)\r
315                 sockaccept=async_run(sock.accept)\r
316                 self.assertTrue(sconn.connect('127.0.0.1',hostport))\r
317                 #wait for connection:\r
318                 sockaccept.wait(60)\r
319                 #if it's connected, then the accept should be finished as well\r
320                 self.assertTrue(sconn.connected)\r
321                 self.assertTrue(type(sockaccept.ret)==tuple)\r
322                 #just to be sure\r
323                 print "Accepted Socket:",sockaccept.ret\r
324                 #close the socket\r
325                 sconn.stop()\r
326                 sock.close()\r
327         \r
328         def test_single_conn_disconnect(self):\r
329                 #create a single_conn\r
330                 sconn=commlib.single_conn('127.0.0.1',6666,{'[Update':self.handle_session_handler},None,'[',False,True)\r
331 \r
332                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
333                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
334                 hostport=random.randint(4000,60000) #assumedly with a large range, this won't conflict with anything that might still be listening\r
335                 sock.bind(('127.0.0.1', hostport))\r
336                 sock.listen(5)\r
337                 sockaccept=async_run(sock.accept)\r
338                 self.assertTrue(sconn.connect('127.0.0.1',hostport))\r
339                 #wait for connection:\r
340                 sockaccept.wait(60)\r
341                 sconn.send('nothing','nothing','n')\r
342                 sconn.disconnect()\r
343                 self.assertFalse(sconn.connected)\r
344                 #would test to ensure that sconn.sock is None, but that could be assuming too much about the inner workings\r
345                 sock.close()\r
346 \r
347         def test_single_conn_send(self):\r
348                 #create a single_conn\r
349                 sconn=commlib.single_conn('127.0.0.1',6666,{'[Update':self.handle_session_handler},None,'[',False,True)\r
350 \r
351                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
352                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
353                 hostport=random.randint(4000,60000)\r
354                 sock.bind(('127.0.0.1', hostport))\r
355                 sock.listen(5)\r
356                 sockaccept=async_run(sock.accept)\r
357                 self.assertTrue(sconn.connect('127.0.0.1',hostport))\r
358                 #wait for connection:\r
359                 sockaccept.wait(60)\r
360                 #should be connected now.\r
361                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
362                 sconn.send('[you','spin me','[')\r
363                 #wait for completion:
364                 receiver.wait()
365                 self.assertEqual(receiver.ret,'spin me[you'+self.EOL)\r
366                 \r
367                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
368                 sconn.send('[right','round [baby]','[')\r
369                 #wait for completion:\r
370                 receiver.wait()\r
371                 self.assertEqual(receiver.ret,'round [[baby][right'+self.EOL)\r
372                 \r
373                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
374                 sconn.send('[right',' round [like] [a] record[baby]\n','[')\r
375                 #wait for completion:\r
376                 receiver.wait()\r
377                 self.assertEqual(receiver.ret,' round [[like] [[a] record[[baby]\n[right'+self.EOL)\r
378                 \r
379                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
380                 sconn.send('[right [round]','round round','[')\r
381                 #wait for completion:\r
382                 receiver.wait()\r
383                 self.assertEqual(receiver.ret,'round round[right [[round]'+self.EOL)\r
384                 \r
385                 receiver=async_run(sockaccept.ret[0].recv,4096)\r
386                 sconn.send('[you [spin]\n','me [right] round [baby]','[')\r
387                 #wait for completion:\r
388                 receiver.wait()\r
389                 self.assertEqual(receiver.ret,'me [[right] round [[baby][you [[spin]\n'+self.EOL)\r
390                 sconn.stop()\r
391                 sock.close()\r
392                 \r
393         def test_single_conn_recv(self):\r
394                 #create a single_conn\r
395                 sconn=commlib.single_conn('127.0.0.1',6666,{'[Update':self.handle_session_handler},None,'[',False,True)\r
396 \r
397                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
398                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
399                 hostport=random.randint(4000,60000)\r
400                 sock.bind(('127.0.0.1', hostport))\r
401                 sock.listen(5)\r
402                 sockaccept=async_run(sock.accept)\r
403                 self.assertTrue(sconn.connect('127.0.0.1',hostport))\r
404                 #give it a minute to connect:
405                 sockaccept.wait(60)
406                 #should be connected now. - send a couple messages.\r
407                 receiver=async_run(sconn.recv,4096)\r
408                 testtxt='THIS IS A TEST.  If This were a real emergency...\n'\r
409                 sockaccept.ret[0].send(testtxt)\r
410                 #wait for completion:\r
411                 receiver.wait()\r
412                 self.assertEqual(receiver.ret,testtxt)\r
413                 \r
414                 receiver=async_run(sconn.recv,4096)\r
415                 testtxt='If you are reading this, thank a teacher... or the internet...'\r
416                 sockaccept.ret[0].send(testtxt)\r
417                 #wait for completion:\r
418                 receiver.wait()\r
419                 self.assertEqual(receiver.ret,testtxt)\r
420                 sconn.stop()\r
421                 sock.close()\r
422 \r
423         def test_single_conn_default_handler(self):\r
424                 #create a single_conn\r
425                 sconn=commlib.single_conn('127.0.0.1',6666,{'[Update':self.handle_session_handler},None,'[',False,True)\r
426 \r
427                 #this could be slightly complicated...\r
428                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
429                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
430                 hostport=random.randint(4000,60000)\r
431                 sock.bind(('127.0.0.1', hostport))\r
432                 sock.listen(5)\r
433                 host= async_run(sock.accept)\r
434                 self.assertTrue(sconn.connect('127.0.0.1',hostport))\r
435                 #wait for connection\r
436                 host.wait(60)\r
437                 #check the length check of handle session (and assume it passes so it can continue)\r
438                 self.assertException(TypeError,sconn.default_handler,{'[Update':function_handle_session_handler},None,'[[')\r
439                 #assume that passing it methods instead of functions won't raise an exception\r
440                 handlesession=async_run(sconn.default_handler,{'[Test1':self.handle_session_handler,'[test1':self.handle_session_handler2,'[Test[3]':self.handle_session_handler3},None,'[')#using a different escape char here for testing\r
441                 #test 1\r
442                 self.handle_session_data=None\r
443                 self.socket_send(host.ret[0],'BigData[Test1'+self.EOL)\r
444                 time.sleep(3)\r
445                 self.assertEquals(self.handle_session_data,'BigData1')\r
446                 self.assertFalse(handlesession.done)\r
447                 #test 1.2\r
448                 self.handle_session_data=None\r
449                 self.socket_send(host.ret[0],'  Big[[Data]\n[Test1'+self.EOL)\r
450                 time.sleep(3)\r
451                 self.assertFalse(handlesession.done)\r
452                 self.assertEquals(self.handle_session_data,'  Big[Data]\n1')\r
453                 #test 2\r
454                 self.handle_session_data=None\r
455                 self.socket_send(host.ret[0],'NiceTest[test1'+self.EOL)\r
456                 time.sleep(3)\r
457                 self.assertFalse(handlesession.done)\r
458                 self.assertEquals(self.handle_session_data,'NiceTest2')\r
459                 #test 3 # was a test for commands not starting with escape sequences.\r
460                 #self.handle_session_data=None\r
461                 #self.socket_send(host.ret[0],'TestData[Test3')\r
462                 #time.sleep(3)\r
463                 #self.assertFalse(handlesession.done)\r
464                 #self.assertTrue(handlesession.exception is None)\r
465                 #self.assertEquals(self.handle_session_data,'TestData3')\r
466                 #test 3.2 #test for commands containing escape sequences\r
467                 self.handle_session_data=None\r
468                 self.socket_send(host.ret[0],'TestData[Test[[3]'+self.EOL)\r
469                 time.sleep(3)\r
470                 self.assertFalse(handlesession.done)\r
471                 self.assertTrue(handlesession.exception is None)\r
472                 self.assertEquals(self.handle_session_data,'TestData3')\r
473                 #test 4 #test for disconnecting when receiving a non-existant command.\r
474                 self.handle_session_data=None\r
475                 self.socket_send(host.ret[0],'TestData[Test89'+self.EOL)\r
476                 time.sleep(3)\r
477                 self.assertTrue(handlesession.done)\r
478                 self.assertTrue(self.handle_session_data is None)\r
479                 sock.close()\r
480                 sconn.stop()\r
481 \r
482         def test_single_conn_run(self):\r
483                 hostport=random.randint(4000,60000)\r
484                 #create a single_conn\r
485                 sconn=commlib.single_conn('127.0.0.1',hostport,{'[Update':self.handle_session_handler},None,'[',False,True)\r
486                 sconn.retrysleep=10 #10 seconds between tries\r
487 \r
488                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
489                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r
490                 sock.bind(('127.0.0.1', hostport))\r
491                 sock.listen(5)\r
492                 sockaccept=async_run(sock.accept)\r
493                 sconn.start()\r
494                 #wait for connection:\r
495                 sockaccept.wait(60)\r
496                 self.assertTrue(sconn.connected)\r
497                 sockaccept.ret[0].close()\r
498                 time.sleep(5)\r
499                 #wont work since it will automatically attempt to reconnect 
500                 #self.assertFalse(sconn.connected)\r
501                 sockaccept=async_run(sock.accept)\r
502                 #wait for connection:\r
503                 sockaccept.wait(60)\r
504                 #should be reconnected\r
505                 self.assertTrue(sconn.connected)\r
506                 self.assertTrue(sockaccept.done and not sockaccept.ret is None)\r
507                 sock.close()\r
508                 sconn.stop()\r
509 \r
510         ## listener tests ##\r
511         def test_listener_run(self):\r
512                 hostport=random.randint(4000,60000)\r
513                 #create a listener\r
514                 lst=commlib.listener(hostport,None,None,5,False)\r
515 \r
516                 lstrun=async_run(lst.run)\r
517                 self.assertFalse(lstrun.done)\r
518                 time.sleep(3)#ensure the listener started 
519                 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r
520                 sock.connect(('127.0.0.1',hostport))\r
521                 time.sleep(3)\r
522                 self.assertTrue(len(lst.connlist)>0)\r
523                 self.assertEqual(len(lst.connlist),1)\r
524 \r
525                 sock.close()\r
526                 lst.default_list_check()\r
527                 self.assertEqual(len(lst.connlist),0)\r
528                 #stop test\r
529                 lst.stop()\r
530                 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r
531                 sock.connect(('127.0.0.1',hostport))\r
532                 self.assertTrue(lst.done)\r
533                 time.sleep(3) 
534                 self.assertTrue(lstrun.done)             \r
535                 \r
536 if __name__=='__main__':\r
537         unittestlib.main(test)