Commit 0ae725dd authored by Grégory Wisniewski's avatar Grégory Wisniewski

Generate randomly the listening ports.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2350 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 47917d49
......@@ -19,6 +19,7 @@ import os
import sys
import time
import ZODB
import socket
import signal
import random
import MySQLdb
......@@ -160,8 +161,7 @@ class NEOProcess(object):
class NEOCluster(object):
def __init__(self, db_list, master_node_count=1,
partitions=1, replicas=0, port_base=10000,
def __init__(self, db_list, master_node_count=1, partitions=1, replicas=0,
db_user='neo', db_password='neo',
db_super_user=DB_ADMIN, db_super_password=DB_PASSWD,
cleanup_on_delete=False, temp_dir=None,
......@@ -178,7 +178,7 @@ class NEOCluster(object):
if clear_databases:
self.setupDB()
self.process_dict = {}
self.last_port = port_base
self.port_set = set()
if temp_dir is None:
temp_dir = tempfile.mkdtemp(prefix='neo_')
print 'Using temp directory %r.' % (temp_dir, )
......@@ -234,9 +234,21 @@ class NEOCluster(object):
NEOProcess(command, uuid, arguments))
def __allocatePort(self):
port = self.last_port
self.last_port += 1
return port
for i in range(10):
port = random.randrange(30000, 40000)
if port in self.port_set:
continue
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.
self.port_set.add(port)
return port
finally:
s.close()
raise RuntimeError, "Can't find port"
def __allocateUUID(self):
uuid = os.urandom(16)
......
......@@ -68,7 +68,6 @@ class ClientTests(NEOFunctionalTest):
NEOFunctionalTest.setUp(self)
self.neo = NEOCluster(
['test_neo1', 'test_neo2', 'test_neo3', 'test_neo4'],
port_base=20000,
replicas=2,
master_node_count=1,
temp_dir=self.getTempDirectory()
......
......@@ -32,7 +32,7 @@ class ClusterTests(NEOFunctionalTest):
self.neo.stop()
def testClusterBreaks(self):
self.neo = NEOCluster(['test_neo1'], port_base=20000,
self.neo = NEOCluster(['test_neo1'],
master_node_count=1, temp_dir=self.getTempDirectory())
neoctl = self.neo.getNEOCTL()
self.neo.setupDB()
......@@ -43,7 +43,7 @@ class ClusterTests(NEOFunctionalTest):
self.neo.expectClusterVerifying()
def testClusterBreaksWithTwoNodes(self):
self.neo = NEOCluster(['test_neo1', 'test_neo2'], port_base=20000,
self.neo = NEOCluster(['test_neo1', 'test_neo2'],
partitions=2, master_node_count=1, replicas=0,
temp_dir=self.getTempDirectory())
neoctl = self.neo.getNEOCTL()
......@@ -55,7 +55,7 @@ class ClusterTests(NEOFunctionalTest):
self.neo.expectClusterVerifying()
def testClusterDoesntBreakWithTwoNodesOneReplica(self):
self.neo = NEOCluster(['test_neo1', 'test_neo2'], port_base=20000,
self.neo = NEOCluster(['test_neo1', 'test_neo2'],
partitions=2, replicas=1, master_node_count=1,
temp_dir=self.getTempDirectory())
neoctl = self.neo.getNEOCTL()
......@@ -68,7 +68,7 @@ class ClusterTests(NEOFunctionalTest):
def testElectionWithManyMasters(self):
MASTER_COUNT = 20
self.neo = NEOCluster(['test_neo1', 'test_neo2'], port_base=20000,
self.neo = NEOCluster(['test_neo1', 'test_neo2'],
partitions=10, replicas=0, master_node_count=MASTER_COUNT,
temp_dir=self.getTempDirectory())
neoctl = self.neo.getNEOCTL()
......
......@@ -26,8 +26,7 @@ class MasterTests(NEOFunctionalTest):
def setUp(self):
NEOFunctionalTest.setUp(self)
self.neo = NEOCluster([], port_base=20000,
master_node_count=MASTER_NODE_COUNT,
self.neo = NEOCluster([], master_node_count=MASTER_NODE_COUNT,
temp_dir=self.getTempDirectory())
self.neo.stop()
self.neo.start()
......
......@@ -49,7 +49,7 @@ class StorageTests(NEOFunctionalTest):
partitions=10, master_node_count=2):
# create a neo cluster
self.neo = NEOCluster(['test_neo%d' % i for i in xrange(storage_number)],
port_base=20000, master_node_count=master_node_count,
master_node_count=master_node_count,
partitions=partitions, replicas=replicas,
temp_dir=self.getTempDirectory(),
clear_databases=True,
......
......@@ -23,8 +23,7 @@ class ZODBTestCase(NEOFunctionalTest):
def setUp(self):
NEOFunctionalTest.setUp(self)
self.neo = NEOCluster(['test_neo1'],
partitions=1, replicas=0, port_base=20000,
self.neo = NEOCluster(['test_neo1'], partitions=1, replicas=0,
master_node_count=1, temp_dir=self.getTempDirectory())
self.neo.stop()
self.neo.setupDB()
......
......@@ -32,8 +32,7 @@ class RecoveryTests(ZODBTestCase, StorageTestBase, RecoveryStorage):
dst_temp_dir = self.getTempDirectory() + '-dst'
if not os.path.exists(dst_temp_dir):
os.makedirs(dst_temp_dir)
self.neo_dst = NEOCluster(['test_neo1-dst'],
partitions=1, replicas=0, port_base=10000,
self.neo_dst = NEOCluster(['test_neo1-dst'], partitions=1, replicas=0,
master_node_count=1, temp_dir=dst_temp_dir)
self.neo_dst.stop()
self.neo_dst.setupDB()
......
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