Explorar o código

Simplify getFreePort() in integration/utils.py

jonathanvdc %!s(int64=8) %!d(string=hai) anos
pai
achega
50ac1f1307
Modificáronse 1 ficheiros con 6 adicións e 15 borrados
  1. 6 15
      integration/utils.py

+ 6 - 15
integration/utils.py

@@ -19,25 +19,16 @@ from check_objects import to_recompile
 username = "test_user"
 parallel_push = True
 
-ports = []
+ports = set()
 
 def getFreePort():
+    """Gets a unique new port."""
     while 1:
         port = random.randint(10000, 20000)
-        ports.append(port)
-
-        exists = False
-        for p in ports:
-            if p == port:
-                if not exists:
-                    # We have hopefully found our own
-                    exists = True
-                else:
-                    # We seem to be the second entry, so chose another one
-                    ports.remove(port)
-                    break
-        else:
-            # Didn't find a duplicate
+        # Check if this port is in the set of ports.
+        if port not in ports:
+            # We have found a unique port. Add it to the set and return.
+            ports.add(port)
             return port
 
 def execute(scriptname, parameters=[], wait=False):