aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/util
diff options
context:
space:
mode:
authorGravatar Adele Zhou <adelez@google.com>2018-05-10 15:19:07 -0700
committerGravatar Adele Zhou <adelez@google.com>2018-05-10 16:12:49 -0700
commitddab3f011f7d4f5adb60388cb3de4ff302285747 (patch)
tree7a18720002f940a8a91ae58308ab418880e44395 /test/core/util
parent173a29d82e3235d9b988f8e6da823ca6df806e21 (diff)
Pick a random starting port
Diffstat (limited to 'test/core/util')
-rw-r--r--test/core/util/port_isolated_runtime_environment.cc17
1 files changed, 13 insertions, 4 deletions
diff --git a/test/core/util/port_isolated_runtime_environment.cc b/test/core/util/port_isolated_runtime_environment.cc
index 5f0585e9fb..ff8342ff4a 100644
--- a/test/core/util/port_isolated_runtime_environment.cc
+++ b/test/core/util/port_isolated_runtime_environment.cc
@@ -19,19 +19,28 @@
/* When running tests on remote machines, the framework takes a round-robin pick
* of a port within certain range. There is no need to recycle ports.
*/
+#include <grpc/support/time.h>
+#include <stdlib.h>
#include "src/core/lib/iomgr/port.h"
#include "test/core/util/test_config.h"
#if defined(GRPC_PORT_ISOLATED_RUNTIME)
#include "test/core/util/port.h"
-#define LOWER_PORT 49152
-static int s_allocated_port = LOWER_PORT;
+#define MIN_PORT 49152
+#define MAX_PORT 65536
+
+int get_random_starting_port() {
+ srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
+ return rand() % (MAX_PORT - MIN_PORT + 1) + MIN_PORT;
+}
+
+static int s_allocated_port = get_random_starting_port();
int grpc_pick_unused_port_or_die(void) {
int allocated_port = s_allocated_port++;
- if (s_allocated_port == 65536) {
- s_allocated_port = LOWER_PORT;
+ if (s_allocated_port == MAX_PORT) {
+ s_allocated_port = MIN_PORT;
}
return allocated_port;