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