Commit 15d72c07 authored by Jim Fulton's avatar Jim Fulton

Updated the mechanism to select test server ports to remove a source

of intermittent test failures.  In ConnectionTests, a random port was
selected without checking if it was in use.  testZEO.get_port (moved
to forker) picked a random port, checking if it was in use, but
clients actually used that port *and* the following one.  Now check
that the returned and subsequent ports are free. (Of course, they
could get used betweed the time they're selected and the time they are
used by the test. Oh well.
parent c7967efe
......@@ -158,8 +158,7 @@ class CommonSetupTearDown(StorageTestBase):
self.addr.append(self._getAddr())
def _getAddr(self):
# port+1 is also used, so only draw even port numbers
return 'localhost', random.randrange(25000, 30000, 2)
return 'localhost', forker.get_port()
def getConfig(self, path, create, read_only):
raise NotImplementedError
......
......@@ -14,6 +14,7 @@
"""Library for forking storage server and connecting client storage"""
import os
import random
import sys
import time
import errno
......@@ -201,3 +202,29 @@ def shutdown_zeo_server(adminaddr):
ack = 'no ack received'
logger.debug('shutdown_zeo_server(): acked: %s' % ack)
s.close()
def get_port():
"""Return a port that is not in use.
Checks if a port is in use by trying to connect to it. Assumes it
is not in use if connect raises an exception. We actually look for
2 consective free ports because most of the clients of this
function will use the returned port and the next one.
Raises RuntimeError after 10 tries.
"""
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))
s1.connect(('localhost', port+1))
except socket.error:
# Perhaps we should check value of error too.
return port
finally:
s.close()
s1.close()
raise RuntimeError("Can't find port")
......@@ -18,9 +18,7 @@ import asyncore
import doctest
import logging
import os
import random
import signal
import socket
import stat
import tempfile
import threading
......@@ -50,6 +48,7 @@ from ZEO.ClientStorage import ClientStorage
import ZEO.zrpc.connection
from ZEO.tests import forker, Cache, CommitLockTests, ThreadTests
from ZEO.tests.forker import get_port
import ZEO.tests.ConnectionTests
......@@ -128,27 +127,6 @@ class MiscZEOTests:
finally:
storage2.close()
def get_port():
"""Return a port that is not in use.
Checks if a port is in use by trying to connect to it. Assumes it
is not in use if connect raises an exception.
Raises RuntimeError after 10 tries.
"""
for i in range(10):
port = random.randrange(20000, 30000)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
try:
s.connect(('localhost', port))
except socket.error:
# Perhaps we should check value of error too.
return port
finally:
s.close()
raise RuntimeError("Can't find port")
class GenericTests(
# Base class for all ZODB tests
StorageTestBase.StorageTestBase,
......
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