Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give more time in test__socket for server to start up #1836

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 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,29 +524,38 @@ 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 PORT
GPORT = None
def echoer():
global PORT
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))
PORT = s.getsockname()[1]
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, ())
time.sleep(1)
for _ in range(20):
time.sleep(0.5)
if GPORT is not None:
break

s = socket.socket()
s.connect(('localhost', PORT))
s.connect(('localhost', GPORT))
f1 = s.makefile('r')
f2 = s.makefile('w')
s.close()
Expand All @@ -558,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 @@ -593,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 @@ -623,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 @@ -645,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__)
Loading