diff options
Diffstat (limited to 'test/core/util/port_windows.c')
-rw-r--r-- | test/core/util/port_windows.c | 97 |
1 files changed, 19 insertions, 78 deletions
diff --git a/test/core/util/port_windows.c b/test/core/util/port_windows.c index 85d7c0ce07..2b6d3dd223 100644 --- a/test/core/util/port_windows.c +++ b/test/core/util/port_windows.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015-2016, Google Inc. + * Copyright 2015, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,18 +37,19 @@ #include "test/core/util/port.h" +#include <errno.h> #include <process.h> #include <stdio.h> -#include <errno.h> #include <string.h> #include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> -#include "src/core/support/env.h" -#include "src/core/httpcli/httpcli.h" -#include "src/core/iomgr/sockaddr_utils.h" +#include "src/core/lib/http/httpcli.h" +#include "src/core/lib/iomgr/sockaddr_utils.h" +#include "src/core/lib/support/env.h" +#include "test/core/util/port_server_client.h" #define NUM_RANDOM_PORTS_TO_PICK 100 @@ -65,7 +66,18 @@ static int has_port_been_chosen(int port) { return 0; } -static void free_chosen_ports(void) { gpr_free(chosen_ports); } +static void free_chosen_ports(void) { + char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); + if (env != NULL) { + size_t i; + for (i = 0; i < num_chosen_ports; i++) { + grpc_free_port_using_server(env, chosen_ports[i]); + } + gpr_free(env); + } + + gpr_free(chosen_ports); +} static void chose_port(int port) { if (chosen_ports == NULL) { @@ -128,77 +140,6 @@ static int is_port_available(int *port, int is_tcp) { return 1; } -typedef struct portreq { - grpc_pollset pollset; - int port; -} portreq; - -static void got_port_from_server(grpc_exec_ctx *exec_ctx, void *arg, - const grpc_httpcli_response *response) { - size_t i; - int port = 0; - portreq *pr = arg; - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - for (i = 0; i < response->body_length; i++) { - GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9'); - port = port * 10 + response->body[i] - '0'; - } - GPR_ASSERT(port > 1024); - gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); - pr->port = port; - grpc_pollset_kick(&pr->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); -} - -static void destroy_pollset_and_shutdown(grpc_exec_ctx *exec_ctx, void *p, - bool success) { - grpc_pollset_destroy(p); - grpc_shutdown(); -} - -static int pick_port_using_server(char *server) { - grpc_httpcli_context context; - grpc_httpcli_request req; - portreq pr; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_closure destroy_pollset_closure; - - grpc_init(); - - memset(&pr, 0, sizeof(pr)); - memset(&req, 0, sizeof(req)); - grpc_pollset_init(&pr.pollset); - pr.port = -1; - - req.host = server; - req.path = "/get"; - - grpc_httpcli_context_init(&context); - grpc_httpcli_get(&exec_ctx, &context, &pr.pollset, &req, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server, - &pr); - gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); - while (pr.port == -1) { - grpc_pollset_worker worker; - grpc_pollset_work(&exec_ctx, &pr.pollset, &worker, - gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1)); - gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); - grpc_exec_ctx_flush(&exec_ctx); - gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); - - grpc_httpcli_context_destroy(&context); - grpc_closure_init(&destroy_pollset_closure, destroy_pollset_and_shutdown, - &pr.pollset); - grpc_pollset_shutdown(&exec_ctx, &pr.pollset, &destroy_pollset_closure); - - grpc_exec_ctx_finish(&exec_ctx); - return pr.port; -} - int grpc_pick_unused_port(void) { /* We repeatedly pick a port and then see whether or not it is available for use both as a TCP socket and a UDP socket. First, we @@ -218,7 +159,7 @@ int grpc_pick_unused_port(void) { char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); if (env) { - int port = pick_port_using_server(env); + int port = grpc_pick_port_using_server(env); gpr_free(env); if (port != 0) { return port; |