port.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 #if defined(GRPC_TEST_PICK_PORT)
23 
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include <grpc/grpc.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
32 
36 #include "test/core/util/port.h"
38 
39 static int* chosen_ports = nullptr;
40 static size_t num_chosen_ports = 0;
41 static grpc_core::Mutex* g_default_port_picker_mu;
42 static gpr_once g_default_port_picker_init = GPR_ONCE_INIT;
43 
44 static void init_default_port_picker() {
45  g_default_port_picker_mu = new grpc_core::Mutex();
46 }
47 
48 static int free_chosen_port_locked(int port) {
49  size_t i;
50  int found = 0;
51  size_t found_at = 0;
52  /* Find the port and erase it from the list, then tell the server it can be
53  freed. */
54  for (i = 0; i < num_chosen_ports; i++) {
55  if (chosen_ports[i] == port) {
56  GPR_ASSERT(found == 0);
57  found = 1;
58  found_at = i;
59  }
60  }
61  if (found) {
62  chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1];
63  num_chosen_ports--;
65  }
66  return found;
67 }
68 
69 static void free_chosen_ports(void) {
70  grpc_core::MutexLock lock(g_default_port_picker_mu);
71  size_t i;
72  grpc_init();
73  for (i = 0; i < num_chosen_ports; i++) {
74  grpc_free_port_using_server(chosen_ports[i]);
75  }
76  grpc_shutdown();
77  gpr_free(chosen_ports);
78 }
79 
80 static void chose_port_locked(int port) {
81  if (chosen_ports == nullptr) {
82  atexit(free_chosen_ports);
83  }
84  num_chosen_ports++;
85  chosen_ports = static_cast<int*>(
86  gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports));
87  chosen_ports[num_chosen_ports - 1] = port;
88 }
89 
90 static int grpc_pick_unused_port_impl(void) {
91  gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
92  grpc_core::MutexLock lock(g_default_port_picker_mu);
94  if (port != 0) {
95  chose_port_locked(port);
96  }
97 
98  return port;
99 }
100 
101 static int grpc_pick_unused_port_or_die_impl(void) {
102  int port = grpc_pick_unused_port();
103  if (port == 0) {
104  fprintf(stderr,
105  "gRPC tests require a helper port server to allocate ports used \n"
106  "during the test.\n\n"
107  "This server is not currently running.\n\n"
108  "To start it, run tools/run_tests/start_port_server.py\n\n");
109  exit(1);
110  }
111  return port;
112 }
113 
114 static void grpc_recycle_unused_port_impl(int port) {
115  gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
116  grpc_core::MutexLock lock(g_default_port_picker_mu);
117  GPR_ASSERT(free_chosen_port_locked(port));
118 }
119 
120 static grpc_pick_port_functions g_pick_port_functions = {
121  grpc_pick_unused_port_impl, grpc_pick_unused_port_or_die_impl,
122  grpc_recycle_unused_port_impl};
123 
124 int grpc_pick_unused_port(void) {
125  return g_pick_port_functions.pick_unused_port_fn();
126 }
127 
129  return g_pick_port_functions.pick_unused_port_or_die_fn();
130 }
131 
132 void grpc_recycle_unused_port(int port) {
133  g_pick_port_functions.recycle_unused_port_fn(port);
134 }
135 
137  GPR_ASSERT(functions.pick_unused_port_fn != nullptr);
138  GPR_ASSERT(functions.pick_unused_port_or_die_fn != nullptr);
139  GPR_ASSERT(functions.recycle_unused_port_fn != nullptr);
140  g_pick_port_functions = functions;
141 }
142 
143 #endif /* GRPC_TEST_PICK_PORT */
log.h
port.h
sockaddr_utils.h
port_server_client.h
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_recycle_unused_port
void grpc_recycle_unused_port(int port)
resolve_address.h
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: impl/codegen/sync_posix.h:52
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
grpc_pick_port_functions::recycle_unused_port_fn
void(* recycle_unused_port_fn)(int port)
Definition: test/core/util/port.h:25
string_util.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
httpcli.h
grpc.h
grpc_pick_port_functions::pick_unused_port_or_die_fn
int(* pick_unused_port_or_die_fn)(void)
Definition: test/core/util/port.h:24
grpc_pick_port_functions
Definition: test/core/util/port.h:22
grpc_free_port_using_server
void grpc_free_port_using_server(int port)
grpc_set_pick_port_functions
void grpc_set_pick_port_functions(grpc_pick_port_functions functions)
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
port.h
grpc_pick_port_functions::pick_unused_port_fn
int(* pick_unused_port_fn)(void)
Definition: test/core/util/port.h:23
alloc.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
grpc_pick_port_using_server
int grpc_pick_port_using_server(void)
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_pick_unused_port
int grpc_pick_unused_port(void)
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:54