time_jump_test.cc
Go to the documentation of this file.
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 
19 #include <spawn.h>
20 
21 #include <sstream>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 
26 #include <gtest/gtest.h>
27 
28 #include "absl/time/time.h"
29 
30 #include <grpc/grpc.h>
31 #include <grpc/support/log.h>
32 
40 
41 extern char** environ;
42 
43 #ifdef GPR_ANDROID
44 // Android doesn't have posix_spawn. Use std::system instead
45 void run_cmd(const char* cmd) { std::system(cmd); }
46 #else
47 void run_cmd(const char* cmd) {
48  pid_t pid;
49  const char* argv[] = {const_cast<const char*>("sh"),
50  const_cast<const char*>("-c"), cmd, nullptr};
51  int status;
52 
53  status = posix_spawn(&pid, const_cast<const char*>("/bin/sh"), nullptr,
54  nullptr, const_cast<char**>(argv), environ);
55  if (status == 0) {
56  if (waitpid(pid, &status, 0) == -1) {
57  perror("waitpid");
58  }
59  }
60 }
61 #endif
62 
63 class TimeJumpTest : public ::testing::TestWithParam<std::string> {
64  protected:
65  void SetUp() override {
66  // Skip test if slowdown factor > 1
67  if (grpc_test_slowdown_factor() != 1) {
68  GTEST_SKIP();
69  } else {
70  grpc_init();
71  }
72  }
73  void TearDown() override {
74  // Skip test if slowdown factor > 1
75  if (grpc_test_slowdown_factor() == 1) {
76  run_cmd("sudo sntp -sS pool.ntp.org");
77  grpc_shutdown();
78  }
79  }
80 
81  const int kWaitTimeMs = 1500;
82 };
83 
84 std::vector<std::string> CreateTestScenarios() {
85  return {"-1M", "+1M", "-1H", "+1H", "-1d", "+1d", "-1y", "+1y"};
86 }
89 
90 TEST_P(TimeJumpTest, TimerRunning) {
94  &timer,
97  [](void*, grpc_error_handle error) {
99  },
100  nullptr, grpc_schedule_on_exec_ctx));
102  std::ostringstream cmd;
103  cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
104  run_cmd(cmd.str().c_str());
106  // We expect 1 wakeup/sec when there are not timer expiries
108  gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
109  GPR_ASSERT(wakeups <= 3);
111 }
112 
113 TEST_P(TimeJumpTest, TimedWait) {
116  {
117  grpc_core::MutexLock lock(&mu);
118  std::thread thd = std::thread([]() {
120  std::ostringstream cmd;
121  cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
122  run_cmd(cmd.str().c_str());
123  });
125  bool timedout = cond.WaitWithTimeout(&mu, absl::Milliseconds(kWaitTimeMs));
128  gpr_log(GPR_DEBUG, "After wait, timedout = %d elapsed_ms = %d", timedout,
129  elapsed_ms);
130  GPR_ASSERT(1 == timedout);
131  GPR_ASSERT(1 ==
133  gpr_time_from_millis(kWaitTimeMs, GPR_TIMESPAN),
135 
136  thd.join();
137  }
138  // We expect 1 wakeup/sec when there are not timer expiries
140  gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
141  GPR_ASSERT(wakeups <= 3);
142 }
143 
144 int main(int argc, char** argv) {
145  grpc::testing::TestEnvironment env(&argc, argv);
146  ::testing::InitGoogleTest(&argc, argv);
147  return RUN_ALL_TESTS();
148 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
environ
char ** environ
Definition: bloaty/third_party/googletest/googlemock/test/gmock_leak_test.py:41
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(TimeJump, TimeJumpTest, ::testing::ValuesIn(CreateTestScenarios()))
grpc_core::CondVar
Definition: src/core/lib/gprpp/sync.h:126
log.h
gpr_time_similar
GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold)
Definition: src/core/lib/gpr/time.cc:206
generate.env
env
Definition: generate.py:37
timer_manager.h
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
TEST_P
TEST_P(TimeJumpTest, TimerRunning)
Definition: time_jump_test.cc:90
error
grpc_error_handle error
Definition: retry_filter.cc:499
closure.h
TimeJumpTest::kWaitTimeMs
const int kWaitTimeMs
Definition: time_jump_test.cc:81
status
absl::Status status
Definition: rls.cc:251
GTEST_SKIP
#define GTEST_SKIP()
Definition: perf_counters_gtest.cc:10
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
GRPC_ERROR_CANCELLED
#define GRPC_ERROR_CANCELLED
Definition: error.h:238
grpc_timer
Definition: iomgr/timer.h:33
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
before
IntBeforeRegisterTypedTestSuiteP before
Definition: googletest/googletest/test/gtest-typed-test_test.cc:376
main
int main(int argc, char **argv)
Definition: time_jump_test.cc:144
absl::Milliseconds
constexpr Duration Milliseconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:415
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
run_cmd
void run_cmd(const char *cmd)
Definition: time_jump_test.cc:47
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
cond
static uv_cond_t cond
Definition: threadpool.c:33
grpc_test_slowdown_factor
int64_t grpc_test_slowdown_factor()
Definition: test/core/util/test_config.cc:76
regen-readme.cmd
cmd
Definition: regen-readme.py:21
gpr_time_to_millis
GPRAPI int32_t gpr_time_to_millis(gpr_timespec timespec)
Definition: src/core/lib/gpr/time.cc:221
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
error.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::ExecCtx
Definition: exec_ctx.h:97
after
IntAfterTypedTestSuiteP after
Definition: googletest/googletest/test/gtest-typed-test_test.cc:375
test_config.h
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
TimeJumpTest
Definition: time_jump_test.cc:63
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
TimeJumpTest::TearDown
void TearDown() override
Definition: time_jump_test.cc:73
CreateTestScenarios
std::vector< std::string > CreateTestScenarios()
Definition: time_jump_test.cc:84
exec_ctx.h
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
timer.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
profile_analyzer.thd
thd
Definition: profile_analyzer.py:168
testing::ValuesIn
internal::ParamGenerator< typename std::iterator_traits< ForwardIterator >::value_type > ValuesIn(ForwardIterator begin, ForwardIterator end)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:297
gpr_timespec
Definition: gpr_types.h:50
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
TimeJumpTest::SetUp
void SetUp() override
Definition: time_jump_test.cc:65
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
sync.h
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_timer_manager_get_wakeups_testonly
uint64_t grpc_timer_manager_get_wakeups_testonly(void)
Definition: iomgr/timer_manager.cc:364
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36