time_jump_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <spawn.h>
  19. #include <sstream>
  20. #include <string>
  21. #include <thread>
  22. #include <vector>
  23. #include <gtest/gtest.h>
  24. #include "absl/time/time.h"
  25. #include <grpc/grpc.h>
  26. #include <grpc/support/log.h>
  27. #include "src/core/lib/gprpp/sync.h"
  28. #include "src/core/lib/iomgr/closure.h"
  29. #include "src/core/lib/iomgr/error.h"
  30. #include "src/core/lib/iomgr/exec_ctx.h"
  31. #include "src/core/lib/iomgr/timer.h"
  32. #include "src/core/lib/iomgr/timer_manager.h"
  33. #include "test/core/util/test_config.h"
  34. extern char** environ;
  35. #ifdef GPR_ANDROID
  36. // Android doesn't have posix_spawn. Use std::system instead
  37. void run_cmd(const char* cmd) { std::system(cmd); }
  38. #else
  39. void run_cmd(const char* cmd) {
  40. pid_t pid;
  41. const char* argv[] = {const_cast<const char*>("sh"),
  42. const_cast<const char*>("-c"), cmd, nullptr};
  43. int status;
  44. status = posix_spawn(&pid, const_cast<const char*>("/bin/sh"), nullptr,
  45. nullptr, const_cast<char**>(argv), environ);
  46. if (status == 0) {
  47. if (waitpid(pid, &status, 0) == -1) {
  48. perror("waitpid");
  49. }
  50. }
  51. }
  52. #endif
  53. class TimeJumpTest : public ::testing::TestWithParam<std::string> {
  54. protected:
  55. void SetUp() override {
  56. // Skip test if slowdown factor > 1
  57. if (grpc_test_slowdown_factor() != 1) {
  58. GTEST_SKIP();
  59. } else {
  60. grpc_init();
  61. }
  62. }
  63. void TearDown() override {
  64. // Skip test if slowdown factor > 1
  65. if (grpc_test_slowdown_factor() == 1) {
  66. run_cmd("sudo sntp -sS pool.ntp.org");
  67. grpc_shutdown();
  68. }
  69. }
  70. const int kWaitTimeMs = 1500;
  71. };
  72. std::vector<std::string> CreateTestScenarios() {
  73. return {"-1M", "+1M", "-1H", "+1H", "-1d", "+1d", "-1y", "+1y"};
  74. }
  75. INSTANTIATE_TEST_SUITE_P(TimeJump, TimeJumpTest,
  76. ::testing::ValuesIn(CreateTestScenarios()));
  77. TEST_P(TimeJumpTest, TimerRunning) {
  78. grpc_core::ExecCtx exec_ctx;
  79. grpc_timer timer;
  80. grpc_timer_init(
  81. &timer,
  82. grpc_core::ExecCtx::Get()->Now() + grpc_core::Duration::Seconds(3),
  83. GRPC_CLOSURE_CREATE(
  84. [](void*, grpc_error_handle error) {
  85. GPR_ASSERT(error == GRPC_ERROR_CANCELLED);
  86. },
  87. nullptr, grpc_schedule_on_exec_ctx));
  88. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  89. std::ostringstream cmd;
  90. cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
  91. run_cmd(cmd.str().c_str());
  92. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(kWaitTimeMs));
  93. // We expect 1 wakeup/sec when there are not timer expiries
  94. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  95. gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
  96. GPR_ASSERT(wakeups <= 3);
  97. grpc_timer_cancel(&timer);
  98. }
  99. TEST_P(TimeJumpTest, TimedWait) {
  100. grpc_core::CondVar cond;
  101. grpc_core::Mutex mu;
  102. {
  103. grpc_core::MutexLock lock(&mu);
  104. std::thread thd = std::thread([]() {
  105. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  106. std::ostringstream cmd;
  107. cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
  108. run_cmd(cmd.str().c_str());
  109. });
  110. gpr_timespec before = gpr_now(GPR_CLOCK_MONOTONIC);
  111. bool timedout = cond.WaitWithTimeout(&mu, absl::Milliseconds(kWaitTimeMs));
  112. gpr_timespec after = gpr_now(GPR_CLOCK_MONOTONIC);
  113. int32_t elapsed_ms = gpr_time_to_millis(gpr_time_sub(after, before));
  114. gpr_log(GPR_DEBUG, "After wait, timedout = %d elapsed_ms = %d", timedout,
  115. elapsed_ms);
  116. GPR_ASSERT(1 == timedout);
  117. GPR_ASSERT(1 ==
  118. gpr_time_similar(gpr_time_sub(after, before),
  119. gpr_time_from_millis(kWaitTimeMs, GPR_TIMESPAN),
  120. gpr_time_from_millis(50, GPR_TIMESPAN)));
  121. thd.join();
  122. }
  123. // We expect 1 wakeup/sec when there are not timer expiries
  124. int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  125. gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
  126. GPR_ASSERT(wakeups <= 3);
  127. }
  128. int main(int argc, char** argv) {
  129. grpc::testing::TestEnvironment env(argc, argv);
  130. ::testing::InitGoogleTest(&argc, argv);
  131. return RUN_ALL_TESTS();
  132. }