|  | @@ -16,9 +16,12 @@
 | 
	
		
			
				|  |  |   *
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -/* 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 <grpc/support/atm.h>
 | 
	
		
			
				|  |  | +#include <grpc/support/log.h>
 | 
	
		
			
				|  |  |  #include <grpc/support/time.h>
 | 
	
		
			
				|  |  |  #include <stdlib.h>
 | 
	
		
			
				|  |  |  #include "src/core/lib/iomgr/port.h"
 | 
	
	
		
			
				|  | @@ -28,22 +31,24 @@
 | 
	
		
			
				|  |  |  #include "test/core/util/port.h"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  #define MIN_PORT 49152
 | 
	
		
			
				|  |  | -#define MAX_PORT 65536
 | 
	
		
			
				|  |  | +#define MAX_PORT 65535
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -int get_random_starting_port() {
 | 
	
		
			
				|  |  | +static int get_random_port_offset() {
 | 
	
		
			
				|  |  |    srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
 | 
	
		
			
				|  |  | -  return rand() % (MAX_PORT - MIN_PORT + 1) + MIN_PORT;
 | 
	
		
			
				|  |  | +  double rnd = static_cast<double>(rand()) /
 | 
	
		
			
				|  |  | +               (static_cast<double>(RAND_MAX) + 1.0);  // values from [0,1)
 | 
	
		
			
				|  |  | +  return static_cast<int>(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) {
 | 
	
		
			
				|  |  | -  int allocated_port = s_allocated_port++;
 | 
	
		
			
				|  |  | -  if (s_allocated_port == MAX_PORT) {
 | 
	
		
			
				|  |  | -    s_allocated_port = MIN_PORT;
 | 
	
		
			
				|  |  | -  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -  return allocated_port;
 | 
	
		
			
				|  |  | +  int orig_counter_val =
 | 
	
		
			
				|  |  | +      static_cast<int>(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; }
 |