[Neo-report] r2372 vincent - /trunk/neo/tests/functional/__init__.py

nobody at svn.erp5.org nobody at svn.erp5.org
Sat Oct 30 00:12:05 CEST 2010


Author: vincent
Date: Sat Oct 30 00:12:04 2010
New Revision: 2372

Log:
Use kernel facilities to find free ports.

This is most efficient (kernel knows what's available) & cleaner.

Of course, this is not perfect either. Still, it solved many "port already
in use" happening for me with previous implementation, for some reason.

Modified:
    trunk/neo/tests/functional/__init__.py

Modified: trunk/neo/tests/functional/__init__.py
==============================================================================
--- trunk/neo/tests/functional/__init__.py [iso-8859-1] (original)
+++ trunk/neo/tests/functional/__init__.py [iso-8859-1] Sat Oct 30 00:12:04 2010
@@ -224,21 +224,17 @@ class NEOCluster(object):
             NEOProcess(command, uuid, arguments))
 
     def __allocatePort(self):
-        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"
+        port_set = self.port_set
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        while True:
+            s.bind(('127.0.0.1', 0))
+            port = s.getsockname()[1]
+            if port not in port_set:
+                break
+        s.close()
+        port_set.add(port)
+        return port
 
     def __allocateUUID(self):
         uuid = os.urandom(16)





More information about the Neo-report mailing list