From be22ed95e49e27a58c61993d014a5063e9949a21 Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Mon, 9 Dec 2024 16:51:50 -0800 Subject: [PATCH 1/2] Give more time in test__socket for server to start up --- Tests/modules/network_related/test__socket.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Tests/modules/network_related/test__socket.py b/Tests/modules/network_related/test__socket.py index 626be3d50..b202d5c21 100644 --- a/Tests/modules/network_related/test__socket.py +++ b/Tests/modules/network_related/test__socket.py @@ -533,22 +533,27 @@ def test_misc(self): def test_makefile_refcount(self): "Ensures that the _socket stays open while there's still a file associated" - global PORT + global GPORT + if 'GPORT' in globals(): + del GPORT def echoer(): - global PORT + global GPORT s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # prevents an "Address already in use" error when the socket is in a TIME_WAIT state s.settimeout(15) # prevents the server from staying open if the client never connects s.bind(('localhost', 0)) - PORT = s.getsockname()[1] + GPORT = s.getsockname()[1] s.listen(5) (s2, ignore) = s.accept() s2.send(s2.recv(10)) _thread.start_new_thread(echoer, ()) - time.sleep(1) + for _ in range(20): + time.sleep(1) + if 'GPORT' in globals(): + break s = socket.socket() - s.connect(('localhost', PORT)) + s.connect(('localhost', GPORT)) f1 = s.makefile('r') f2 = s.makefile('w') s.close() From e36bfb2c57018cfcc13ac5fb5d022afc28bf1603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Mon, 9 Dec 2024 21:51:04 -0500 Subject: [PATCH 2/2] Clean up resources and speed up test --- Tests/modules/network_related/test__socket.py | 77 ++++++++++--------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/Tests/modules/network_related/test__socket.py b/Tests/modules/network_related/test__socket.py index b202d5c21..ce1ba4548 100644 --- a/Tests/modules/network_related/test__socket.py +++ b/Tests/modules/network_related/test__socket.py @@ -419,8 +419,6 @@ def test_getfqdn(self): pass def test_cp5814(self): - global EXIT_CODE - global HAS_EXITED EXIT_CODE = -1 HAS_EXITED = False @@ -428,7 +426,6 @@ def test_cp5814(self): #Server code server = """ -from time import sleep import _socket import os @@ -454,12 +451,12 @@ def test_cp5814(self): #Verifications if not addr[0] in [HOST, '127.0.0.1']: raise Exception('The address, %s, was unexpected' % str(addr)) - if data!=b'stuff': - raise Exception('%s!=stuff' % str(data)) - sleep(10) + if data != b'stuff': + raise Exception('%s != stuff' % str(data)) finally: conn.close() + s.close() try: os.remove(r"{PORTFILE}") except: @@ -467,8 +464,8 @@ def test_cp5814(self): """.format(PORTFILE=portFile) #Spawn off a thread to startup the server def server_thread(): - global EXIT_CODE - global HAS_EXITED + nonlocal EXIT_CODE + nonlocal HAS_EXITED serverFile = os.path.join(self.temporary_dir, "cp5814server_%d.py" % os.getpid()) self.write_to_file(serverFile, server) EXIT_CODE = os.system('"%s" %s' % @@ -484,7 +481,7 @@ def server_thread(): portex = None startTime = time.perf_counter() for _ in range(20): - time.sleep(1) + time.sleep(0.5) if EXIT_CODE > 0: self.fail("Server died with exit code %d" % EXIT_CODE) try: @@ -506,13 +503,14 @@ def server_thread(): s.close() #Ensure the server didn't die - for i in range(100): - if not HAS_EXITED: - print("*", end="") - time.sleep(1) - else: + for _ in range(100): + if HAS_EXITED: self.assertEqual(EXIT_CODE, 0) break + + print("*", end="") + time.sleep(0.5) + self.assertTrue(HAS_EXITED) #Verification @@ -526,32 +524,36 @@ def server_thread(): class SocketMakefileTest(IronPythonTestCase): def test_misc(self): - f = socket.socket().makefile() + s = socket.socket() + f = s.makefile() f.bufsize = 4096 self.assertEqual(4096, f.bufsize) + f.close() + s.close() def test_makefile_refcount(self): "Ensures that the _socket stays open while there's still a file associated" - global GPORT - if 'GPORT' in globals(): - del GPORT + GPORT = None def echoer(): - global GPORT + nonlocal GPORT s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # prevents an "Address already in use" error when the socket is in a TIME_WAIT state s.settimeout(15) # prevents the server from staying open if the client never connects s.bind(('localhost', 0)) GPORT = s.getsockname()[1] s.listen(5) - (s2, ignore) = s.accept() + (s2, _) = s.accept() s2.send(s2.recv(10)) + s2.close() + s.close() _thread.start_new_thread(echoer, ()) for _ in range(20): - time.sleep(1) - if 'GPORT' in globals(): + time.sleep(0.5) + if GPORT is not None: break + s = socket.socket() s.connect(('localhost', GPORT)) f1 = s.makefile('r') @@ -563,9 +565,10 @@ def echoer(): str = f1.readline() self.assertEqual(str, test_msg) + f2.close() + f1.close() + def test_cp7451(self): - global EXIT_CODE - global HAS_EXITED EXIT_CODE = -1 HAS_EXITED = False @@ -573,7 +576,6 @@ def test_cp7451(self): #Server code server = """ -from time import sleep import socket as _socket import os @@ -598,12 +600,12 @@ def test_cp7451(self): #Verifications if not addr[0] in [HOST, '127.0.0.1']: raise Exception('The address, %s, was unexpected' % str(addr)) - if data!=b'stuff2': - raise Exception('%s!=stuff2' % str(data)) - sleep(10) + if data != b'stuff2': + raise Exception('%s != stuff2' % str(data)) finally: conn.close() + s.close() try: os.remove(r"{PORTFILE}") except: @@ -611,8 +613,8 @@ def test_cp7451(self): """.format(PORTFILE=portFile) #Spawn off a thread to startup the server def server_thread(): - global EXIT_CODE - global HAS_EXITED + nonlocal EXIT_CODE + nonlocal HAS_EXITED serverFile = os.path.join(self.temporary_dir, "cp7451server_%d.py" % os.getpid()) self.write_to_file(serverFile, server) EXIT_CODE = os.system('"%s" %s' % @@ -628,7 +630,7 @@ def server_thread(): portex = None startTime = time.perf_counter() for _ in range(20): - time.sleep(1) + time.sleep(0.5) if EXIT_CODE > 0: self.fail("Server died with exit code %d" % EXIT_CODE) try: @@ -650,16 +652,19 @@ def server_thread(): s.close() #Ensure the server didn't die - for i in range(100): - if not HAS_EXITED: - print("*", end="") - time.sleep(1) - else: + for _ in range(100): + if HAS_EXITED: self.assertEqual(EXIT_CODE, 0) break + + print("*", end="") + time.sleep(0.5) + self.assertTrue(HAS_EXITED) #Verification self.assertEqual(f.read(6), "stuff2") + f.close() + run_test(__name__)