fork_test.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. *
  3. * Copyright 2017 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 "src/core/lib/gprpp/fork.h"
  19. #include "src/core/lib/gprpp/thd.h"
  20. #include "test/core/util/test_config.h"
  21. static void test_init() {
  22. GPR_ASSERT(!grpc_core::Fork::Enabled());
  23. // Default fork support (disabled)
  24. grpc_core::Fork::GlobalInit();
  25. GPR_ASSERT(!grpc_core::Fork::Enabled());
  26. grpc_core::Fork::GlobalShutdown();
  27. // Explicitly disabled fork support
  28. grpc_core::Fork::Enable(false);
  29. grpc_core::Fork::GlobalInit();
  30. GPR_ASSERT(!grpc_core::Fork::Enabled());
  31. grpc_core::Fork::GlobalShutdown();
  32. // Explicitly enabled fork support
  33. grpc_core::Fork::Enable(true);
  34. grpc_core::Fork::GlobalInit();
  35. GPR_ASSERT(grpc_core::Fork::Enabled());
  36. grpc_core::Fork::GlobalShutdown();
  37. }
  38. // This spawns CONCURRENT_TEST_THREADS that last up to
  39. // THREAD_DELAY_MS, and checks that the Fork::AwaitThreads()
  40. // returns roughly after THREAD_DELAY_MS. The epsilon is high
  41. // because tsan threads can take a while to spawn/join.
  42. #define THREAD_DELAY_MS 6000
  43. #define THREAD_DELAY_EPSILON 1500
  44. #define CONCURRENT_TEST_THREADS 100
  45. static void sleeping_thd(void* arg) {
  46. int64_t sleep_ms = reinterpret_cast<int64_t>(arg);
  47. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  48. gpr_time_from_millis(sleep_ms, GPR_TIMESPAN)));
  49. }
  50. static void test_thd_count() {
  51. // Test no active threads
  52. grpc_core::Fork::Enable(true);
  53. grpc_core::Fork::GlobalInit();
  54. grpc_core::Fork::AwaitThreads();
  55. grpc_core::Fork::GlobalShutdown();
  56. grpc_core::Fork::Enable(true);
  57. grpc_core::Fork::GlobalInit();
  58. grpc_core::Thread thds[CONCURRENT_TEST_THREADS];
  59. gpr_timespec est_end_time =
  60. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  61. gpr_time_from_millis(THREAD_DELAY_MS, GPR_TIMESPAN));
  62. gpr_timespec tolerance =
  63. gpr_time_from_millis(THREAD_DELAY_EPSILON, GPR_TIMESPAN);
  64. for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) {
  65. intptr_t sleep_time_ms =
  66. (i * THREAD_DELAY_MS) / (CONCURRENT_TEST_THREADS - 1);
  67. thds[i] = grpc_core::Thread("grpc_fork_test", sleeping_thd,
  68. reinterpret_cast<void*>(sleep_time_ms));
  69. thds[i].Start();
  70. }
  71. grpc_core::Fork::AwaitThreads();
  72. gpr_timespec end_time = gpr_now(GPR_CLOCK_REALTIME);
  73. for (auto& thd : thds) {
  74. thd.Join();
  75. }
  76. GPR_ASSERT(gpr_time_similar(end_time, est_end_time, tolerance));
  77. grpc_core::Fork::GlobalShutdown();
  78. }
  79. static void exec_ctx_thread(void* arg) {
  80. bool* exec_ctx_created = static_cast<bool*>(arg);
  81. grpc_core::Fork::IncExecCtxCount();
  82. *exec_ctx_created = true;
  83. }
  84. static void test_exec_count() {
  85. grpc_core::Fork::Enable(true);
  86. grpc_core::Fork::GlobalInit();
  87. grpc_core::Fork::IncExecCtxCount();
  88. GPR_ASSERT(grpc_core::Fork::BlockExecCtx());
  89. grpc_core::Fork::DecExecCtxCount();
  90. grpc_core::Fork::AllowExecCtx();
  91. grpc_core::Fork::IncExecCtxCount();
  92. grpc_core::Fork::IncExecCtxCount();
  93. GPR_ASSERT(!grpc_core::Fork::BlockExecCtx());
  94. grpc_core::Fork::DecExecCtxCount();
  95. grpc_core::Fork::DecExecCtxCount();
  96. grpc_core::Fork::IncExecCtxCount();
  97. GPR_ASSERT(grpc_core::Fork::BlockExecCtx());
  98. grpc_core::Fork::DecExecCtxCount();
  99. grpc_core::Fork::AllowExecCtx();
  100. // Test that block_exec_ctx() blocks grpc_core::Fork::IncExecCtxCount
  101. bool exec_ctx_created = false;
  102. grpc_core::Thread thd =
  103. grpc_core::Thread("grpc_fork_test", exec_ctx_thread, &exec_ctx_created);
  104. grpc_core::Fork::IncExecCtxCount();
  105. GPR_ASSERT(grpc_core::Fork::BlockExecCtx());
  106. grpc_core::Fork::DecExecCtxCount();
  107. thd.Start();
  108. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  109. gpr_time_from_seconds(1, GPR_TIMESPAN)));
  110. GPR_ASSERT(!exec_ctx_created);
  111. grpc_core::Fork::AllowExecCtx();
  112. thd.Join(); // This ensure that the call got un-blocked
  113. grpc_core::Fork::GlobalShutdown();
  114. }
  115. int main(int argc, char* argv[]) {
  116. grpc::testing::TestEnvironment env(argc, argv);
  117. test_init();
  118. test_thd_count();
  119. test_exec_count();
  120. return 0;
  121. }