test/core/util/test_config.cc
Go to the documentation of this file.
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 
20 
21 #include <inttypes.h>
22 #include <signal.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "absl/debugging/failure_signal_handler.h"
29 #include "absl/debugging/symbolize.h"
30 #include "absl/strings/match.h"
31 #include "absl/strings/strip.h"
32 
33 #include <grpc/grpc.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 
38 #include "src/core/lib/gpr/env.h"
44 #include "test/core/util/build.h"
46 
49 
50 #if GPR_GETPID_IN_UNISTD_H
51 #include <unistd.h>
52 static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
53 #endif
54 
55 #if GPR_GETPID_IN_PROCESS_H
56 #include <process.h>
57 static unsigned seed(void) { return (unsigned)_getpid(); }
58 #endif
59 
61  int64_t sanitizer_multiplier = 1;
62  if (BuiltUnderValgrind()) {
63  sanitizer_multiplier = 20;
64  } else if (BuiltUnderTsan()) {
65  sanitizer_multiplier = 5;
66  } else if (BuiltUnderAsan()) {
67  sanitizer_multiplier = 3;
68  } else if (BuiltUnderMsan()) {
69  sanitizer_multiplier = 4;
70  } else if (BuiltUnderUbsan()) {
71  sanitizer_multiplier = 5;
72  }
73  return sanitizer_multiplier;
74 }
75 
79 }
80 
82  return gpr_time_add(
85  grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
86  GPR_TIMESPAN));
87 }
88 
90  return gpr_time_add(
93  grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
94  GPR_TIMESPAN));
95 }
96 
97 namespace {
98 void RmArg(int i, int* argc, char** argv) {
99  --(*argc);
100  if (i < *argc) {
101  memmove(argv + i, argv + i + 1, *argc - i);
102  }
103 }
104 
105 void ParseTestArgs(int* argc, char** argv) {
106  if (argc == nullptr || *argc <= 1) return;
107  // flags to look for and consume
108  const absl::string_view poller_flag{"--poller="};
109  const absl::string_view engine_flag{"--engine="};
110  int i = 1;
111  while (i < *argc) {
112  if (absl::StartsWith(argv[i], poller_flag)) {
113  gpr_setenv("GRPC_POLL_STRATEGY", argv[i] + poller_flag.length());
114  // remove the spent argv
115  RmArg(i, argc, argv);
116  continue;
117  }
118  if (absl::StartsWith(argv[i], engine_flag)) {
119  absl::Status engine_set =
121  argv[i] + engine_flag.length());
122  if (!engine_set.ok()) {
123  gpr_log(GPR_ERROR, "%s", engine_set.ToString().c_str());
124  GPR_ASSERT(false);
125  }
126  // remove the spent argv
127  RmArg(i, argc, argv);
128  continue;
129  }
130  ++i;
131  }
132 }
133 } // namespace
134 
135 void grpc_test_init(int* argc, char** argv) {
137  ParseTestArgs(argc, argv);
142  "test slowdown factor: sanitizer=%" PRId64 ", fixture=%" PRId64
143  ", poller=%" PRId64 ", total=%" PRId64,
146  /* seed rng with pid, so we don't end up with the same random numbers as a
147  concurrently running test binary */
148  srand(seed());
149 }
150 
153  while (grpc_is_initialized()) {
157  if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
158  return false;
159  }
160  }
161  return true;
162 }
163 
164 namespace grpc {
165 namespace testing {
166 
167 TestEnvironment::TestEnvironment(int* argc, char** argv) {
168  grpc_test_init(argc, argv);
169 }
170 
172  // This will wait until gRPC shutdown has actually happened to make sure
173  // no gRPC resources (such as thread) are active. (timeout = 10s)
174  if (!grpc_wait_until_shutdown(10)) {
175  gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
176  }
177  if (BuiltUnderMsan()) {
178  // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
179  // running. Although the code above waits until shutdown is done, chances
180  // are that thread itself is still alive. To workaround this problem, this
181  // is going to wait for 0.5 sec to give a chance to the shutdown thread to
182  // exit. https://github.com/grpc/grpc/issues/23695
185  }
186  gpr_log(GPR_INFO, "TestEnvironment ends");
187 }
188 
190 
192  grpc_shutdown();
193  if (!grpc_wait_until_shutdown(10)) {
194  gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
195  }
196 }
197 
198 } // namespace testing
199 } // namespace grpc
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
grpc_test_sanitizer_slowdown_factor
int64_t grpc_test_sanitizer_slowdown_factor()
Definition: test/core/util/test_config.cc:60
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
test_init.h
absl::Status::ToString
std::string ToString(StatusToStringMode mode=StatusToStringMode::kDefault) const
Definition: third_party/abseil-cpp/absl/status/status.h:821
gpr_log_verbosity_init
GPRAPI void gpr_log_verbosity_init(void)
Definition: log.cc:115
BuiltUnderAsan
bool BuiltUnderAsan()
Definition: build.cc:39
grpc
Definition: grpcpp/alarm.h:33
grpc_event_engine::experimental::InitializeTestingEventEngineFactory
absl::Status InitializeTestingEventEngineFactory(absl::string_view engine)
Definition: test_init.cc:28
string.h
options
double_dict options[]
Definition: capstone_test.c:55
absl::FailureSignalHandlerOptions
Definition: abseil-cpp/absl/debugging/failure_signal_handler.h:56
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
absl::StartsWith
bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:58
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
useful.h
env.h
grpc::testing::TestGrpcScope::TestGrpcScope
TestGrpcScope()
Definition: test/core/util/test_config.cc:189
stack_tracer.h
grpc_test_init
void grpc_test_init(int *argc, char **argv)
Definition: test/core/util/test_config.cc:135
BuiltUnderValgrind
bool BuiltUnderValgrind()
Definition: build.cc:15
build.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
g_poller_slowdown_factor
int64_t g_poller_slowdown_factor
Definition: test/core/util/test_config.cc:48
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
init.h
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
absl::InstallFailureSignalHandler
void InstallFailureSignalHandler(const FailureSignalHandlerOptions &options)
Definition: abseil-cpp/absl/debugging/failure_signal_handler.cc:378
grpc_test_slowdown_factor
int64_t grpc_test_slowdown_factor()
Definition: test/core/util/test_config.cc:76
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
examine_stack.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc::testing::TestEnvironment::~TestEnvironment
~TestEnvironment()
Definition: test/core/util/test_config.cc:171
grpc::testing::TestEnvironment::TestEnvironment
TestEnvironment(int *argc, char **argv)
Definition: test/core/util/test_config.cc:167
gpr_types.h
test_config.h
grpc_core::testing::InitializeStackTracer
void InitializeStackTracer(const char *argv0)
Definition: stack_tracer.cc:103
grpc_is_initialized
GRPCAPI int grpc_is_initialized(void)
Definition: init.cc:247
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
BuiltUnderMsan
bool BuiltUnderMsan()
Definition: build.cc:55
grpc::testing::TestGrpcScope::~TestGrpcScope
~TestGrpcScope()
Definition: test/core/util/test_config.cc:191
g_fixture_slowdown_factor
int64_t g_fixture_slowdown_factor
Definition: test/core/util/test_config.cc:47
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
gpr_time_from_micros
GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:115
alloc.h
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc_maybe_wait_for_async_shutdown
void grpc_maybe_wait_for_async_shutdown(void)
Definition: init.cc:255
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
BuiltUnderTsan
bool BuiltUnderTsan()
Definition: build.cc:23
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
gpr_timespec
Definition: gpr_types.h:50
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
BuiltUnderUbsan
bool BuiltUnderUbsan()
Definition: build.cc:71
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc_wait_until_shutdown
bool grpc_wait_until_shutdown(int64_t time_s)
Definition: test/core/util/test_config.cc:151
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_setenv
void gpr_setenv(const char *name, const char *value)


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:27