[Python-checkins] r60338 - python/trunk/Lib/test/test_asynchat.py

neal.norwitz python-checkins at python.org
Sun Jan 27 02:44:05 CET 2008


Author: neal.norwitz
Date: Sun Jan 27 02:44:05 2008
New Revision: 60338

Modified:
   python/trunk/Lib/test/test_asynchat.py
Log:
Eliminate the sleeps that assume the server will start in .5 seconds.
This should make the test less flaky.  It also speeds up the test
by about 75% on my box (20+ seconds -> ~4 seconds).


Modified: python/trunk/Lib/test/test_asynchat.py
==============================================================================
--- python/trunk/Lib/test/test_asynchat.py	(original)
+++ python/trunk/Lib/test/test_asynchat.py	Sun Jan 27 02:44:05 2008
@@ -15,12 +15,17 @@
     # client each send
     chunk_size = 1
 
+    def __init__(self, event):
+        threading.Thread.__init__(self)
+        self.event = event
+
     def run(self):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         global PORT
         PORT = test_support.bind_port(sock, HOST, PORT)
         sock.listen(1)
+        self.event.set()
         conn, client = sock.accept()
         self.buffer = ""
         # collect data until quit message is seen
@@ -74,6 +79,16 @@
         self.buffer = ""
 
 
+def start_echo_server():
+    event = threading.Event()
+    s = echo_server(event)
+    s.start()
+    event.wait()
+    event.clear()
+    time.sleep(0.01) # Give server time to start accepting.
+    return s, event
+
+
 class TestAsynchat(unittest.TestCase):
     usepoll = False
 
@@ -84,10 +99,13 @@
         pass
 
     def line_terminator_check(self, term, server_chunk):
-        s = echo_server()
+        event = threading.Event()
+        s = echo_server(event)
         s.chunk_size = server_chunk
         s.start()
-        time.sleep(0.5) # Give server time to initialize
+        event.wait()
+        event.clear()
+        time.sleep(0.01) # Give server time to start accepting.
         c = echo_client(term)
         c.push("hello ")
         c.push("world%s" % term)
@@ -119,9 +137,7 @@
 
     def numeric_terminator_check(self, termlen):
         # Try reading a fixed number of bytes
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client(termlen)
         data = "hello world, I'm not dead yet!\n"
         c.push(data)
@@ -142,9 +158,7 @@
 
     def test_none_terminator(self):
         # Try reading a fixed number of bytes
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client(None)
         data = "hello world, I'm not dead yet!\n"
         c.push(data)
@@ -156,9 +170,7 @@
         self.assertEqual(c.buffer, data)
 
     def test_simple_producer(self):
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client('\n')
         data = "hello world\nI'm not dead yet!\n"
         p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
@@ -169,9 +181,7 @@
         self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
 
     def test_string_producer(self):
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client('\n')
         data = "hello world\nI'm not dead yet!\n"
         c.push_with_producer(data+SERVER_QUIT)
@@ -182,9 +192,7 @@
 
     def test_empty_line(self):
         # checks that empty lines are handled correctly
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client('\n')
         c.push("hello world\n\nI'm not dead yet!\n")
         c.push(SERVER_QUIT)
@@ -194,9 +202,7 @@
         self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
 
     def test_close_when_done(self):
-        s = echo_server()
-        s.start()
-        time.sleep(0.5) # Give server time to initialize
+        s, event = start_echo_server()
         c = echo_client('\n')
         c.push("hello world\nI'm not dead yet!\n")
         c.push(SERVER_QUIT)


More information about the Python-checkins mailing list