Debian Codegraph @

test/core/util/port_isolated_runtime_environment.cc

raw
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 #if defined(GRPC_PORT_ISOLATED_RUNTIME)
20
21 /* When individual tests run in an isolated runtime environment (e.g. each test
22 * runs in a separate container) the framework takes a round-robin pick of a
23 * port within certain range. There is no need to recycle ports.
24 */
25 #include <stdlib.h>
26
27 #include <grpc/support/atm.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30
31 #include "src/core/lib/iomgr/port.h"
32 #include "test/core/util/port.h"
33 #include "test/core/util/test_config.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
56 int grpc_pick_unused_port_or_die(void) {
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 */