port_isolated_runtime_environment.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 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 
19 /* When individual tests run in an isolated runtime environment (e.g. each test
20  * runs in a separate container) the framework takes a round-robin pick of a
21  * port within certain range. There is no need to recycle ports.
22  */
23 #include <stdlib.h>
24 
25 #include <grpc/support/atm.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 
31 #if defined(GRPC_PORT_ISOLATED_RUNTIME)
32 
33 #include "test/core/util/port.h"
34 
35 #define MIN_PORT 1025
36 #define MAX_PORT 32766
37 
38 static int get_random_port_offset() {
39  srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
40  double rnd = static_cast<double>(rand()) /
41  (static_cast<double>(RAND_MAX) + 1.0); // values from [0,1)
42  return static_cast<int>(rnd * (MAX_PORT - MIN_PORT + 1));
43 }
44 
45 static int s_initial_offset = get_random_port_offset();
46 static gpr_atm s_pick_counter = 0;
47 
48 static int grpc_pick_unused_port_or_die_impl(void) {
49  int orig_counter_val =
50  static_cast<int>(gpr_atm_full_fetch_add(&s_pick_counter, 1));
51  GPR_ASSERT(orig_counter_val < (MAX_PORT - MIN_PORT + 1));
52  return MIN_PORT +
53  (s_initial_offset + orig_counter_val) % (MAX_PORT - MIN_PORT + 1);
54 }
55 
57  while (true) {
58  int port = grpc_pick_unused_port_or_die_impl();
59  // 5985 cannot be bound on Windows RBE and results in
60  // WSA_ERROR 10013: "An attempt was made to access a socket in a way
61  // forbidden by its access permissions."
62  if (port == 5985) {
63  continue;
64  }
65  return port;
66  }
67 }
68 
69 void grpc_recycle_unused_port(int port) { (void)port; }
70 
71 #endif /* GRPC_PORT_ISOLATED_RUNTIME */
gpr_atm_full_fetch_add
#define gpr_atm_full_fetch_add(p, delta)
Definition: impl/codegen/atm_gcc_atomic.h:62
log.h
port.h
grpc_recycle_unused_port
void grpc_recycle_unused_port(int port)
time.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
gpr_atm
intptr_t gpr_atm
Definition: impl/codegen/atm_gcc_atomic.h:32
port.h
atm.h
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:44