3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/core/util/test_config.h"
21 #include <grpc/impl/codegen/gpr_types.h>
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
33 #include "src/core/lib/gpr/string.h"
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/gprpp/examine_stack.h"
36 #include "src/core/lib/surface/init.h"
37 #include "test/core/util/stack_tracer.h"
39 #include "absl/debugging/failure_signal_handler.h"
40 #include "absl/debugging/symbolize.h"
42 int64_t g_fixture_slowdown_factor = 1;
43 int64_t g_poller_slowdown_factor = 1;
45 #if GPR_GETPID_IN_UNISTD_H
47 static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
50 #if GPR_GETPID_IN_PROCESS_H
52 static unsigned seed(void) { return (unsigned)_getpid(); }
55 bool BuiltUnderValgrind() {
56 #ifdef RUNNING_ON_VALGRIND
63 bool BuiltUnderTsan() {
64 #if defined(__has_feature)
65 #if __has_feature(thread_sanitizer)
71 #ifdef THREAD_SANITIZER
79 bool BuiltUnderAsan() {
80 #if defined(__has_feature)
81 #if __has_feature(address_sanitizer)
87 #ifdef ADDRESS_SANITIZER
95 bool BuiltUnderMsan() {
96 #if defined(__has_feature)
97 #if __has_feature(memory_sanitizer)
103 #ifdef MEMORY_SANITIZER
111 bool BuiltUnderUbsan() {
119 int64_t grpc_test_sanitizer_slowdown_factor() {
120 int64_t sanitizer_multiplier = 1;
121 if (BuiltUnderValgrind()) {
122 sanitizer_multiplier = 20;
123 } else if (BuiltUnderTsan()) {
124 sanitizer_multiplier = 5;
125 } else if (BuiltUnderAsan()) {
126 sanitizer_multiplier = 3;
127 } else if (BuiltUnderMsan()) {
128 sanitizer_multiplier = 4;
129 } else if (BuiltUnderUbsan()) {
130 sanitizer_multiplier = 5;
132 return sanitizer_multiplier;
135 int64_t grpc_test_slowdown_factor() {
136 return grpc_test_sanitizer_slowdown_factor() * g_fixture_slowdown_factor *
137 g_poller_slowdown_factor;
140 gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s) {
142 gpr_now(GPR_CLOCK_MONOTONIC),
143 gpr_time_from_millis(
144 grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
148 gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms) {
150 gpr_now(GPR_CLOCK_MONOTONIC),
151 gpr_time_from_micros(
152 grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
156 void grpc_test_init(int argc, char** argv) {
157 grpc_core::testing::InitializeStackTracer(argv[0]);
158 absl::FailureSignalHandlerOptions options;
159 absl::InstallFailureSignalHandler(options);
161 "test slowdown factor: sanitizer=%" PRId64 ", fixture=%" PRId64
162 ", poller=%" PRId64 ", total=%" PRId64,
163 grpc_test_sanitizer_slowdown_factor(), g_fixture_slowdown_factor,
164 g_poller_slowdown_factor, grpc_test_slowdown_factor());
165 /* seed rng with pid, so we don't end up with the same random numbers as a
166 concurrently running test binary */
170 bool grpc_wait_until_shutdown(int64_t time_s) {
171 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s);
172 while (grpc_is_initialized()) {
173 grpc_maybe_wait_for_async_shutdown();
174 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
175 gpr_time_from_millis(1, GPR_TIMESPAN)));
176 if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
186 TestEnvironment::TestEnvironment(int argc, char** argv) {
187 grpc_test_init(argc, argv);
190 TestEnvironment::~TestEnvironment() {
191 // This will wait until gRPC shutdown has actually happened to make sure
192 // no gRPC resources (such as thread) are active. (timeout = 10s)
193 if (!grpc_wait_until_shutdown(10)) {
194 gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
196 if (BuiltUnderMsan()) {
197 // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
198 // running. Although the code above waits until shutdown is done, chances
199 // are that thread itself is still alive. To workaround this problem, this
200 // is going to wait for 0.5 sec to give a chance to the shutdown thread to
201 // exit. https://github.com/grpc/grpc/issues/23695
202 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
203 gpr_time_from_millis(500, GPR_TIMESPAN)));
205 gpr_log(GPR_INFO, "TestEnvironment ends");
208 TestGrpcScope::TestGrpcScope() { grpc_init(); }
210 TestGrpcScope::~TestGrpcScope() {
212 if (!grpc_wait_until_shutdown(10)) {
213 gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
217 } // namespace testing