diff options
authorSZEDER Gábor <>2019-01-05 01:08:58 (GMT)
committerJunio C Hamano <>2019-01-07 17:24:06 (GMT)
commitfa840581805c794e93e03a83a944839f15ba08d0 (patch)
parent61f292db4e1df5b4e871cefffff3e9ec01ef2ade (diff)
test-lib-functions: introduce the 'test_set_port' helper function
Several test scripts run daemons like 'git-daemon' or Apache, and communicate with them through TCP sockets. To have unique ports where these daemons are accessible, the ports are usually the number of the corresponding test scripts, unless the user overrides them via environment variables, and thus all those tests and test libs contain more or less the same bit of one-liner boilerplate code to find out the port. The last patch in this series will make this a bit more complicated. Factor out finding the port for a daemon into the common helper function 'test_set_port' to avoid repeating ourselves. Take special care of test scripts with "low" numbers: - Test numbers below 1024 would result in a port that's only usable as root, so set their port to '10000 + test-nr' to make sure it doesn't interfere with other tests in the test suite. This makes the hardcoded port number in '' unnecessary, remove it. - The shell's arithmetic evaluation interprets numbers with leading zeros as octal values, which means that test number below 1000 and containing the digits 8 or 9 will trigger an error. Remove all leading zeros from the test numbers to prevent this. Note that the 'git p4' tests are unlike the other tests involving daemons in that: - '' doesn't use the test's number for unique port as is, but does a bit of additional arithmetic on top [1]. - The port is not overridable via an environment variable. With this patch even 'git p4' tests will use the test's number as default port, and it will be overridable via the P4DPORT environment variable. [1] Commit fc00233071 (git-p4 tests: refactor and cleanup, 2011-08-22) introduced that "unusual" unique port computation without explaining why it was necessary (as opposed to simply using the test number as is). It seems to be just unnecessary complication, and in any case that commit came way before the "test nr as unique port" got "standardized" for other daemons in commits c44132fcf3 (tests: auto-set git-daemon port, 2014-02-10), 3bb486e439 (tests: auto-set LIB_HTTPD_PORT from test name, 2014-02-10), and bf9d7df950 (t/ improve svnserve tests with parallel make test, 2017-12-01). Signed-off-by: SZEDER Gábor <> Signed-off-by: Junio C Hamano <>
7 files changed, 41 insertions, 13 deletions
diff --git a/t/ b/t/
index f98de95..41eb1e3 100644
--- a/t/
+++ b/t/
@@ -28,7 +28,7 @@ then
test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
+test_set_port LIB_GIT_DAEMON_PORT
diff --git a/t/ b/t/
index c275994..b3be3ba 100644
--- a/t/
+++ b/t/
@@ -53,14 +53,7 @@ time_in_seconds () {
(cd / && "$PYTHON_PATH" -c 'import time; print(int(time.time()))')
-# Try to pick a unique port: guess a large number, then hope
-# no more than one of each test is running.
-# This does not handle the case where somebody else is running the
-# same tests and has chosen the same ports.
-P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
+test_set_port P4DPORT
diff --git a/t/ b/t/
index a8130f9..f3b478c 100644
--- a/t/
+++ b/t/
@@ -13,6 +13,7 @@ fi
+test_set_port SVNSERVE_PORT
svn >/dev/null 2>&1
if test $? -ne 1
@@ -119,7 +120,6 @@ require_svnserve () {
start_svnserve () {
svnserve --listen-port $SVNSERVE_PORT \
--root "$rawsvnrepo" \
--listen-once \
diff --git a/t/ b/t/
index a8729f8..e465116 100644
--- a/t/
+++ b/t/
@@ -82,7 +82,7 @@ case $(uname) in
+test_set_port LIB_HTTPD_PORT
diff --git a/t/ b/t/
index ba3887f..0aca8d7 100755
--- a/t/
+++ b/t/
@@ -480,7 +480,6 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
! grep "$TREE_HASH" out
-LIB_HTTPD_PORT=12345 # default port, 410, cannot be used as non-root
diff --git a/t/ b/t/
index 32e722d..cd9e606 100755
--- a/t/
+++ b/t/
@@ -260,7 +260,7 @@ test_lazy_prereq GIT_DAEMON '
# This test spawns a daemon, so run it only if the user would be OK with
# testing with git-daemon.
test_expect_success PIPE,JGIT,GIT_DAEMON 'indicate no refs in standards-compliant empty remote' '
- JGIT_DAEMON_PORT=${JGIT_DAEMON_PORT-${this_test#t}} &&
+ test_set_port JGIT_DAEMON_PORT &&
git init --bare empty.git &&
>empty.git/git-daemon-export-ok &&
diff --git a/t/ b/t/
index 6b3bbf9..4459bdd 100644
--- a/t/
+++ b/t/
@@ -1263,3 +1263,39 @@ test_oid () {
fi &&
eval "printf '%s' \"\${$var}\""
+# Choose a port number based on the test script's number and store it in
+# the given variable name, unless that variable already contains a number.
+test_set_port () {
+ local var=$1 port
+ if test $# -ne 1 || test -z "$var"
+ then
+ BUG "test_set_port requires a variable name"
+ fi
+ eval port=\$$var
+ case "$port" in
+ "")
+ # No port is set in the given env var, use the test
+ # number as port number instead.
+ # Remove not only the leading 't', but all leading zeros
+ # as well, so the arithmetic below won't (mis)interpret
+ # a test number like '0123' as an octal value.
+ port=${this_test#${this_test%%[1-9]*}}
+ if test "${port:-0}" -lt 1024
+ then
+ # root-only port, use a larger one instead.
+ port=$(($port + 10000))
+ fi
+ eval $var=$port
+ ;;
+ *[^0-9]*|0*)
+ error >&7 "invalid port number: $port"
+ ;;
+ *)
+ # The user has specified the port.
+ ;;
+ esac