diff options
author | Craig Tiller <craig.tiller@gmail.com> | 2015-09-02 12:36:25 -0700 |
---|---|---|
committer | Craig Tiller <craig.tiller@gmail.com> | 2015-09-02 12:36:25 -0700 |
commit | 4326f739b6db5208f9bd685415acfa7db46a123a (patch) | |
tree | 415c45d59cbf6b9c9f38f55e1915bcb425e72257 /test/core | |
parent | 03ab064fb7ddaddd3068757f0bff6457459cb726 (diff) |
Ensure port picking doesnt repeatedly return the same port
Diffstat (limited to 'test/core')
-rw-r--r-- | test/core/util/port_windows.c | 29 |
1 files changed, 29 insertions, 0 deletions
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; } |