aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/util/port_posix.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/util/port_posix.c')
-rw-r--r--test/core/util/port_posix.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c
index eabd62fafc..265e0acee1 100644
--- a/test/core/util/port_posix.c
+++ b/test/core/util/port_posix.c
@@ -68,6 +68,31 @@ static int has_port_been_chosen(int port) {
return 0;
}
+static int free_chosen_port(int port) {
+ size_t i;
+ int found = 0;
+ size_t found_at = 0;
+ char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
+ /* Find the port and erase it from the list, then tell the server it can be
+ freed. */
+ for (i = 0; i < num_chosen_ports; i++) {
+ if (chosen_ports[i] == port) {
+ GPR_ASSERT(found == 0);
+ found = 1;
+ found_at = i;
+ }
+ }
+ if (found) {
+ chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1];
+ num_chosen_ports--;
+ if (env) {
+ grpc_free_port_using_server(env, port);
+ }
+ }
+ gpr_free(env);
+ return found;
+}
+
static void free_chosen_ports(void) {
char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
if (env != NULL) {
@@ -210,4 +235,6 @@ int grpc_pick_unused_port_or_die(void) {
return port;
}
+void grpc_recycle_unused_port(int port) { GPR_ASSERT(free_chosen_port(port)); }
+
#endif /* GPR_POSIX_SOCKET && GRPC_TEST_PICK_PORT */