Skip to content

Commit

Permalink
Use 127.0.0.1 and port 0 in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Fulton committed Jul 8, 2016
1 parent 1ce6fd7 commit 7e958f5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/ZEO/tests/ConnectionTests.py
Expand Up @@ -84,7 +84,7 @@ def setUp(self, before=None):
self.file = 'storage_conf'
self._servers = []
self.caches = []
self.addr = [('localhost', 0)]
self.addr = [('127.0.0.1', 0)]
self.startServer()

def tearDown(self):
Expand Down Expand Up @@ -127,7 +127,7 @@ def _newAddr(self):
self.addr.append(self._getAddr())

def _getAddr(self):
return 'localhost', forker.get_port(self)
return '127.0.0.1', forker.get_port(self)

def getConfig(self, path, create, read_only):
raise NotImplementedError
Expand Down Expand Up @@ -966,7 +966,7 @@ def checkTimeout(self):
self.assertRaises(ClientDisconnected, storage.tpc_finish, txn)

# Make sure it's logged as CRITICAL
for line in open("server-0.log"):
for line in open("server.log"):
if (('Transaction timeout after' in line) and
('CRITICAL ZEO.StorageServer' in line)
):
Expand Down
2 changes: 1 addition & 1 deletion src/ZEO/tests/client-config.test
Expand Up @@ -18,7 +18,7 @@ The simplest client configuration specified a server address:

>>> storage.getName(), storage.__class__.__name__
... # doctest: +ELLIPSIS
("[('localhost', ...)] (connected)", 'ClientStorage')
("[('127.0.0.1', ...)] (connected)", 'ClientStorage')

>>> storage.blob_dir
>>> storage._storage
Expand Down
46 changes: 8 additions & 38 deletions src/ZEO/tests/forker.py
Expand Up @@ -37,7 +37,7 @@ def __init__(self, addr, **options):
if isinstance(addr, str):
self.logpath = addr+'.log'
else:
self.logpath = 'server-%s.log' % addr[1]
self.logpath = 'server.log'
addr = '%s:%s' % addr
self.address = addr
self.read_only = None
Expand Down Expand Up @@ -195,10 +195,10 @@ def start_zeo_server(storage_conf=None, zeo_conf=None, port=None, keep=False,

if zeo_conf is None or isinstance(zeo_conf, dict):
if port is None:
raise AssertionError("The port wasn't specified")
port = 0

if isinstance(port, int):
addr = 'localhost', port
addr = '127.0.0.1', port
else:
addr = port

Expand Down Expand Up @@ -256,7 +256,7 @@ def _quote_arg(s):
def shutdown_zeo_server(stop):
stop()

def get_port(test=None):
def get_port(ignored=None):
"""Return a port that is not in use.
Checks if a port is in use by trying to connect to it. Assumes it
Expand All @@ -267,23 +267,20 @@ def get_port(test=None):
Raises RuntimeError after 10 tries.
"""

if test is not None:
return get_port2(test)

for i in range(10):
port = random.randrange(20000, 30000)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
try:
s.connect(('localhost', port))
s.connect(('127.0.0.1', port))
except socket.error:
pass # Perhaps we should check value of error too.
else:
continue

try:
s1.connect(('localhost', port+1))
s1.connect(('127.0.0.1', port+1))
except socket.error:
pass # Perhaps we should check value of error too.
else:
Expand All @@ -296,35 +293,11 @@ def get_port(test=None):
s1.close()
raise RuntimeError("Can't find port")

def get_port2(test):
for i in range(10):
while 1:
port = random.randrange(20000, 30000)
if port%3 == 0:
break

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('localhost', port+2))
except socket.error as e:
if e.args[0] != errno.EADDRINUSE:
raise
s.close()
continue

if not (can_connect(port) or can_connect(port+1)):
zope.testing.setupstack.register(test, s.close)
return port

s.close()

raise RuntimeError("Can't find port")

def can_connect(port):
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
try:
c.connect(('localhost', port))
c.connect(('127.0.0.1', port))
except socket.error:
return False # Perhaps we should check value of error too.
else:
Expand All @@ -346,7 +319,7 @@ def start_server(storage_conf=None, zeo_conf=None, port=None, keep=False,
"""
if port is None:
if addr is None:
port = get_port2(test)
port = 0
else:
port = addr[1]
elif addr is not None:
Expand All @@ -367,9 +340,6 @@ def start_server(storage_conf=None, zeo_conf=None, port=None, keep=False,

test.globs['start_server'] = start_server

def get_port():
return get_port2(test)

test.globs['get_port'] = get_port

def stop_server(stop):
Expand Down
15 changes: 6 additions & 9 deletions src/ZEO/tests/testZEO.py
Expand Up @@ -1025,7 +1025,7 @@ def dont_log_poskeyerrors_on_server():
>>> cs.close()
>>> stop_server(admin)
>>> with open('server-%s.log' % addr[1]) as f:
>>> with open('server.log') as f:
... 'POSKeyError' in f.read()
False
"""
Expand Down Expand Up @@ -1199,7 +1199,7 @@ def client_labels():
>>> db.close()
>>> @wait_until
... def check_for_test_label_1():
... with open('server-%s.log' % addr[1]) as f:
... with open('server.log') as f:
... for line in f:
... if 'test-label-1' in line:
... print(line.split()[1:4])
Expand All @@ -1220,11 +1220,10 @@ def client_labels():
>>> db.close()
>>> @wait_until
... def check_for_test_label_2():
... with open('server-%s.log' % addr[1]) as f:
... for line in open('server-%s.log' % addr[1]):
... if 'test-label-2' in line:
... print(line.split()[1:4])
... return True
... for line in open('server.log'):
... if 'test-label-2' in line:
... print(line.split()[1:4])
... return True
['INFO', 'ZEO.StorageServer', '(test-label-2']
"""
Expand Down Expand Up @@ -1520,7 +1519,6 @@ def __init__(self, name, blob_dir, shared=False, extrafsoptions=''):
else:
server_blob_dir = 'server-'+blob_dir
self.globs = {}
port = forker.get_port2(self)
addr, stop = forker.start_zeo_server(
"""
<blobstorage>
Expand All @@ -1531,7 +1529,6 @@ def __init__(self, name, blob_dir, shared=False, extrafsoptions=''):
</filestorage>
</blobstorage>
""" % (server_blob_dir, name+'.fs', extrafsoptions),
port=port,
)
zope.testing.setupstack.register(self, stop)
if shared:
Expand Down
4 changes: 2 additions & 2 deletions src/ZEO/tests/testssl.py
Expand Up @@ -298,7 +298,7 @@ def ssl_client(**ssl_settings):
"""%import ZEO
<clientstorage>
server localhost:0
server 127.0.0.1:0
{}
</clientstorage>
""".format(ssl_conf(**ssl_settings))
Expand All @@ -309,7 +309,7 @@ def create_server(**ssl_settings):
f.write(
"""
<zeo>
address localhost:0
address 127.0.0.1:0
{}
</zeo>
<mappingstorage>
Expand Down

0 comments on commit 7e958f5

Please sign in to comment.