Skip to content
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
50 changes: 28 additions & 22 deletions tests/suite/modules/network_related/test__socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,19 @@ def test_cp5814(self):

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

#Server code
# Server code
server = """
import _socket
import os

HOST = 'localhost'
PORT = 0
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
s.settimeout(20) # prevents the server from staying open if the client never connects
s.settimeout(90) # prevents the server from staying open if the client never connects
s.bind((HOST, PORT))
s.listen(1)

conn = None
try:
with open(r"{PORTFILE}", "w") as f:
print(s.getsockname()[1], file=f)
Expand All @@ -471,14 +472,15 @@ def test_cp5814(self):
raise Exception('%s != stuff' % str(data))

finally:
conn.close()
if conn is not None:
conn.close()
s.close()
try:
os.remove(r"{PORTFILE}")
except:
pass
""".format(PORTFILE=portFile)
#Spawn off a thread to startup the server
# Spawn off a thread to startup the server
def server_thread():
nonlocal EXIT_CODE
nonlocal HAS_EXITED
Expand All @@ -493,13 +495,14 @@ def server_thread():
pass

_thread.start_new_thread(server_thread, ())
#Give the server a chance to startup
# Give the server a chance to startup
portex = None
startTime = time.perf_counter()
for _ in range(20):
# Wait a minute since starting a new process can be slow (e.g. Mono during CI)
while time.perf_counter() - startTime < 60:
time.sleep(0.5)
if EXIT_CODE > 0:
self.fail("Server died with exit code %d" % EXIT_CODE)
if HAS_EXITED:
self.fail("Server exited early with exit code %d" % EXIT_CODE)
try:
with open(portFile) as f:
PORT = int(f.read())
Expand All @@ -510,15 +513,15 @@ def server_thread():
duration = time.perf_counter() - startTime
self.fail("Server not detected after trying for %g s, last detection attempt resulted in %r" % (duration, portex))

#Client
# Client
HOST = 'localhost'
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(b"stuff")
data, addr = s.recvfrom(1024)
s.close()

#Ensure the server didn't die
# Ensure the server didn't die
for _ in range(100):
if HAS_EXITED:
self.assertEqual(EXIT_CODE, 0)
Expand All @@ -529,7 +532,7 @@ def server_thread():

self.assertTrue(HAS_EXITED)

#Verification
# Verification
self.assertEqual(data, b"stuff")
if is_cli:
self.assertEqual(addr[0], "0.0.0.0")
Expand Down Expand Up @@ -594,18 +597,19 @@ def test_cp7451(self):

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

#Server code
# Server code
server = """
import socket as _socket
import os

HOST = 'localhost'
PORT = 0
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
s.settimeout(20) # prevents the server from staying open if the client never connects
s.settimeout(90) # prevents the server from staying open if the client never connects
s.bind((HOST, PORT))
s.listen(1)

conn = None
try:
with open(r"{PORTFILE}", "w") as f:
print(s.getsockname()[1], file=f)
Expand All @@ -623,14 +627,15 @@ def test_cp7451(self):
raise Exception('%s != stuff2' % str(data))

finally:
conn.close()
if conn is not None:
conn.close()
s.close()
try:
os.remove(r"{PORTFILE}")
except:
pass
""".format(PORTFILE=portFile)
#Spawn off a thread to startup the server
# Spawn off a thread to startup the server
def server_thread():
nonlocal EXIT_CODE
nonlocal HAS_EXITED
Expand All @@ -645,13 +650,14 @@ def server_thread():
pass

_thread.start_new_thread(server_thread, ())
#Give the server a chance to startup
# Give the server a chance to startup
portex = None
startTime = time.perf_counter()
for _ in range(20):
# Wait a minute since starting a new process can be slow (e.g. Mono during CI)
while time.perf_counter() - startTime < 60:
time.sleep(0.5)
if EXIT_CODE > 0:
self.fail("Server died with exit code %d" % EXIT_CODE)
if HAS_EXITED:
self.fail("Server exited early with exit code %d" % EXIT_CODE)
try:
with open(portFile) as f:
PORT = int(f.read())
Expand All @@ -662,15 +668,15 @@ def server_thread():
duration = time.perf_counter() - startTime
self.fail("Server not detected after trying for %g s, last detection attempt resulted in %r" % (duration, portex))

#Client
# Client
HOST = 'localhost'
s = socket.socket()
s.connect((HOST, PORT))
s.send(b"stuff2")
f = s.makefile()
s.close()

#Ensure the server didn't die
# Ensure the server didn't die
for _ in range(100):
if HAS_EXITED:
self.assertEqual(EXIT_CODE, 0)
Expand All @@ -681,7 +687,7 @@ def server_thread():

self.assertTrue(HAS_EXITED)

#Verification
# Verification
self.assertEqual(f.read(6), "stuff2")

f.close()
Expand Down
Loading