From 32c0153f8181feb89e0abf2ef47c02fb63f70063 Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Wed, 2 Aug 2017 16:09:08 -0700 Subject: [PATCH 1/2] Make port picking functions overridable --- test/core/util/port.c | 12 +++++++++--- test/core/util/port.h | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/test/core/util/port.c b/test/core/util/port.c index f430c543bd1..4df7572f270 100644 --- a/test/core/util/port.c +++ b/test/core/util/port.c @@ -79,7 +79,7 @@ static void chose_port(int port) { chosen_ports[num_chosen_ports - 1] = port; } -int grpc_pick_unused_port(void) { +static int grpc_pick_unused_port_impl(void) { int port = grpc_pick_port_using_server(); if (port != 0) { chose_port(port); @@ -88,7 +88,7 @@ int grpc_pick_unused_port(void) { return port; } -int grpc_pick_unused_port_or_die(void) { +static int grpc_pick_unused_port_or_die_impl(void) { int port = grpc_pick_unused_port(); if (port == 0) { fprintf(stderr, @@ -101,6 +101,12 @@ int grpc_pick_unused_port_or_die(void) { return port; } -void grpc_recycle_unused_port(int port) { GPR_ASSERT(free_chosen_port(port)); } +static void grpc_recycle_unused_port_impl(int port) { + GPR_ASSERT(free_chosen_port(port)); +} + +int (*grpc_pick_unused_port)(void) = grpc_pick_unused_port_impl; +int (*grpc_pick_unused_port_or_die)(void) = grpc_pick_unused_port_or_die_impl; +void (*grpc_recycle_unused_port)(int port) = grpc_recycle_unused_port_impl; #endif /* GRPC_TEST_PICK_PORT */ diff --git a/test/core/util/port.h b/test/core/util/port.h index 154e8f830c6..0e15f397a53 100644 --- a/test/core/util/port.h +++ b/test/core/util/port.h @@ -25,16 +25,16 @@ extern "C" { /* pick a port number that is currently unused by either tcp or udp. return 0 on failure. */ -int grpc_pick_unused_port(void); +extern int (*grpc_pick_unused_port)(void); /* pick a port number that is currently unused by either tcp or udp. abort on failure. */ -int grpc_pick_unused_port_or_die(void); +extern int (*grpc_pick_unused_port_or_die)(void); /* Return a port which was previously returned by grpc_pick_unused_port(). * Implementations of grpc_pick_unused_port() backed by a portserver may limit * the total number of ports available; this lets a binary return its allocated * ports back to the server if it is going to allocate a large number. */ -void grpc_recycle_unused_port(int port); +extern void (*grpc_recycle_unused_port)(int port); #ifdef __cplusplus } From 36969385e50ac6340129b85df11de72b23ec05e2 Mon Sep 17 00:00:00 2001 From: Yuchen Zeng Date: Wed, 2 Aug 2017 16:40:24 -0700 Subject: [PATCH 2/2] Group the port picking functions --- test/core/util/port.c | 25 ++++++++++++++++++++++--- test/core/util/port.h | 15 ++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/test/core/util/port.c b/test/core/util/port.c index 4df7572f270..b1fc722858e 100644 --- a/test/core/util/port.c +++ b/test/core/util/port.c @@ -105,8 +105,27 @@ static void grpc_recycle_unused_port_impl(int port) { GPR_ASSERT(free_chosen_port(port)); } -int (*grpc_pick_unused_port)(void) = grpc_pick_unused_port_impl; -int (*grpc_pick_unused_port_or_die)(void) = grpc_pick_unused_port_or_die_impl; -void (*grpc_recycle_unused_port)(int port) = grpc_recycle_unused_port_impl; +static grpc_pick_port_functions g_pick_port_functions = { + grpc_pick_unused_port_impl, grpc_pick_unused_port_or_die_impl, + grpc_recycle_unused_port_impl}; + +int grpc_pick_unused_port(void) { + return g_pick_port_functions.pick_unused_port_fn(); +} + +int grpc_pick_unused_port_or_die(void) { + return g_pick_port_functions.pick_unused_port_or_die_fn(); +} + +void grpc_recycle_unused_port(int port) { + g_pick_port_functions.recycle_unused_port_fn(port); +} + +void grpc_set_pick_port_functions(grpc_pick_port_functions functions) { + GPR_ASSERT(functions.pick_unused_port_fn != NULL); + GPR_ASSERT(functions.pick_unused_port_or_die_fn != NULL); + GPR_ASSERT(functions.recycle_unused_port_fn != NULL); + g_pick_port_functions = functions; +} #endif /* GRPC_TEST_PICK_PORT */ diff --git a/test/core/util/port.h b/test/core/util/port.h index 0e15f397a53..602099dea6d 100644 --- a/test/core/util/port.h +++ b/test/core/util/port.h @@ -23,18 +23,27 @@ extern "C" { #endif +typedef struct grpc_pick_port_functions { + int (*pick_unused_port_fn)(void); + int (*pick_unused_port_or_die_fn)(void); + void (*recycle_unused_port_fn)(int port); +} grpc_pick_port_functions; + /* pick a port number that is currently unused by either tcp or udp. return 0 on failure. */ -extern int (*grpc_pick_unused_port)(void); +int grpc_pick_unused_port(void); /* pick a port number that is currently unused by either tcp or udp. abort on failure. */ -extern int (*grpc_pick_unused_port_or_die)(void); +int grpc_pick_unused_port_or_die(void); /* Return a port which was previously returned by grpc_pick_unused_port(). * Implementations of grpc_pick_unused_port() backed by a portserver may limit * the total number of ports available; this lets a binary return its allocated * ports back to the server if it is going to allocate a large number. */ -extern void (*grpc_recycle_unused_port)(int port); +void grpc_recycle_unused_port(int port); + +/** Request the family of pick_port functions in \a functions be used. */ +void grpc_set_pick_port_functions(grpc_pick_port_functions functions); #ifdef __cplusplus }