test_config.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "test/core/util/test_config.h"
  19. #include <inttypes.h>
  20. #include <signal.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "absl/debugging/failure_signal_handler.h"
  26. #include "absl/debugging/symbolize.h"
  27. #include <grpc/grpc.h>
  28. #include <grpc/impl/codegen/gpr_types.h>
  29. #include <grpc/support/alloc.h>
  30. #include <grpc/support/log.h>
  31. #include "src/core/lib/gpr/string.h"
  32. #include "src/core/lib/gpr/useful.h"
  33. #include "src/core/lib/gprpp/examine_stack.h"
  34. #include "src/core/lib/surface/init.h"
  35. #include "test/core/util/build.h"
  36. #include "test/core/util/stack_tracer.h"
  37. int64_t g_fixture_slowdown_factor = 1;
  38. int64_t g_poller_slowdown_factor = 1;
  39. #if GPR_GETPID_IN_UNISTD_H
  40. #include <unistd.h>
  41. static unsigned seed(void) { return static_cast<unsigned>(getpid()); }
  42. #endif
  43. #if GPR_GETPID_IN_PROCESS_H
  44. #include <process.h>
  45. static unsigned seed(void) { return (unsigned)_getpid(); }
  46. #endif
  47. int64_t grpc_test_sanitizer_slowdown_factor() {
  48. int64_t sanitizer_multiplier = 1;
  49. if (BuiltUnderValgrind()) {
  50. sanitizer_multiplier = 20;
  51. } else if (BuiltUnderTsan()) {
  52. sanitizer_multiplier = 5;
  53. } else if (BuiltUnderAsan()) {
  54. sanitizer_multiplier = 3;
  55. } else if (BuiltUnderMsan()) {
  56. sanitizer_multiplier = 4;
  57. } else if (BuiltUnderUbsan()) {
  58. sanitizer_multiplier = 5;
  59. }
  60. return sanitizer_multiplier;
  61. }
  62. int64_t grpc_test_slowdown_factor() {
  63. return grpc_test_sanitizer_slowdown_factor() * g_fixture_slowdown_factor *
  64. g_poller_slowdown_factor;
  65. }
  66. gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s) {
  67. return gpr_time_add(
  68. gpr_now(GPR_CLOCK_MONOTONIC),
  69. gpr_time_from_millis(
  70. grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_s,
  71. GPR_TIMESPAN));
  72. }
  73. gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms) {
  74. return gpr_time_add(
  75. gpr_now(GPR_CLOCK_MONOTONIC),
  76. gpr_time_from_micros(
  77. grpc_test_slowdown_factor() * static_cast<int64_t>(1e3) * time_ms,
  78. GPR_TIMESPAN));
  79. }
  80. void grpc_test_init(int /*argc*/, char** argv) {
  81. grpc_core::testing::InitializeStackTracer(argv[0]);
  82. absl::FailureSignalHandlerOptions options;
  83. absl::InstallFailureSignalHandler(options);
  84. gpr_log_verbosity_init();
  85. gpr_log(GPR_DEBUG,
  86. "test slowdown factor: sanitizer=%" PRId64 ", fixture=%" PRId64
  87. ", poller=%" PRId64 ", total=%" PRId64,
  88. grpc_test_sanitizer_slowdown_factor(), g_fixture_slowdown_factor,
  89. g_poller_slowdown_factor, grpc_test_slowdown_factor());
  90. /* seed rng with pid, so we don't end up with the same random numbers as a
  91. concurrently running test binary */
  92. srand(seed());
  93. }
  94. bool grpc_wait_until_shutdown(int64_t time_s) {
  95. gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s);
  96. while (grpc_is_initialized()) {
  97. grpc_maybe_wait_for_async_shutdown();
  98. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  99. gpr_time_from_millis(1, GPR_TIMESPAN)));
  100. if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. namespace grpc {
  107. namespace testing {
  108. TestEnvironment::TestEnvironment(int argc, char** argv) {
  109. grpc_test_init(argc, argv);
  110. }
  111. TestEnvironment::~TestEnvironment() {
  112. // This will wait until gRPC shutdown has actually happened to make sure
  113. // no gRPC resources (such as thread) are active. (timeout = 10s)
  114. if (!grpc_wait_until_shutdown(10)) {
  115. gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
  116. }
  117. if (BuiltUnderMsan()) {
  118. // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
  119. // running. Although the code above waits until shutdown is done, chances
  120. // are that thread itself is still alive. To workaround this problem, this
  121. // is going to wait for 0.5 sec to give a chance to the shutdown thread to
  122. // exit. https://github.com/grpc/grpc/issues/23695
  123. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  124. gpr_time_from_millis(500, GPR_TIMESPAN)));
  125. }
  126. gpr_log(GPR_INFO, "TestEnvironment ends");
  127. }
  128. TestGrpcScope::TestGrpcScope() { grpc_init(); }
  129. TestGrpcScope::~TestGrpcScope() {
  130. grpc_shutdown();
  131. if (!grpc_wait_until_shutdown(10)) {
  132. gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
  133. }
  134. }
  135. } // namespace testing
  136. } // namespace grpc