From 9e735f40e5299d0bd0e6b09d1fa0f9fb38bfb7b6 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 29 Aug 2018 09:36:22 +0200 Subject: fix port picker for remote bazel --- test/core/util/port_isolated_runtime_environment.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/core/util/port_isolated_runtime_environment.cc b/test/core/util/port_isolated_runtime_environment.cc index ff8342ff4a..773290b795 100644 --- a/test/core/util/port_isolated_runtime_environment.cc +++ b/test/core/util/port_isolated_runtime_environment.cc @@ -16,8 +16,9 @@ * */ -/* 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. +/* When individual tests run in an isolated runtime environment (e.g. each test + * runs in a separate container) the framework takes a round-robin pick of a + * port within certain range. There is no need to recycle ports. */ #include #include @@ -28,18 +29,21 @@ #include "test/core/util/port.h" #define MIN_PORT 49152 -#define MAX_PORT 65536 +#define MAX_PORT 65535 int get_random_starting_port() { srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - return rand() % (MAX_PORT - MIN_PORT + 1) + MIN_PORT; + double rnd = static_cast(rand()) / + (static_cast(RAND_MAX) + 1.0); // values from [0,1) + return static_cast(rnd * (MAX_PORT - MIN_PORT + 1)) + MIN_PORT; } static int s_allocated_port = get_random_starting_port(); int grpc_pick_unused_port_or_die(void) { + // TODO(jtattermusch): protect by mutex int allocated_port = s_allocated_port++; - if (s_allocated_port == MAX_PORT) { + if (s_allocated_port == MAX_PORT + 1) { s_allocated_port = MIN_PORT; } -- cgit v1.2.3 From 577c2e4090386c75c30bbf9110aa596b766f6037 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 31 Aug 2018 15:28:04 +0200 Subject: threadsafe port picker for remote bazel --- test/core/util/port_isolated_runtime_environment.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/core/util/port_isolated_runtime_environment.cc b/test/core/util/port_isolated_runtime_environment.cc index 773290b795..c1ca185a67 100644 --- a/test/core/util/port_isolated_runtime_environment.cc +++ b/test/core/util/port_isolated_runtime_environment.cc @@ -20,6 +20,8 @@ * runs in a separate container) the framework takes a round-robin pick of a * port within certain range. There is no need to recycle ports. */ +#include +#include #include #include #include "src/core/lib/iomgr/port.h" @@ -31,23 +33,22 @@ #define MIN_PORT 49152 #define MAX_PORT 65535 -int get_random_starting_port() { +static int get_random_port_offset() { srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); double rnd = static_cast(rand()) / (static_cast(RAND_MAX) + 1.0); // values from [0,1) - return static_cast(rnd * (MAX_PORT - MIN_PORT + 1)) + MIN_PORT; + return static_cast(rnd * (MAX_PORT - MIN_PORT + 1)); } -static int s_allocated_port = get_random_starting_port(); +static int s_initial_offset = get_random_port_offset(); +static gpr_atm s_pick_counter = 0; int grpc_pick_unused_port_or_die(void) { - // TODO(jtattermusch): protect by mutex - int allocated_port = s_allocated_port++; - if (s_allocated_port == MAX_PORT + 1) { - s_allocated_port = MIN_PORT; - } - - return allocated_port; + int orig_counter_val = + static_cast(gpr_atm_full_fetch_add(&s_pick_counter, 1)); + GPR_ASSERT(orig_counter_val < (MAX_PORT - MIN_PORT + 1)); + return MIN_PORT + + (s_initial_offset + orig_counter_val) % (MAX_PORT - MIN_PORT + 1); } void grpc_recycle_unused_port(int port) { (void)port; } -- cgit v1.2.3