timer_test.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. *
  3. * Copyright 2019 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/iomgr/timer.h"
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/gprpp/time.h"
  23. #include "src/core/lib/iomgr/closure.h"
  24. #include "src/core/lib/iomgr/error.h"
  25. #include "src/core/lib/iomgr/exec_ctx.h"
  26. #include "src/core/lib/iomgr/timer_manager.h"
  27. #include "test/core/util/test_config.h"
  28. #ifdef GRPC_POSIX_SOCKET_EV
  29. #include "src/core/lib/iomgr/ev_posix.h"
  30. #endif
  31. // MAYBE_SKIP_TEST is a macro to determine if this particular test configuration
  32. // should be skipped based on a decision made at SetUp time.
  33. #define MAYBE_SKIP_TEST \
  34. do { \
  35. if (do_not_test_) { \
  36. return; \
  37. } \
  38. } while (0)
  39. class TimerTest : public ::testing::Test {
  40. protected:
  41. void SetUp() override {
  42. grpc_init();
  43. // Skip test if slowdown factor > 1, or we are
  44. // using event manager.
  45. #ifdef GRPC_POSIX_SOCKET_EV
  46. if (grpc_test_slowdown_factor() != 1 ||
  47. grpc_event_engine_run_in_background()) {
  48. #else
  49. if (grpc_test_slowdown_factor() != 1) {
  50. #endif
  51. do_not_test_ = true;
  52. }
  53. }
  54. void TearDown() override { grpc_shutdown(); }
  55. bool do_not_test_{false};
  56. };
  57. #ifndef GPR_WINDOWS
  58. // the test fails with too many wakeups on windows opt build
  59. // the mechanism by which that happens is described in
  60. // https://github.com/grpc/grpc/issues/20436
  61. TEST_F(TimerTest, NoTimers) {
  62. MAYBE_SKIP_TEST;
  63. grpc_core::ExecCtx exec_ctx;
  64. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  65. // We expect to get 1 wakeup per second. Sometimes we also get a wakeup
  66. // during initialization, so in 1.5 seconds we expect to get 1 or 2 wakeups.
  67. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  68. GPR_ASSERT(wakeups == 1 || wakeups == 2);
  69. }
  70. #endif
  71. TEST_F(TimerTest, OneTimerExpires) {
  72. MAYBE_SKIP_TEST;
  73. grpc_core::ExecCtx exec_ctx;
  74. grpc_timer timer;
  75. int timer_fired = 0;
  76. grpc_timer_init(
  77. &timer,
  78. grpc_core::ExecCtx::Get()->Now() + grpc_core::Duration::Milliseconds(500),
  79. GRPC_CLOSURE_CREATE(
  80. [](void* arg, grpc_error_handle) {
  81. int* timer_fired = static_cast<int*>(arg);
  82. ++*timer_fired;
  83. },
  84. &timer_fired, grpc_schedule_on_exec_ctx));
  85. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  86. GPR_ASSERT(1 == timer_fired);
  87. // We expect to get 1 wakeup/second + 1 wakeup for the expired timer + maybe 1
  88. // wakeup during initialization. i.e. in 1.5 seconds we expect 2 or 3 wakeups.
  89. // Actual number of wakeups is more due to bug
  90. // https://github.com/grpc/grpc/issues/19947
  91. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  92. gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
  93. }
  94. TEST_F(TimerTest, MultipleTimersExpire) {
  95. MAYBE_SKIP_TEST;
  96. grpc_core::ExecCtx exec_ctx;
  97. const int kNumTimers = 10;
  98. grpc_timer timers[kNumTimers];
  99. int timer_fired = 0;
  100. for (int i = 0; i < kNumTimers; ++i) {
  101. grpc_timer_init(&timers[i],
  102. grpc_core::ExecCtx::Get()->Now() +
  103. grpc_core::Duration::Milliseconds(500) +
  104. grpc_core::Duration::Milliseconds(i),
  105. GRPC_CLOSURE_CREATE(
  106. [](void* arg, grpc_error_handle) {
  107. int* timer_fired = static_cast<int*>(arg);
  108. ++*timer_fired;
  109. },
  110. &timer_fired, grpc_schedule_on_exec_ctx));
  111. }
  112. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  113. GPR_ASSERT(kNumTimers == timer_fired);
  114. // We expect to get 1 wakeup/second + 1 wakeup for per timer fired + maybe 1
  115. // wakeup during initialization. i.e. in 1.5 seconds we expect 11 or 12
  116. // wakeups. Actual number of wakeups is more due to bug
  117. // https://github.com/grpc/grpc/issues/19947
  118. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  119. gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
  120. }
  121. TEST_F(TimerTest, CancelSomeTimers) {
  122. MAYBE_SKIP_TEST;
  123. grpc_core::ExecCtx exec_ctx;
  124. const int kNumTimers = 10;
  125. grpc_timer timers[kNumTimers];
  126. int timer_fired = 0;
  127. for (int i = 0; i < kNumTimers; ++i) {
  128. grpc_timer_init(&timers[i],
  129. grpc_core::ExecCtx::Get()->Now() +
  130. grpc_core::Duration::Milliseconds(500) +
  131. grpc_core::Duration::Milliseconds(i),
  132. GRPC_CLOSURE_CREATE(
  133. [](void* arg, grpc_error_handle error) {
  134. if (error == GRPC_ERROR_CANCELLED) {
  135. return;
  136. }
  137. int* timer_fired = static_cast<int*>(arg);
  138. ++*timer_fired;
  139. },
  140. &timer_fired, grpc_schedule_on_exec_ctx));
  141. }
  142. for (int i = 0; i < kNumTimers / 2; ++i) {
  143. grpc_timer_cancel(&timers[i]);
  144. }
  145. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  146. GPR_ASSERT(kNumTimers / 2 == timer_fired);
  147. // We expect to get 1 wakeup/second + 1 wakeup per timer fired + maybe 1
  148. // wakeup during initialization. i.e. in 1.5 seconds we expect 6 or 7 wakeups.
  149. // Actual number of wakeups is more due to bug
  150. // https://github.com/grpc/grpc/issues/19947
  151. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  152. gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
  153. }
  154. // Enable the following test after
  155. // https://github.com/grpc/grpc/issues/20049 has been fixed.
  156. TEST_F(TimerTest, DISABLED_TimerNotCanceled) {
  157. grpc_core::ExecCtx exec_ctx;
  158. grpc_timer timer;
  159. grpc_timer_init(
  160. &timer,
  161. grpc_core::ExecCtx::Get()->Now() + grpc_core::Duration::Seconds(10),
  162. GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr,
  163. grpc_schedule_on_exec_ctx));
  164. }
  165. // Enable the following test after
  166. // https://github.com/grpc/grpc/issues/20064 has been fixed.
  167. TEST_F(TimerTest, DISABLED_CancelRace) {
  168. MAYBE_SKIP_TEST;
  169. grpc_core::ExecCtx exec_ctx;
  170. const int kNumTimers = 10;
  171. grpc_timer timers[kNumTimers];
  172. for (int i = 0; i < kNumTimers; ++i) {
  173. grpc_timer* arg = (i != 0) ? &timers[i - 1] : nullptr;
  174. grpc_timer_init(&timers[i],
  175. grpc_core::ExecCtx::Get()->Now() +
  176. grpc_core::Duration::Milliseconds(100),
  177. GRPC_CLOSURE_CREATE(
  178. [](void* arg, grpc_error_handle /*error*/) {
  179. grpc_timer* timer = static_cast<grpc_timer*>(arg);
  180. if (timer) {
  181. grpc_timer_cancel(timer);
  182. }
  183. },
  184. arg, grpc_schedule_on_exec_ctx));
  185. }
  186. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  187. }
  188. // Enable the following test after
  189. // https://github.com/grpc/grpc/issues/20066 has been fixed.
  190. TEST_F(TimerTest, DISABLED_CancelNextTimer) {
  191. MAYBE_SKIP_TEST;
  192. grpc_core::ExecCtx exec_ctx;
  193. const int kNumTimers = 10;
  194. grpc_timer timers[kNumTimers];
  195. for (int i = 0; i < kNumTimers; ++i) {
  196. grpc_timer_init_unset(&timers[i]);
  197. }
  198. for (int i = 0; i < kNumTimers; ++i) {
  199. grpc_timer* arg = nullptr;
  200. if (i < kNumTimers - 1) {
  201. arg = &timers[i + 1];
  202. }
  203. grpc_timer_init(&timers[i],
  204. grpc_core::ExecCtx::Get()->Now() +
  205. grpc_core::Duration::Milliseconds(100),
  206. GRPC_CLOSURE_CREATE(
  207. [](void* arg, grpc_error_handle /*error*/) {
  208. grpc_timer* timer = static_cast<grpc_timer*>(arg);
  209. if (timer) {
  210. grpc_timer_cancel(timer);
  211. }
  212. },
  213. arg, grpc_schedule_on_exec_ctx));
  214. }
  215. grpc_timer_cancel(&timers[0]);
  216. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  217. }
  218. int main(int argc, char** argv) {
  219. grpc::testing::TestEnvironment env(argc, argv);
  220. ::testing::InitGoogleTest(&argc, argv);
  221. return RUN_ALL_TESTS();
  222. }