| 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 <string> |
| 25 |
|
| 26 |
#include "absl/debugging/failure_signal_handler.h" |
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/strings/match.h" |
| 29 |
#include "absl/strings/string_view.h" |
| 30 |
|
| 31 |
#include <grpc/grpc.h> |
| 32 |
#include <grpc/support/log.h> |
| 33 |
#include <grpc/support/time.h> |
| 34 |
|
| 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 |
|
| 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 |
|
| 52 |
static unsigned seed(void) { return (unsigned)_getpid(); } |
| 53 |
#endif |
| 54 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 { |
| 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 |
|
| 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 |
gpr_log(GPR_ERROR, "%s", engine_set.ToString().c_str()); |
| 113 |
GPR_ASSERT(false); |
| 114 |
} |
| 115 |
// remove the spent argv |
| 116 |
RmArg(i, argc, argv); |
| 117 |
continue; |
| 118 |
} |
| 119 |
++i; |
| 120 |
} |
| 121 |
} |
| 122 |
} // namespace |
| 123 |
|
| 124 |
void grpc_test_init(int* argc, char** argv) { |
| 125 |
gpr_log_verbosity_init(); |
| 126 |
ParseTestArgs(argc, argv); |
| 127 |
grpc_core::testing::InitializeStackTracer(argv[0]); |
| 128 |
absl::FailureSignalHandlerOptions options; |
| 129 |
absl::InstallFailureSignalHandler(options); |
| 130 |
gpr_log(GPR_DEBUG, |
| 131 |
"test slowdown factor: sanitizer=%" PRId64 ", fixture=%" PRId64 |
| 132 |
", poller=%" PRId64 ", total=%" PRId64, |
| 133 |
grpc_test_sanitizer_slowdown_factor(), g_fixture_slowdown_factor, |
| 134 |
g_poller_slowdown_factor, grpc_test_slowdown_factor()); |
| 135 |
/* seed rng with pid, so we don't end up with the same random numbers as a |
| 136 |
concurrently running test binary */ |
| 137 |
srand(seed()); |
| 138 |
} |
| 139 |
|
| 140 |
bool grpc_wait_until_shutdown(int64_t time_s) { |
| 141 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s); |
| 142 |
while (grpc_is_initialized()) { |
| 143 |
grpc_maybe_wait_for_async_shutdown(); |
| 144 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 145 |
gpr_time_from_millis(1, GPR_TIMESPAN))); |
| 146 |
if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
} |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
namespace grpc { |
| 154 |
namespace testing { |
| 155 |
|
| 156 |
TestEnvironment::TestEnvironment(int* argc, char** argv) { |
| 157 |
grpc_test_init(argc, argv); |
| 158 |
} |
| 159 |
|
| 160 |
TestEnvironment::~TestEnvironment() { |
| 161 |
// This will wait until gRPC shutdown has actually happened to make sure |
| 162 |
// no gRPC resources (such as thread) are active. (timeout = 10s) |
| 163 |
if (!grpc_wait_until_shutdown(10)) { |
| 164 |
gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown"); |
| 165 |
} |
| 166 |
if (BuiltUnderMsan()) { |
| 167 |
// This is a workaround for MSAN. MSAN doesn't like having shutdown thread |
| 168 |
// running. Although the code above waits until shutdown is done, chances |
| 169 |
// are that thread itself is still alive. To workaround this problem, this |
| 170 |
// is going to wait for 0.5 sec to give a chance to the shutdown thread to |
| 171 |
// exit. https://github.com/grpc/grpc/issues/23695 |
| 172 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 173 |
gpr_time_from_millis(500, GPR_TIMESPAN))); |
| 174 |
} |
| 175 |
gpr_log(GPR_INFO, "TestEnvironment ends"); |
| 176 |
} |
| 177 |
|
| 178 |
TestGrpcScope::TestGrpcScope() { grpc_init(); } |
| 179 |
|
| 180 |
TestGrpcScope::~TestGrpcScope() { |
| 181 |
grpc_shutdown(); |
| 182 |
if (!grpc_wait_until_shutdown(10)) { |
| 183 |
gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown"); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
} // namespace testing |
| 188 |
} // namespace grpc |