• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/atm.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26 #include <stdlib.h>
27 #include "src/core/lib/iomgr/port.h"
28 #include "test/core/util/test_config.h"
29 #if defined(GRPC_PORT_ISOLATED_RUNTIME)
30 
31 #include "test/core/util/port.h"
32 
33 #define MIN_PORT 1025
34 #define MAX_PORT 32766
35 
get_random_port_offset()36 static int get_random_port_offset() {
37   srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
38   double rnd = static_cast<double>(rand()) /
39                (static_cast<double>(RAND_MAX) + 1.0);  // values from [0,1)
40   return static_cast<int>(rnd * (MAX_PORT - MIN_PORT + 1));
41 }
42 
43 static int s_initial_offset = get_random_port_offset();
44 static gpr_atm s_pick_counter = 0;
45 
grpc_pick_unused_port_or_die(void)46 int grpc_pick_unused_port_or_die(void) {
47   int orig_counter_val =
48       static_cast<int>(gpr_atm_full_fetch_add(&s_pick_counter, 1));
49   GPR_ASSERT(orig_counter_val < (MAX_PORT - MIN_PORT + 1));
50   return MIN_PORT +
51          (s_initial_offset + orig_counter_val) % (MAX_PORT - MIN_PORT + 1);
52 }
53 
grpc_recycle_unused_port(int port)54 void grpc_recycle_unused_port(int port) { (void)port; }
55 
56 #endif /* GRPC_PORT_ISOLATED_RUNTIME */
57