From c8cb4ed45d8ce33ede9611e903d3e6e6b28c490f Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 28 Aug 2015 16:02:34 -0400 Subject: Removed on_connect callback from udp_server_test --- test/core/iomgr/udp_server_test.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'test/core') diff --git a/test/core/iomgr/udp_server_test.c b/test/core/iomgr/udp_server_test.c index c91752b937..58f63390b0 100644 --- a/test/core/iomgr/udp_server_test.c +++ b/test/core/iomgr/udp_server_test.c @@ -49,8 +49,6 @@ static grpc_pollset g_pollset; static int g_number_of_reads = 0; static int g_number_of_bytes_read = 0; -static void on_connect(void *arg, grpc_endpoint *udp) {} - static void on_read(int fd, grpc_udp_server_cb new_transport_cb, void *cb_arg) { char read_buffer[512]; int byte_count; @@ -73,7 +71,7 @@ static void test_no_op(void) { static void test_no_op_with_start(void) { grpc_udp_server *s = grpc_udp_server_create(); LOG_TEST("test_no_op_with_start"); - grpc_udp_server_start(s, NULL, 0, on_connect, NULL); + grpc_udp_server_start(s, NULL, 0); grpc_udp_server_destroy(s, NULL, NULL); } @@ -100,7 +98,7 @@ static void test_no_op_with_port_and_start(void) { GPR_ASSERT(grpc_udp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), on_read)); - grpc_udp_server_start(s, NULL, 0, on_connect, NULL); + grpc_udp_server_start(s, NULL, 0); grpc_udp_server_destroy(s, NULL, NULL); } @@ -130,7 +128,7 @@ static void test_receive(int number_of_clients) { GPR_ASSERT(addr_len <= sizeof(addr)); pollsets[0] = &g_pollset; - grpc_udp_server_start(s, pollsets, 1, on_connect, NULL); + grpc_udp_server_start(s, pollsets, 1); gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); -- cgit v1.2.3 From 0cf27f7e18820ef748bbf7db2566b45d62fc4d3c Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Sat, 12 Sep 2015 00:34:27 -0700 Subject: Fixed leak introduced in #3293 --- test/core/end2end/dualstack_socket_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/core') diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c index ec3fb65105..fec9667db8 100644 --- a/test/core/end2end/dualstack_socket_test.c +++ b/test/core/end2end/dualstack_socket_test.c @@ -267,7 +267,7 @@ void test_connect(const char *server_host, const char *client_host, int port, int external_dns_works(const char *host) { grpc_resolved_addresses *res = grpc_blocking_resolve_address(host, "80"); if (res != NULL) { - gpr_free(res); + grpc_resolved_addresses_destroy(res); return 1; } return 0; -- cgit v1.2.3 From ae322afe60742e7d19e399752901fc0d0a36ed49 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Sun, 13 Sep 2015 18:41:21 +0000 Subject: Better port selection - avoid IANA and Linux ephemeral port ranges - support dropping allocated ports - aggressively try to reclaim ports if we reach exhaustion - set SO_REUSEADDR on test port binds --- test/core/util/port_posix.c | 75 ++++++++++++++++++++++++++++++++++++++---- tools/run_tests/port_server.py | 30 ++++++++++++++--- 2 files changed, 93 insertions(+), 12 deletions(-) (limited to 'test/core') diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index be45bae496..5c09388254 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -47,6 +47,7 @@ #include #include #include +#include #include "src/core/httpcli/httpcli.h" #include "src/core/support/env.h" @@ -66,7 +67,71 @@ static int has_port_been_chosen(int port) { return 0; } -static void free_chosen_ports() { gpr_free(chosen_ports); } +typedef struct freereq { + grpc_pollset pollset; + int done; +} freereq; + +static void destroy_pollset_and_shutdown(void *p) { + grpc_pollset_destroy(p); + grpc_shutdown(); +} + +static void freed_port_from_server(void *arg, + const grpc_httpcli_response *response) { + freereq *pr = arg; + GPR_ASSERT(response); + GPR_ASSERT(response->status == 200); + gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); + pr->done = 1; + grpc_pollset_kick(&pr->pollset, NULL); + gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); +} + +static void free_port_using_server(char *server, int port) { + grpc_httpcli_context context; + grpc_httpcli_request req; + freereq pr; + char *path; + + grpc_init(); + + memset(&pr, 0, sizeof(pr)); + memset(&req, 0, sizeof(req)); + grpc_pollset_init(&pr.pollset); + + req.host = server; + gpr_asprintf(&path, "/drop/%d", port); + req.path = path; + + grpc_httpcli_context_init(&context); + grpc_httpcli_get(&context, &pr.pollset, &req, + GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), freed_port_from_server, + &pr); + gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); + while (!pr.done) { + grpc_pollset_worker worker; + grpc_pollset_work(&pr.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), + GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1)); + } + gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); + + grpc_httpcli_context_destroy(&context); + grpc_pollset_shutdown(&pr.pollset, destroy_pollset_and_shutdown, &pr.pollset); + gpr_free(path); +} + +static void free_chosen_ports() { + char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); + if (env != NULL) { + size_t i; + for (i = 0; i < num_chosen_ports; i++) { + free_port_using_server(env, chosen_ports[i]); + } + } + + gpr_free(chosen_ports); +} static void chose_port(int port) { if (chosen_ports == NULL) { @@ -151,11 +216,6 @@ static void got_port_from_server(void *arg, gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); } -static void destroy_pollset_and_shutdown(void *p) { - grpc_pollset_destroy(p); - grpc_shutdown(); -} - static int pick_port_using_server(char *server) { grpc_httpcli_context context; grpc_httpcli_request req; @@ -211,8 +271,9 @@ int grpc_pick_unused_port(void) { int port = pick_port_using_server(env); gpr_free(env); if (port != 0) { - return port; + chose_port(port); } + return port; } for (;;) { diff --git a/tools/run_tests/port_server.py b/tools/run_tests/port_server.py index 0f81470d28..d14c829fe0 100755 --- a/tools/run_tests/port_server.py +++ b/tools/run_tests/port_server.py @@ -37,6 +37,7 @@ import os import socket import sys import time +import yaml argp = argparse.ArgumentParser(description='Server for httpcli_test') argp.add_argument('-p', '--port', default=12345, type=int) @@ -51,16 +52,17 @@ with open(__file__) as f: _MY_VERSION = hashlib.sha1(f.read()).hexdigest() -def refill_pool(): +def refill_pool(max_timeout): """Scan for ports not marked for being in use""" - for i in range(10000, 65000): + for i in range(1025, 32767): if len(pool) > 100: break if i in in_use: age = time.time() - in_use[i] - if age < 600: + if age < max_timeout: continue del in_use[i] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: s.bind(('localhost', i)) pool.append(i) @@ -73,8 +75,12 @@ def refill_pool(): def allocate_port(): global pool global in_use - if not pool: - refill_pool() + max_timeout = 600 + while not pool: + refill_pool(max_timeout) + if not pool: + time.sleep(1) + max_timeout /= 2 port = pool[0] pool = pool[1:] in_use[port] = time.time() @@ -97,12 +103,26 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): p = allocate_port() self.log_message('allocated port %d' % p) self.wfile.write('%d' % p) + elif self.path[0:6] == '/drop/': + self.send_response(200) + self.send_header('Content-Type', 'text/plain') + self.end_headers() + p = int(self.path[6:]) + del in_use[p] + pool.append(p) + self.log_message('drop port %d' % p) elif self.path == '/version': # fetch a version string and the current process pid self.send_response(200) self.send_header('Content-Type', 'text/plain') self.end_headers() self.wfile.write(_MY_VERSION) + elif self.path == '/dump': + self.send_response(200) + self.send_header('Content-Type', 'text/plain') + self.end_headers() + now = time.time() + self.wfile.write(yaml.dump({'pool': pool, 'in_use': dict((k, now - v) for k, v in in_use.iteritems())})) elif self.path == '/quit': self.send_response(200) self.end_headers() -- cgit v1.2.3 From d8374ef194155370f5ab3f8619ac546ca36271a5 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Sun, 13 Sep 2015 12:01:38 -0700 Subject: No need for the assert here --- test/core/util/port_posix.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'test/core') diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index 5c09388254..6af86cc930 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -80,8 +80,6 @@ static void destroy_pollset_and_shutdown(void *p) { static void freed_port_from_server(void *arg, const grpc_httpcli_response *response) { freereq *pr = arg; - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); pr->done = 1; grpc_pollset_kick(&pr->pollset, NULL); -- cgit v1.2.3 From e388ed3a458b42928a358bc51078ddc7c39f120a Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Mon, 14 Sep 2015 15:52:38 -0400 Subject: Remove arguments from on_read in test --- test/core/iomgr/udp_server_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/core') diff --git a/test/core/iomgr/udp_server_test.c b/test/core/iomgr/udp_server_test.c index 10f236e945..24b6b1c717 100644 --- a/test/core/iomgr/udp_server_test.c +++ b/test/core/iomgr/udp_server_test.c @@ -49,7 +49,7 @@ static grpc_pollset g_pollset; static int g_number_of_reads = 0; static int g_number_of_bytes_read = 0; -static void on_read(int fd, grpc_udp_server_cb new_transport_cb, void *cb_arg) { +static void on_read(int fd) { char read_buffer[512]; ssize_t byte_count; -- cgit v1.2.3 From 404044acc27dcdae5b6d37d0a5120f3e90e9b08d Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 14 Sep 2015 14:49:53 -0700 Subject: Retry failed port get attempts a few times --- test/core/util/port_posix.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/core') diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index 6af86cc930..49337c0c28 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -194,6 +194,9 @@ static int is_port_available(int *port, int is_tcp) { typedef struct portreq { grpc_pollset pollset; int port; + int retries; + char *server; + grpc_httpcli_context *ctx; } portreq; static void got_port_from_server(void *arg, @@ -201,6 +204,19 @@ static void got_port_from_server(void *arg, size_t i; int port = 0; portreq *pr = arg; + if (!response || response->status != 200) { + grpc_httpcli_request req; + memset(&req, 0, sizeof(req)); + GPR_ASSERT(pr->retries < 10); + pr->retries++; + req.host = pr->server; + req.path = "/get"; + gpr_log(GPR_DEBUG, "failed port pick from server: retrying"); + sleep(1); + grpc_httpcli_get(pr->ctx, &pr->pollset, &req, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), + got_port_from_server, pr); + return; + } GPR_ASSERT(response); GPR_ASSERT(response->status == 200); for (i = 0; i < response->body_length; i++) { @@ -225,6 +241,8 @@ static int pick_port_using_server(char *server) { memset(&req, 0, sizeof(req)); grpc_pollset_init(&pr.pollset); pr.port = -1; + pr.server = server; + pr.ctx = &context; req.host = server; req.path = "/get"; -- cgit v1.2.3 From 7dc34ac7a057abba51a38fb7f6a73ee827933e70 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 15 Sep 2015 07:45:39 -0700 Subject: Fix memory leak --- test/core/util/port_posix.c | 1 + 1 file changed, 1 insertion(+) (limited to 'test/core') diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index 49337c0c28..03f4c4912e 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -126,6 +126,7 @@ static void free_chosen_ports() { for (i = 0; i < num_chosen_ports; i++) { free_port_using_server(env, chosen_ports[i]); } + gpr_free(env); } gpr_free(chosen_ports); -- cgit v1.2.3