Skip to content

Commit

Permalink
Fix test__socket, again… (#1849)
Browse files Browse the repository at this point in the history
* Fix test__socket, again...

* Clean up test
  • Loading branch information
BCSharp authored Dec 23, 2024
1 parent e104794 commit 7a6252b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Tests/modules/network_related/test__socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time
import unittest

from iptest import IronPythonTestCase, is_cli, is_osx, is_linux, is_windows, is_cpython, run_test
from iptest import IronPythonTestCase, is_cli, is_mono, is_osx, is_linux, is_windows, is_cpython, run_test

AF_DICT = {"AF_APPLETALK" : 5,
"AF_DECnet" : 12,
Expand Down Expand Up @@ -432,7 +432,6 @@ def test_cp5814(self):
HOST = 'localhost'
PORT = 0
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
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(20) # prevents the server from staying open if the client never connects
s.bind((HOST, PORT))
s.listen(1)
Expand Down Expand Up @@ -534,15 +533,14 @@ def test_misc(self):
def test_makefile_refcount(self):
"Ensures that the _socket stays open while there's still a file associated"

GPORT = None
PORT = None
def echoer():
nonlocal GPORT
nonlocal PORT
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)
PORT = s.getsockname()[1]
(s2, _) = s.accept()
s2.send(s2.recv(10))
s2.close()
Expand All @@ -551,11 +549,16 @@ def echoer():
_thread.start_new_thread(echoer, ())
for _ in range(20):
time.sleep(0.5)
if GPORT is not None:
if PORT is not None:
break

if is_mono:
# Warm up Mono to connecting sockets
dummy = socket.socket()
s = socket.socket()
s.connect(('localhost', GPORT))
if is_mono:
dummy.close()
s.connect(('localhost', PORT))
f1 = s.makefile('r')
f2 = s.makefile('w')
s.close()
Expand All @@ -582,7 +585,6 @@ def test_cp7451(self):
HOST = 'localhost'
PORT = 0
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
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(20) # prevents the server from staying open if the client never connects
s.bind((HOST, PORT))
s.listen(1)
Expand Down

0 comments on commit 7a6252b

Please sign in to comment.