Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / core / util / test_config.cc
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 <grpc/impl/codegen/gpr_types.h>
22 #include <inttypes.h>
23 #include <signal.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32
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"
38
39 #include "absl/debugging/failure_signal_handler.h"
40 #include "absl/debugging/symbolize.h"
41
42 int64_t g_fixture_slowdown_factor = 1;
43 int64_t g_poller_slowdown_factor = 1;
44
45 #if GPR_GETPID_IN_UNISTD_H
46 #include <unistd.h>
47 static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
48 #endif
49
50 #if GPR_GETPID_IN_PROCESS_H
51 #include <process.h>
52 static unsigned seed(void) { return (unsigned)_getpid(); }
53 #endif
54
55 bool BuiltUnderValgrind() {
56 #ifdef RUNNING_ON_VALGRIND
57   return true;
58 #else
59   return false;
60 #endif
61 }
62
63 bool BuiltUnderTsan() {
64 #if defined(__has_feature)
65 #if __has_feature(thread_sanitizer)
66   return true;
67 #else
68   return false;
69 #endif
70 #else
71 #ifdef THREAD_SANITIZER
72   return true;
73 #else
74   return false;
75 #endif
76 #endif
77 }
78
79 bool BuiltUnderAsan() {
80 #if defined(__has_feature)
81 #if __has_feature(address_sanitizer)
82   return true;
83 #else
84   return false;
85 #endif
86 #else
87 #ifdef ADDRESS_SANITIZER
88   return true;
89 #else
90   return false;
91 #endif
92 #endif
93 }
94
95 bool BuiltUnderMsan() {
96 #if defined(__has_feature)
97 #if __has_feature(memory_sanitizer)
98   return true;
99 #else
100   return false;
101 #endif
102 #else
103 #ifdef MEMORY_SANITIZER
104   return true;
105 #else
106   return false;
107 #endif
108 #endif
109 }
110
111 bool BuiltUnderUbsan() {
112 #ifdef GRPC_UBSAN
113   return true;
114 #else
115   return false;
116 #endif
117 }
118
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;
131   }
132   return sanitizer_multiplier;
133 }
134
135 int64_t grpc_test_slowdown_factor() {
136   return grpc_test_sanitizer_slowdown_factor() * g_fixture_slowdown_factor *
137          g_poller_slowdown_factor;
138 }
139
140 gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s) {
141   return gpr_time_add(
142       gpr_now(GPR_CLOCK_MONOTONIC),
143       gpr_time_from_millis(
144           grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
145           GPR_TIMESPAN));
146 }
147
148 gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms) {
149   return gpr_time_add(
150       gpr_now(GPR_CLOCK_MONOTONIC),
151       gpr_time_from_micros(
152           grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
153           GPR_TIMESPAN));
154 }
155
156 void grpc_test_init(int argc, char** argv) {
157   grpc_core::testing::InitializeStackTracer(argv[0]);
158   absl::FailureSignalHandlerOptions options;
159   absl::InstallFailureSignalHandler(options);
160   gpr_log(GPR_DEBUG,
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 */
167   srand(seed());
168 }
169
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) {
177       return false;
178     }
179   }
180   return true;
181 }
182
183 namespace grpc {
184 namespace testing {
185
186 TestEnvironment::TestEnvironment(int argc, char** argv) {
187   grpc_test_init(argc, argv);
188 }
189
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");
195   }
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)));
204   }
205   gpr_log(GPR_INFO, "TestEnvironment ends");
206 }
207
208 TestGrpcScope::TestGrpcScope() { grpc_init(); }
209
210 TestGrpcScope::~TestGrpcScope() {
211   grpc_shutdown();
212   if (!grpc_wait_until_shutdown(10)) {
213     gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
214   }
215 }
216
217 }  // namespace testing
218 }  // namespace grpc