Issue #13453: Fix a race condition in test_poplib.

parent 10db4dec
...@@ -309,32 +309,34 @@ class TestTimeouts(TestCase): ...@@ -309,32 +309,34 @@ class TestTimeouts(TestCase):
def setUp(self): def setUp(self):
self.evt = threading.Event() self.evt = threading.Event()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(3) self.sock.settimeout(60) # Safety net. Look issue 11812
self.port = test_support.bind_port(self.sock) self.port = test_support.bind_port(self.sock)
threading.Thread(target=self.server, args=(self.evt,self.sock)).start() self.thread = threading.Thread(target=self.server, args=(self.evt,self.sock))
time.sleep(.1) self.thread.setDaemon(True)
self.thread.start()
self.evt.wait()
def tearDown(self): def tearDown(self):
self.evt.wait() self.thread.join()
del self.thread # Clear out any dangling Thread objects.
def server(self, evt, serv): def server(self, evt, serv):
serv.listen(5) serv.listen(5)
evt.set()
try: try:
conn, addr = serv.accept() conn, addr = serv.accept()
except socket.timeout:
pass
else:
conn.send(b"+ Hola mundo\n") conn.send(b"+ Hola mundo\n")
conn.close() conn.close()
except socket.timeout:
pass
finally: finally:
serv.close() serv.close()
evt.set()
def testTimeoutDefault(self): def testTimeoutDefault(self):
self.assertTrue(socket.getdefaulttimeout() is None) self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30) socket.setdefaulttimeout(30)
try: try:
pop = poplib.POP3("localhost", self.port) pop = poplib.POP3(HOST, self.port)
finally: finally:
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
self.assertEqual(pop.sock.gettimeout(), 30) self.assertEqual(pop.sock.gettimeout(), 30)
...@@ -351,7 +353,7 @@ class TestTimeouts(TestCase): ...@@ -351,7 +353,7 @@ class TestTimeouts(TestCase):
pop.sock.close() pop.sock.close()
def testTimeoutValue(self): def testTimeoutValue(self):
pop = poplib.POP3("localhost", self.port, timeout=30) pop = poplib.POP3(HOST, self.port, timeout=30)
self.assertEqual(pop.sock.gettimeout(), 30) self.assertEqual(pop.sock.gettimeout(), 30)
pop.sock.close() pop.sock.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment