Commit 7e958f5b authored by Jim Fulton's avatar Jim Fulton

Use 127.0.0.1 and port 0 in more places

parent 1ce6fd78
...@@ -84,7 +84,7 @@ class CommonSetupTearDown(StorageTestBase): ...@@ -84,7 +84,7 @@ class CommonSetupTearDown(StorageTestBase):
self.file = 'storage_conf' self.file = 'storage_conf'
self._servers = [] self._servers = []
self.caches = [] self.caches = []
self.addr = [('localhost', 0)] self.addr = [('127.0.0.1', 0)]
self.startServer() self.startServer()
def tearDown(self): def tearDown(self):
...@@ -127,7 +127,7 @@ class CommonSetupTearDown(StorageTestBase): ...@@ -127,7 +127,7 @@ class CommonSetupTearDown(StorageTestBase):
self.addr.append(self._getAddr()) self.addr.append(self._getAddr())
def _getAddr(self): 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): def getConfig(self, path, create, read_only):
raise NotImplementedError raise NotImplementedError
...@@ -966,7 +966,7 @@ class TimeoutTests(CommonSetupTearDown): ...@@ -966,7 +966,7 @@ class TimeoutTests(CommonSetupTearDown):
self.assertRaises(ClientDisconnected, storage.tpc_finish, txn) self.assertRaises(ClientDisconnected, storage.tpc_finish, txn)
# Make sure it's logged as CRITICAL # 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 if (('Transaction timeout after' in line) and
('CRITICAL ZEO.StorageServer' in line) ('CRITICAL ZEO.StorageServer' in line)
): ):
......
...@@ -18,7 +18,7 @@ The simplest client configuration specified a server address: ...@@ -18,7 +18,7 @@ The simplest client configuration specified a server address:
>>> storage.getName(), storage.__class__.__name__ >>> storage.getName(), storage.__class__.__name__
... # doctest: +ELLIPSIS ... # doctest: +ELLIPSIS
("[('localhost', ...)] (connected)", 'ClientStorage') ("[('127.0.0.1', ...)] (connected)", 'ClientStorage')
>>> storage.blob_dir >>> storage.blob_dir
>>> storage._storage >>> storage._storage
......
...@@ -37,7 +37,7 @@ class ZEOConfig: ...@@ -37,7 +37,7 @@ class ZEOConfig:
if isinstance(addr, str): if isinstance(addr, str):
self.logpath = addr+'.log' self.logpath = addr+'.log'
else: else:
self.logpath = 'server-%s.log' % addr[1] self.logpath = 'server.log'
addr = '%s:%s' % addr addr = '%s:%s' % addr
self.address = addr self.address = addr
self.read_only = None self.read_only = None
...@@ -195,10 +195,10 @@ def start_zeo_server(storage_conf=None, zeo_conf=None, port=None, keep=False, ...@@ -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 zeo_conf is None or isinstance(zeo_conf, dict):
if port is None: if port is None:
raise AssertionError("The port wasn't specified") port = 0
if isinstance(port, int): if isinstance(port, int):
addr = 'localhost', port addr = '127.0.0.1', port
else: else:
addr = port addr = port
...@@ -256,7 +256,7 @@ else: ...@@ -256,7 +256,7 @@ else:
def shutdown_zeo_server(stop): def shutdown_zeo_server(stop):
stop() stop()
def get_port(test=None): def get_port(ignored=None):
"""Return a port that is not in use. """Return a port that is not in use.
Checks if a port is in use by trying to connect to it. Assumes it Checks if a port is in use by trying to connect to it. Assumes it
...@@ -267,23 +267,20 @@ def get_port(test=None): ...@@ -267,23 +267,20 @@ def get_port(test=None):
Raises RuntimeError after 10 tries. Raises RuntimeError after 10 tries.
""" """
if test is not None:
return get_port2(test)
for i in range(10): for i in range(10):
port = random.randrange(20000, 30000) port = random.randrange(20000, 30000)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
try: try:
s.connect(('localhost', port)) s.connect(('127.0.0.1', port))
except socket.error: except socket.error:
pass # Perhaps we should check value of error too. pass # Perhaps we should check value of error too.
else: else:
continue continue
try: try:
s1.connect(('localhost', port+1)) s1.connect(('127.0.0.1', port+1))
except socket.error: except socket.error:
pass # Perhaps we should check value of error too. pass # Perhaps we should check value of error too.
else: else:
...@@ -296,35 +293,11 @@ def get_port(test=None): ...@@ -296,35 +293,11 @@ def get_port(test=None):
s1.close() s1.close()
raise RuntimeError("Can't find port") 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): def can_connect(port):
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
try: try:
c.connect(('localhost', port)) c.connect(('127.0.0.1', port))
except socket.error: except socket.error:
return False # Perhaps we should check value of error too. return False # Perhaps we should check value of error too.
else: else:
...@@ -346,7 +319,7 @@ def setUp(test): ...@@ -346,7 +319,7 @@ def setUp(test):
""" """
if port is None: if port is None:
if addr is None: if addr is None:
port = get_port2(test) port = 0
else: else:
port = addr[1] port = addr[1]
elif addr is not None: elif addr is not None:
...@@ -367,9 +340,6 @@ def setUp(test): ...@@ -367,9 +340,6 @@ def setUp(test):
test.globs['start_server'] = start_server test.globs['start_server'] = start_server
def get_port():
return get_port2(test)
test.globs['get_port'] = get_port test.globs['get_port'] = get_port
def stop_server(stop): def stop_server(stop):
......
...@@ -1025,7 +1025,7 @@ def dont_log_poskeyerrors_on_server(): ...@@ -1025,7 +1025,7 @@ def dont_log_poskeyerrors_on_server():
>>> cs.close() >>> cs.close()
>>> stop_server(admin) >>> stop_server(admin)
>>> with open('server-%s.log' % addr[1]) as f: >>> with open('server.log') as f:
... 'POSKeyError' in f.read() ... 'POSKeyError' in f.read()
False False
""" """
...@@ -1199,7 +1199,7 @@ constructor. ...@@ -1199,7 +1199,7 @@ constructor.
>>> db.close() >>> db.close()
>>> @wait_until >>> @wait_until
... def check_for_test_label_1(): ... 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: ... for line in f:
... if 'test-label-1' in line: ... if 'test-label-1' in line:
... print(line.split()[1:4]) ... print(line.split()[1:4])
...@@ -1220,11 +1220,10 @@ You can specify the client label via a configuration file as well: ...@@ -1220,11 +1220,10 @@ You can specify the client label via a configuration file as well:
>>> db.close() >>> db.close()
>>> @wait_until >>> @wait_until
... def check_for_test_label_2(): ... def check_for_test_label_2():
... with open('server-%s.log' % addr[1]) as f: ... for line in open('server.log'):
... for line in open('server-%s.log' % addr[1]): ... if 'test-label-2' in line:
... if 'test-label-2' in line: ... print(line.split()[1:4])
... print(line.split()[1:4]) ... return True
... return True
['INFO', 'ZEO.StorageServer', '(test-label-2'] ['INFO', 'ZEO.StorageServer', '(test-label-2']
""" """
...@@ -1520,7 +1519,6 @@ class ServerManagingClientStorage(ClientStorage): ...@@ -1520,7 +1519,6 @@ class ServerManagingClientStorage(ClientStorage):
else: else:
server_blob_dir = 'server-'+blob_dir server_blob_dir = 'server-'+blob_dir
self.globs = {} self.globs = {}
port = forker.get_port2(self)
addr, stop = forker.start_zeo_server( addr, stop = forker.start_zeo_server(
""" """
<blobstorage> <blobstorage>
...@@ -1531,7 +1529,6 @@ class ServerManagingClientStorage(ClientStorage): ...@@ -1531,7 +1529,6 @@ class ServerManagingClientStorage(ClientStorage):
</filestorage> </filestorage>
</blobstorage> </blobstorage>
""" % (server_blob_dir, name+'.fs', extrafsoptions), """ % (server_blob_dir, name+'.fs', extrafsoptions),
port=port,
) )
zope.testing.setupstack.register(self, stop) zope.testing.setupstack.register(self, stop)
if shared: if shared:
......
...@@ -298,7 +298,7 @@ def ssl_client(**ssl_settings): ...@@ -298,7 +298,7 @@ def ssl_client(**ssl_settings):
"""%import ZEO """%import ZEO
<clientstorage> <clientstorage>
server localhost:0 server 127.0.0.1:0
{} {}
</clientstorage> </clientstorage>
""".format(ssl_conf(**ssl_settings)) """.format(ssl_conf(**ssl_settings))
...@@ -309,7 +309,7 @@ def create_server(**ssl_settings): ...@@ -309,7 +309,7 @@ def create_server(**ssl_settings):
f.write( f.write(
""" """
<zeo> <zeo>
address localhost:0 address 127.0.0.1:0
{} {}
</zeo> </zeo>
<mappingstorage> <mappingstorage>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment