From 4326f739b6db5208f9bd685415acfa7db46a123a Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 2 Sep 2015 12:36:25 -0700 Subject: Ensure port picking doesnt repeatedly return the same port --- test/core/util/port_windows.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test/core') diff --git a/test/core/util/port_windows.c b/test/core/util/port_windows.c index 2f64626cf3..a57fe692e1 100644 --- a/test/core/util/port_windows.c +++ b/test/core/util/port_windows.c @@ -52,6 +52,30 @@ #define NUM_RANDOM_PORTS_TO_PICK 100 +static int *chosen_ports = NULL; +static size_t num_chosen_ports = 0; + +static int has_port_been_chosen(int port) { + size_t i; + for (i = 0; i < num_chosen_ports; i++) { + if (chosen_ports[i] == port) { + return 1; + } + } + return 0; +} + +static void free_chosen_ports() { gpr_free(chosen_ports); } + +static void chose_port(int port) { + if (chosen_ports == NULL) { + atexit(free_chosen_ports); + } + num_chosen_ports++; + chosen_ports = gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports); + chosen_ports[num_chosen_ports - 1] = port; +} + static int is_port_available(int *port, int is_tcp) { const int proto = is_tcp ? IPPROTO_TCP : 0; const SOCKET fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); @@ -202,6 +226,10 @@ int grpc_pick_unused_port(void) { port = 0; } + if (has_port_been_chosen(port)) { + continue; + } + if (!is_port_available(&port, is_tcp)) { continue; } @@ -218,6 +246,7 @@ int grpc_pick_unused_port(void) { /* TODO(ctiller): consider caching this port in some structure, to avoid handing it out again */ + chose_port(port); return port; } -- cgit v1.2.3