• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/test_util/test_config.h"
20 
21 #include <grpc/grpc.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/time.h>
24 #include <inttypes.h>
25 #include <stdlib.h>
26 
27 #include <mutex>
28 
29 #include "absl/debugging/failure_signal_handler.h"
30 #include "absl/log/globals.h"
31 #include "absl/log/initialize.h"
32 #include "absl/log/log.h"
33 #include "absl/status/status.h"
34 #include "absl/strings/match.h"
35 #include "absl/strings/str_format.h"
36 #include "absl/strings/string_view.h"
37 #include "src/core/lib/surface/init.h"
38 #include "src/core/util/crash.h"
39 #include "test/core/event_engine/test_init.h"
40 #include "test/core/test_util/build.h"
41 #include "test/core/test_util/stack_tracer.h"
42 
43 int64_t g_fixture_slowdown_factor = 1;
44 int64_t g_poller_slowdown_factor = 1;
45 
46 #if GPR_GETPID_IN_UNISTD_H
47 #include <unistd.h>
48 
seed(void)49 static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
50 #endif
51 
52 #if GPR_GETPID_IN_PROCESS_H
53 #include <process.h>
54 
seed(void)55 static unsigned seed(void) { return (unsigned)_getpid(); }
56 #endif
57 
58 #ifdef GPR_WINDOWS
59 // clang-format off
60 #include <winsock2.h>
61 #include <iphlpapi.h>
62 // clang-format on
63 #endif
64 
grpc_test_sanitizer_slowdown_factor()65 int64_t grpc_test_sanitizer_slowdown_factor() {
66   int64_t sanitizer_multiplier = 1;
67   if (BuiltUnderValgrind()) {
68     sanitizer_multiplier = 20;
69   } else if (BuiltUnderTsan()) {
70     sanitizer_multiplier = 5;
71   } else if (BuiltUnderAsan()) {
72     sanitizer_multiplier = 3;
73   } else if (BuiltUnderMsan()) {
74     sanitizer_multiplier = 4;
75   } else if (BuiltUnderUbsan()) {
76     sanitizer_multiplier = 5;
77   }
78   return sanitizer_multiplier;
79 }
80 
grpc_test_slowdown_factor()81 int64_t grpc_test_slowdown_factor() {
82   return grpc_test_sanitizer_slowdown_factor() * g_fixture_slowdown_factor *
83          g_poller_slowdown_factor;
84 }
85 
grpc_timeout_seconds_to_deadline(int64_t time_s)86 gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s) {
87   return gpr_time_add(
88       gpr_now(GPR_CLOCK_MONOTONIC),
89       gpr_time_from_millis(
90           grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
91           GPR_TIMESPAN));
92 }
93 
grpc_timeout_milliseconds_to_deadline(int64_t time_ms)94 gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms) {
95   return gpr_time_add(
96       gpr_now(GPR_CLOCK_MONOTONIC),
97       gpr_time_from_micros(
98           grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
99           GPR_TIMESPAN));
100 }
101 
102 namespace {
RmArg(int i,int * argc,char ** argv)103 void RmArg(int i, int* argc, char** argv) {
104   --(*argc);
105   while (i < *argc) {
106     argv[i] = argv[i + 1];
107     ++i;
108   }
109 }
110 
ParseTestArgs(int * argc,char ** argv)111 void ParseTestArgs(int* argc, char** argv) {
112   if (argc == nullptr || *argc <= 1) return;
113   // flags to look for and consume
114   const absl::string_view engine_flag{"--engine="};
115   int i = 1;
116   while (i < *argc) {
117     if (absl::StartsWith(argv[i], engine_flag)) {
118       absl::Status engine_set =
119           grpc_event_engine::experimental::InitializeTestingEventEngineFactory(
120               argv[i] + engine_flag.length());
121       if (!engine_set.ok()) {
122         grpc_core::Crash(absl::StrFormat("%s", engine_set.ToString().c_str()));
123       }
124       // remove the spent argv
125       RmArg(i, argc, argv);
126       continue;
127     }
128     ++i;
129   }
130 }
131 
132 // grpc-oss-only-begin
133 std::once_flag log_flag;
134 // grpc-oss-only-end
135 
136 }  // namespace
137 
grpc_test_init(int * argc,char ** argv)138 void grpc_test_init(int* argc, char** argv) {
139   // grpc-oss-only-begin
140   std::call_once(log_flag, []() { absl::InitializeLog(); });
141   absl::SetGlobalVLogLevel(2);
142   absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo);
143   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
144   // grpc-oss-only-end
145   gpr_log_verbosity_init();
146   ParseTestArgs(argc, argv);
147   grpc_core::testing::InitializeStackTracer(argv[0]);
148   absl::FailureSignalHandlerOptions options;
149   absl::InstallFailureSignalHandler(options);
150   VLOG(2) << "test slowdown factor: sanitizer="
151           << grpc_test_sanitizer_slowdown_factor()
152           << ", fixture=" << g_fixture_slowdown_factor
153           << ", poller=" << g_poller_slowdown_factor
154           << ", total=" << grpc_test_slowdown_factor();
155   // seed rng with pid, so we don't end up with the same random numbers as a
156   // concurrently running test binary
157   srand(seed());
158 }
159 
grpc_set_absl_verbosity_debug()160 void grpc_set_absl_verbosity_debug() {
161   absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo);
162   absl::SetVLogLevel("*grpc*/*", 2);
163 }
164 
grpc_wait_until_shutdown(int64_t time_s)165 bool grpc_wait_until_shutdown(int64_t time_s) {
166   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s);
167   while (grpc_is_initialized()) {
168     grpc_maybe_wait_for_async_shutdown();
169     gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
170                                  gpr_time_from_millis(1, GPR_TIMESPAN)));
171     if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
172       return false;
173     }
174   }
175   return true;
176 }
177 
grpc_disable_all_absl_logs()178 void grpc_disable_all_absl_logs() {
179   absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfinity);
180   absl::SetVLogLevel("*grpc*/*", -1);
181 }
182 
grpc_prewarm_os_for_tests()183 void grpc_prewarm_os_for_tests() {
184 #ifdef GPR_WINDOWS
185   // On Windows RBE, c-ares' ares_init_options which internally calls
186   // GetAdaptersAddresses sometimes take >20s to return causing tests to
187   // timeout. This is a hack to prewarm the cache by calling that function
188   // during test setup.
189 #define IPAA_INITIAL_BUF_SZ 15 * 1024
190   ULONG AddrFlags = 0;
191   ULONG Bufsz = IPAA_INITIAL_BUF_SZ;
192   ULONG ReqBufsz = IPAA_INITIAL_BUF_SZ;
193   IP_ADAPTER_ADDRESSES* ipaa;
194   ipaa = static_cast<IP_ADAPTER_ADDRESSES*>(malloc(Bufsz));
195   GetAdaptersAddresses(AF_UNSPEC, AddrFlags, NULL, ipaa, &ReqBufsz);
196   free(ipaa);
197 #endif
198 }
199 
200 namespace grpc {
201 namespace testing {
202 
TestEnvironment(int * argc,char ** argv)203 TestEnvironment::TestEnvironment(int* argc, char** argv) {
204   grpc_test_init(argc, argv);
205 }
206 
~TestEnvironment()207 TestEnvironment::~TestEnvironment() {
208   // This will wait until gRPC shutdown has actually happened to make sure
209   // no gRPC resources (such as thread) are active. (timeout = 10s)
210   if (!grpc_wait_until_shutdown(10)) {
211     LOG(ERROR) << "Timeout in waiting for gRPC shutdown";
212   }
213   if (BuiltUnderMsan()) {
214     // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
215     // running. Although the code above waits until shutdown is done, chances
216     // are that thread itself is still alive. To workaround this problem, this
217     // is going to wait for 0.5 sec to give a chance to the shutdown thread to
218     // exit. https://github.com/grpc/grpc/issues/23695
219     gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
220                                  gpr_time_from_millis(500, GPR_TIMESPAN)));
221   }
222   LOG(INFO) << "TestEnvironment ends";
223 }
224 
TestGrpcScope()225 TestGrpcScope::TestGrpcScope() { grpc_init(); }
226 
~TestGrpcScope()227 TestGrpcScope::~TestGrpcScope() {
228   grpc_shutdown();
229   if (!grpc_wait_until_shutdown(10)) {
230     LOG(ERROR) << "Timeout in waiting for gRPC shutdown";
231   }
232 }
233 
234 }  // namespace testing
235 }  // namespace grpc
236