Patchwork [2,of,3] run-tests: refactor port allocation into functions

login
register
mail settings
Submitter timeless@mozdev.org
Date Feb. 17, 2016, 7:43 p.m.
Message ID <9043cf6f4c845593820c.1455738189@waste.org>
Download mbox | patch
Permalink /patch/13248/
State Accepted
Headers show

Comments

timeless@mozdev.org - Feb. 17, 2016, 7:43 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1455737792 0
#      Wed Feb 17 19:36:32 2016 +0000
# Node ID 9043cf6f4c845593820cfa3e8715b198159ffc44
# Parent  7db2ab69bf18b79ba1df9d2b350720de3373a043
run-tests: refactor port allocation into functions

Adding a port reservation was too hard and someone did it wrong.
By refactoring, such reservations can be managed more safely.

This also adds documentation so that the next person who tries
is more likely to update all the places correctly.

Note that in this commit the reservation and consumers do not
match, that will be fixed in the next commit.

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -708,6 +708,10 @@ 
         """Terminate execution of this test."""
         self._aborted = True
 
+    def _portmap(self, i):
+        offset = '' if i == 0 else '%s' % i
+        return (br':%d\b' % (self._startport + i), b':$HGPORT%s' % offset)
+
     def _getreplacements(self):
         """Obtain a mapping of text replacements to apply to test output.
 
@@ -716,11 +720,12 @@ 
         occur.
         """
         r = [
-            (br':%d\b' % self._startport, b':$HGPORT'),
-            (br':%d\b' % (self._startport + 1), b':$HGPORT1'),
-            (br':%d\b' % (self._startport + 2), b':$HGPORT2'),
-            (br':%d\b' % (self._startport + 2), b':$HGPORT3'),
-            (br':%d\b' % (self._startport + 2), b':$HGPORT4'),
+            # This list should be parallel to defineport in _getenv
+            self._portmap(0),
+            self._portmap(1),
+            self._portmap(2),
+            self._portmap(3),
+            self._portmap(4),
             (br'(?m)^(saved backup bundle to .*\.hg)( \(glob\))?$',
              br'\1 (glob)'),
             ]
@@ -737,14 +742,18 @@ 
 
     def _getenv(self):
         """Obtain environment variables to use during test execution."""
+        def defineport(i):
+            offset = '' if i == 0 else '%s' % i
+            env["HGPORT%s" % offset] = '%s' % (self._startport + i)
         env = os.environ.copy()
         env['TESTTMP'] = self._testtmp
         env['HOME'] = self._testtmp
-        env["HGPORT"] = str(self._startport)
-        env["HGPORT1"] = str(self._startport + 1)
-        env["HGPORT2"] = str(self._startport + 2)
-        env["HGPORT3"] = str(self._startport + 3)
-        env["HGPORT4"] = str(self._startport + 4)
+        # This number should match portneeded in _getport
+        # XXX currently it does not, this is a bug that will be fixed
+        # in the next commit.
+        for port in xrange(5):
+            # This list should be parallel to _portmap in _getreplacements
+            defineport(port)
         env["HGRCPATH"] = os.path.join(self._threadtmp, b'.hgrc')
         env["DAEMON_PIDS"] = os.path.join(self._threadtmp, b'daemon.pids')
         env["HGEDITOR"] = ('"' + sys.executable + '"'