Debian Codegraph @

test/cpp/common/time_jump_test.cc

raw
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
33 #include "src/core/lib/gprpp/sync.h"
34 #include "src/core/lib/iomgr/closure.h"
35 #include "src/core/lib/iomgr/error.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/iomgr/timer.h"
38 #include "src/core/lib/iomgr/timer_manager.h"
39 #include "test/core/util/test_config.h"
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 }
87 INSTANTIATE_TEST_SUITE_P(TimeJump, TimeJumpTest,
88 ::testing::ValuesIn(CreateTestScenarios()));
89
90 TEST_P(TimeJumpTest, TimerRunning) {
91 grpc_core::ExecCtx exec_ctx;
92 grpc_timer timer;
93 grpc_timer_init(&timer,
94 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(3),
95 GRPC_CLOSURE_CREATE(
96 [](void*, grpc_error_handle error) {
97 GPR_ASSERT(error == absl::CancelledError());
98 },
99 nullptr, grpc_schedule_on_exec_ctx));
100 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
101 std::ostringstream cmd;
102 cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
103 run_cmd(cmd.str().c_str());
104 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(kWaitTimeMs));
105 // We expect 1 wakeup/sec when there are not timer expiries
106 int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
107 gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
108 GPR_ASSERT(wakeups <= 3);
109 grpc_timer_cancel(&timer);
110 }
111
112 TEST_P(TimeJumpTest, TimedWait) {
113 grpc_core::CondVar cond;
114 grpc_core::Mutex mu;
115 {
116 grpc_core::MutexLock lock(&mu);
117 std::thread thd = std::thread([]() {
118 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
119 std::ostringstream cmd;
120 cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
121 run_cmd(cmd.str().c_str());
122 });
123 gpr_timespec before = gpr_now(GPR_CLOCK_MONOTONIC);
124 bool timedout = cond.WaitWithTimeout(&mu, absl::Milliseconds(kWaitTimeMs));
125 gpr_timespec after = gpr_now(GPR_CLOCK_MONOTONIC);
126 int32_t elapsed_ms = gpr_time_to_millis(gpr_time_sub(after, before));
127 gpr_log(GPR_DEBUG, "After wait, timedout = %d elapsed_ms = %d", timedout,
128 elapsed_ms);
129 GPR_ASSERT(1 == timedout);
130 GPR_ASSERT(1 ==
131 gpr_time_similar(gpr_time_sub(after, before),
132 gpr_time_from_millis(kWaitTimeMs, GPR_TIMESPAN),
133 gpr_time_from_millis(50, GPR_TIMESPAN)));
134
135 thd.join();
136 }
137 // We expect 1 wakeup/sec when there are not timer expiries
138 int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
139 gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
140 GPR_ASSERT(wakeups <= 3);
141 }
142
143 int main(int argc, char** argv) {
144 grpc::testing::TestEnvironment env(&argc, argv);
145 ::testing::InitGoogleTest(&argc, argv);
146 return RUN_ALL_TESTS();
147 }