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
19 #include "test/core/util/test_config.h"
20
21 #include <inttypes.h>
22 #include <stdlib.h>
23
24 #include "absl/debugging/failure_signal_handler.h"
25 #include "absl/status/status.h"
26 #include "absl/strings/match.h"
27 #include "absl/strings/str_format.h"
28 #include "absl/strings/string_view.h"
29
30 #include <grpc/grpc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
33
34 #include "src/core/lib/gprpp/crash.h"
35 #include "src/core/lib/surface/init.h"
36 #include "test/core/event_engine/test_init.h"
37 #include "test/core/util/build.h"
38 #include "test/core/util/stack_tracer.h"
39
40 int64_t g_fixture_slowdown_factor = 1;
41 int64_t g_poller_slowdown_factor = 1;
42
43 #if GPR_GETPID_IN_UNISTD_H
44 #include <unistd.h>
45
seed(void)46 static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
47 #endif
48
49 #if GPR_GETPID_IN_PROCESS_H
50 #include <process.h>
51
seed(void)52 static unsigned seed(void) { return (unsigned)_getpid(); }
53 #endif
54
grpc_test_sanitizer_slowdown_factor()55 int64_t grpc_test_sanitizer_slowdown_factor() {
56 int64_t sanitizer_multiplier = 1;
57 if (BuiltUnderValgrind()) {
58 sanitizer_multiplier = 20;
59 } else if (BuiltUnderTsan()) {
60 sanitizer_multiplier = 5;
61 } else if (BuiltUnderAsan()) {
62 sanitizer_multiplier = 3;
63 } else if (BuiltUnderMsan()) {
64 sanitizer_multiplier = 4;
65 } else if (BuiltUnderUbsan()) {
66 sanitizer_multiplier = 5;
67 }
68 return sanitizer_multiplier;
69 }
70
grpc_test_slowdown_factor()71 int64_t grpc_test_slowdown_factor() {
72 return grpc_test_sanitizer_slowdown_factor() * g_fixture_slowdown_factor *
73 g_poller_slowdown_factor;
74 }
75
grpc_timeout_seconds_to_deadline(int64_t time_s)76 gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s) {
77 return gpr_time_add(
78 gpr_now(GPR_CLOCK_MONOTONIC),
79 gpr_time_from_millis(
80 grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
81 GPR_TIMESPAN));
82 }
83
grpc_timeout_milliseconds_to_deadline(int64_t time_ms)84 gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms) {
85 return gpr_time_add(
86 gpr_now(GPR_CLOCK_MONOTONIC),
87 gpr_time_from_micros(
88 grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
89 GPR_TIMESPAN));
90 }
91
92 namespace {
RmArg(int i,int * argc,char ** argv)93 void RmArg(int i, int* argc, char** argv) {
94 --(*argc);
95 while (i < *argc) {
96 argv[i] = argv[i + 1];
97 ++i;
98 }
99 }
100
ParseTestArgs(int * argc,char ** argv)101 void ParseTestArgs(int* argc, char** argv) {
102 if (argc == nullptr || *argc <= 1) return;
103 // flags to look for and consume
104 const absl::string_view engine_flag{"--engine="};
105 int i = 1;
106 while (i < *argc) {
107 if (absl::StartsWith(argv[i], engine_flag)) {
108 absl::Status engine_set =
109 grpc_event_engine::experimental::InitializeTestingEventEngineFactory(
110 argv[i] + engine_flag.length());
111 if (!engine_set.ok()) {
112 grpc_core::Crash(absl::StrFormat("%s", engine_set.ToString().c_str()));
113 }
114 // remove the spent argv
115 RmArg(i, argc, argv);
116 continue;
117 }
118 ++i;
119 }
120 }
121 } // namespace
122
grpc_test_init(int * argc,char ** argv)123 void grpc_test_init(int* argc, char** argv) {
124 gpr_log_verbosity_init();
125 ParseTestArgs(argc, argv);
126 grpc_core::testing::InitializeStackTracer(argv[0]);
127 absl::FailureSignalHandlerOptions options;
128 absl::InstallFailureSignalHandler(options);
129 gpr_log(GPR_DEBUG,
130 "test slowdown factor: sanitizer=%" PRId64 ", fixture=%" PRId64
131 ", poller=%" PRId64 ", total=%" PRId64,
132 grpc_test_sanitizer_slowdown_factor(), g_fixture_slowdown_factor,
133 g_poller_slowdown_factor, grpc_test_slowdown_factor());
134 // seed rng with pid, so we don't end up with the same random numbers as a
135 // concurrently running test binary
136 srand(seed());
137 }
138
grpc_wait_until_shutdown(int64_t time_s)139 bool grpc_wait_until_shutdown(int64_t time_s) {
140 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s);
141 while (grpc_is_initialized()) {
142 grpc_maybe_wait_for_async_shutdown();
143 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
144 gpr_time_from_millis(1, GPR_TIMESPAN)));
145 if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
146 return false;
147 }
148 }
149 return true;
150 }
151
152 namespace grpc {
153 namespace testing {
154
TestEnvironment(int * argc,char ** argv)155 TestEnvironment::TestEnvironment(int* argc, char** argv) {
156 grpc_test_init(argc, argv);
157 }
158
~TestEnvironment()159 TestEnvironment::~TestEnvironment() {
160 // This will wait until gRPC shutdown has actually happened to make sure
161 // no gRPC resources (such as thread) are active. (timeout = 10s)
162 if (!grpc_wait_until_shutdown(10)) {
163 gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
164 }
165 if (BuiltUnderMsan()) {
166 // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
167 // running. Although the code above waits until shutdown is done, chances
168 // are that thread itself is still alive. To workaround this problem, this
169 // is going to wait for 0.5 sec to give a chance to the shutdown thread to
170 // exit. https://github.com/grpc/grpc/issues/23695
171 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
172 gpr_time_from_millis(500, GPR_TIMESPAN)));
173 }
174 gpr_log(GPR_INFO, "TestEnvironment ends");
175 }
176
TestGrpcScope()177 TestGrpcScope::TestGrpcScope() { grpc_init(); }
178
~TestGrpcScope()179 TestGrpcScope::~TestGrpcScope() {
180 grpc_shutdown();
181 if (!grpc_wait_until_shutdown(10)) {
182 gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
183 }
184 }
185
186 } // namespace testing
187 } // namespace grpc
188