Преглед на файлове

Simplify getFreePort() in integration/utils.py

jonathanvdc преди 8 години
родител
ревизия
50ac1f1307
променени са 1 файла, в които са добавени 6 реда и са изтрити 15 реда
  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):