[Python-checkins] r56604 - python/trunk/Lib/test/test_asyncore.py

facundo.batista python-checkins at python.org
Sat Jul 28 16:21:22 CEST 2007


Author: facundo.batista
Date: Sat Jul 28 16:21:22 2007
New Revision: 56604

Modified:
   python/trunk/Lib/test/test_asyncore.py
Log:

Moved all of the capture_server socket setup code into the try block
so that the event gets set if a failure occurs during server setup
(otherwise the test will block forever).  Changed to let the OS assign
the server port number, and client side of test waits for port number
assignment before proceeding. The test data in DispatcherWithSendTests
is also sent in multiple send() calls instead of one to make sure this
works properly. [GSoC - Alan McIntyre]


Modified: python/trunk/Lib/test/test_asyncore.py
==============================================================================
--- python/trunk/Lib/test/test_asyncore.py	(original)
+++ python/trunk/Lib/test/test_asyncore.py	Sat Jul 28 16:21:22 2007
@@ -12,7 +12,7 @@
 from StringIO import StringIO
 
 HOST = "127.0.0.1"
-PORT = 54329
+PORT = None
 
 class dummysocket:
     def __init__(self):
@@ -53,12 +53,14 @@
 
 # used when testing senders; just collects what it gets until newline is sent
 def capture_server(evt, buf):
-    serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    serv.settimeout(3)
-    serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-    serv.bind(("", PORT))
-    serv.listen(5)
     try:
+        serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        serv.settimeout(3)
+        serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        serv.bind(("", 0))
+        global PORT
+        PORT = serv.getsockname()[1]
+        serv.listen(5)
         conn, addr = serv.accept()
     except socket.timeout:
         pass
@@ -78,6 +80,7 @@
         conn.close()
     finally:
         serv.close()
+        PORT = None
         evt.set()
 
 
@@ -338,12 +341,26 @@
         self.evt = threading.Event()
         cap = StringIO()
         threading.Thread(target=capture_server, args=(self.evt,cap)).start()
-        time.sleep(1) # Give server time to initialize
 
-        data = "Suppose there isn't a 16-ton weight?"*5
+        # wait until server thread has assigned a port number
+        n = 1000
+        while PORT is None and n > 0:
+            time.sleep(0.01)
+            n -= 1
+
+        # wait a little longer for the server to initialize (it sometimes
+        # refuses connections on slow machines without this wait)
+        time.sleep(0.2)
+
+        data = "Suppose there isn't a 16-ton weight?"
         d = dispatcherwithsend_noread()
         d.create_socket(socket.AF_INET, socket.SOCK_STREAM)
         d.connect((HOST, PORT))
+
+        # give time for socket to connect
+        time.sleep(0.1)
+
+        d.send(data)
         d.send(data)
         d.send('\n')
 
@@ -354,7 +371,7 @@
 
         self.evt.wait()
 
-        self.assertEqual(cap.getvalue(), data)
+        self.assertEqual(cap.getvalue(), data*2)
 
 
 class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):


More information about the Python-checkins mailing list