Skip to content

Commit

Permalink
Merge pull request grpc#3204 from ctiller/fix-teh-tests
Browse files Browse the repository at this point in the history
Fixes a couple of tests on Windows
  • Loading branch information
nicolasnoble committed Sep 4, 2015
2 parents 8d6536e + 627758d commit 2d96240
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/core/surface/completion_queue_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static void test_threading(int producers, int consumers) {

int main(int argc, char **argv) {
grpc_test_init(argc, argv);
grpc_iomgr_init();
grpc_init();
test_no_op();
test_wait_empty();
test_shutdown_then_next_polling();
Expand All @@ -336,6 +336,6 @@ int main(int argc, char **argv) {
test_threading(1, 10);
test_threading(10, 1);
test_threading(10, 10);
grpc_iomgr_shutdown();
grpc_shutdown();
return 0;
}
29 changes: 29 additions & 0 deletions test/core/util/port_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down

0 comments on commit 2d96240

Please sign in to comment.