1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <spawn.h>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
#include "absl/time/time.h"
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/iomgr/timer_manager.h"
#include "test/core/util/test_config.h"
extern char** environ;
#ifdef GPR_ANDROID
// Android doesn't have posix_spawn. Use std::system instead
void run_cmd(const char* cmd) { std::system(cmd); }
#else
void run_cmd(const char* cmd) {
pid_t pid;
const char* argv[] = {const_cast<const char*>("sh"),
const_cast<const char*>("-c"), cmd, nullptr};
int status;
status = posix_spawn(&pid, const_cast<const char*>("/bin/sh"), nullptr,
nullptr, const_cast<char**>(argv), environ);
if (status == 0) {
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid");
}
}
}
#endif
class TimeJumpTest : public ::testing::TestWithParam<std::string> {
protected:
void SetUp() override {
// Skip test if slowdown factor > 1
if (grpc_test_slowdown_factor() != 1) {
GTEST_SKIP();
} else {
grpc_init();
}
}
void TearDown() override {
// Skip test if slowdown factor > 1
if (grpc_test_slowdown_factor() == 1) {
run_cmd("sudo sntp -sS pool.ntp.org");
grpc_shutdown();
}
}
const int kWaitTimeMs = 1500;
};
std::vector<std::string> CreateTestScenarios() {
return {"-1M", "+1M", "-1H", "+1H", "-1d", "+1d", "-1y", "+1y"};
}
INSTANTIATE_TEST_SUITE_P(TimeJump, TimeJumpTest,
::testing::ValuesIn(CreateTestScenarios()));
TEST_P(TimeJumpTest, TimerRunning) {
grpc_core::ExecCtx exec_ctx;
grpc_timer timer;
grpc_timer_init(&timer,
grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(3),
GRPC_CLOSURE_CREATE(
[](void*, grpc_error_handle error) {
GPR_ASSERT(error == absl::CancelledError());
},
nullptr, grpc_schedule_on_exec_ctx));
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
std::ostringstream cmd;
cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
run_cmd(cmd.str().c_str());
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(kWaitTimeMs));
// We expect 1 wakeup/sec when there are not timer expiries
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
GPR_ASSERT(wakeups <= 3);
grpc_timer_cancel(&timer);
}
TEST_P(TimeJumpTest, TimedWait) {
grpc_core::CondVar cond;
grpc_core::Mutex mu;
{
grpc_core::MutexLock lock(&mu);
std::thread thd = std::thread([]() {
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
std::ostringstream cmd;
cmd << "sudo date `date -v" << GetParam() << " \"+%m%d%H%M%y\"`";
run_cmd(cmd.str().c_str());
});
gpr_timespec before = gpr_now(GPR_CLOCK_MONOTONIC);
bool timedout = cond.WaitWithTimeout(&mu, absl::Milliseconds(kWaitTimeMs));
gpr_timespec after = gpr_now(GPR_CLOCK_MONOTONIC);
int32_t elapsed_ms = gpr_time_to_millis(gpr_time_sub(after, before));
gpr_log(GPR_DEBUG, "After wait, timedout = %d elapsed_ms = %d", timedout,
elapsed_ms);
GPR_ASSERT(1 == timedout);
GPR_ASSERT(1 ==
gpr_time_similar(gpr_time_sub(after, before),
gpr_time_from_millis(kWaitTimeMs, GPR_TIMESPAN),
gpr_time_from_millis(50, GPR_TIMESPAN)));
thd.join();
}
// We expect 1 wakeup/sec when there are not timer expiries
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
GPR_ASSERT(wakeups <= 3);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|