From eb54e7f1ae875b984aa3963bcc952e806126c3a9 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 28 Jan 2019 15:23:33 -0800 Subject: [PATCH] get free port that can be used by non root users (> 1024) --- test/IXTest.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/IXTest.cpp b/test/IXTest.cpp index 7a9c1096..d2062524 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -69,7 +69,7 @@ namespace ix Logger() << msg; } - int getFreePort() + int getAnyFreePort() { int defaultPort = 8090; @@ -117,4 +117,23 @@ namespace ix return port; } + + int getFreePort() + { + while (true) + { + int port = getAnyFreePort(); + + // + // Only port above 1024 can be used by non root users, but for some + // reason I got port 7 returned with macOS when binding on port 0... + // + if (port > 1024) + { + return port; + } + } + + return -1; + } }