Skip to content

Commit

Permalink
Clean up resources and speed up test
Browse files Browse the repository at this point in the history
  • Loading branch information
slozier committed Dec 10, 2024
1 parent be22ed9 commit e36bfb2
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions Tests/modules/network_related/test__socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,13 @@ def test_getfqdn(self):
pass

def test_cp5814(self):
global EXIT_CODE
global HAS_EXITED
EXIT_CODE = -1
HAS_EXITED = False

portFile = os.path.join(self.temporary_dir, "cp5814port_%d" % os.getpid())

#Server code
server = """
from time import sleep
import _socket
import os
Expand All @@ -454,21 +451,21 @@ 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:
pass
""".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' %
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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')
Expand All @@ -563,17 +565,17 @@ 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

portFile = os.path.join(self.temporary_dir, "cp7451port_%d" % os.getpid())

#Server code
server = """
from time import sleep
import socket as _socket
import os
Expand All @@ -598,21 +600,21 @@ 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:
pass
""".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' %
Expand All @@ -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:
Expand All @@ -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__)

0 comments on commit e36bfb2

Please sign in to comment.