mirror of
https://github.com/python/cpython.git
synced 2025-12-04 16:43:27 +00:00
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).
This commit is contained in:
parent
3d785e2c6a
commit
6e070814b2
1 changed files with 26 additions and 20 deletions
|
|
@ -15,12 +15,17 @@ class echo_server(threading.Thread):
|
||||||
# client each send
|
# client each send
|
||||||
chunk_size = 1
|
chunk_size = 1
|
||||||
|
|
||||||
|
def __init__(self, event):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.event = event
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
global PORT
|
global PORT
|
||||||
PORT = test_support.bind_port(sock, HOST, PORT)
|
PORT = test_support.bind_port(sock, HOST, PORT)
|
||||||
sock.listen(1)
|
sock.listen(1)
|
||||||
|
self.event.set()
|
||||||
conn, client = sock.accept()
|
conn, client = sock.accept()
|
||||||
self.buffer = ""
|
self.buffer = ""
|
||||||
# collect data until quit message is seen
|
# collect data until quit message is seen
|
||||||
|
|
@ -74,6 +79,16 @@ class echo_client(asynchat.async_chat):
|
||||||
self.buffer = ""
|
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):
|
class TestAsynchat(unittest.TestCase):
|
||||||
usepoll = False
|
usepoll = False
|
||||||
|
|
||||||
|
|
@ -84,10 +99,13 @@ class TestAsynchat(unittest.TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def line_terminator_check(self, term, server_chunk):
|
def line_terminator_check(self, term, server_chunk):
|
||||||
s = echo_server()
|
event = threading.Event()
|
||||||
|
s = echo_server(event)
|
||||||
s.chunk_size = server_chunk
|
s.chunk_size = server_chunk
|
||||||
s.start()
|
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 = echo_client(term)
|
||||||
c.push("hello ")
|
c.push("hello ")
|
||||||
c.push("world%s" % term)
|
c.push("world%s" % term)
|
||||||
|
|
@ -119,9 +137,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
|
|
||||||
def numeric_terminator_check(self, termlen):
|
def numeric_terminator_check(self, termlen):
|
||||||
# Try reading a fixed number of bytes
|
# Try reading a fixed number of bytes
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client(termlen)
|
c = echo_client(termlen)
|
||||||
data = "hello world, I'm not dead yet!\n"
|
data = "hello world, I'm not dead yet!\n"
|
||||||
c.push(data)
|
c.push(data)
|
||||||
|
|
@ -142,9 +158,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
|
|
||||||
def test_none_terminator(self):
|
def test_none_terminator(self):
|
||||||
# Try reading a fixed number of bytes
|
# Try reading a fixed number of bytes
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client(None)
|
c = echo_client(None)
|
||||||
data = "hello world, I'm not dead yet!\n"
|
data = "hello world, I'm not dead yet!\n"
|
||||||
c.push(data)
|
c.push(data)
|
||||||
|
|
@ -156,9 +170,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
self.assertEqual(c.buffer, data)
|
self.assertEqual(c.buffer, data)
|
||||||
|
|
||||||
def test_simple_producer(self):
|
def test_simple_producer(self):
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client('\n')
|
c = echo_client('\n')
|
||||||
data = "hello world\nI'm not dead yet!\n"
|
data = "hello world\nI'm not dead yet!\n"
|
||||||
p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
|
p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
|
||||||
|
|
@ -169,9 +181,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
|
self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
|
||||||
|
|
||||||
def test_string_producer(self):
|
def test_string_producer(self):
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client('\n')
|
c = echo_client('\n')
|
||||||
data = "hello world\nI'm not dead yet!\n"
|
data = "hello world\nI'm not dead yet!\n"
|
||||||
c.push_with_producer(data+SERVER_QUIT)
|
c.push_with_producer(data+SERVER_QUIT)
|
||||||
|
|
@ -182,9 +192,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
|
|
||||||
def test_empty_line(self):
|
def test_empty_line(self):
|
||||||
# checks that empty lines are handled correctly
|
# checks that empty lines are handled correctly
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client('\n')
|
c = echo_client('\n')
|
||||||
c.push("hello world\n\nI'm not dead yet!\n")
|
c.push("hello world\n\nI'm not dead yet!\n")
|
||||||
c.push(SERVER_QUIT)
|
c.push(SERVER_QUIT)
|
||||||
|
|
@ -194,9 +202,7 @@ class TestAsynchat(unittest.TestCase):
|
||||||
self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
|
self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
|
||||||
|
|
||||||
def test_close_when_done(self):
|
def test_close_when_done(self):
|
||||||
s = echo_server()
|
s, event = start_echo_server()
|
||||||
s.start()
|
|
||||||
time.sleep(0.5) # Give server time to initialize
|
|
||||||
c = echo_client('\n')
|
c = echo_client('\n')
|
||||||
c.push("hello world\nI'm not dead yet!\n")
|
c.push("hello world\nI'm not dead yet!\n")
|
||||||
c.push(SERVER_QUIT)
|
c.push(SERVER_QUIT)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue