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