| 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 "src/core/lib/iomgr/timer.h" |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/support/log.h> |
| 25 |
|
| 26 |
#include "src/core/lib/gprpp/time.h" |
| 27 |
#include "src/core/lib/iomgr/closure.h" |
| 28 |
#include "src/core/lib/iomgr/error.h" |
| 29 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 30 |
#include "src/core/lib/iomgr/timer_manager.h" |
| 31 |
#include "test/core/util/test_config.h" |
| 32 |
|
| 33 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 34 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
// MAYBE_SKIP_TEST is a macro to determine if this particular test configuration |
| 38 |
// should be skipped based on a decision made at SetUp time. |
| 39 |
#define MAYBE_SKIP_TEST \ |
| 40 |
do { \ |
| 41 |
if (do_not_test_) { \ |
| 42 |
return; \ |
| 43 |
} \ |
| 44 |
} while (0) |
| 45 |
|
| 46 |
class TimerTest : public ::testing::Test { |
| 47 |
protected: |
| 48 |
void SetUp() override { |
| 49 |
grpc_init(); |
| 50 |
// Skip test if slowdown factor > 1, or we are |
| 51 |
// using event manager. |
| 52 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 53 |
if (grpc_test_slowdown_factor() != 1 || |
| 54 |
grpc_event_engine_run_in_background()) { |
| 55 |
#else |
| 56 |
if (grpc_test_slowdown_factor() != 1) { |
| 57 |
#endif |
| 58 |
do_not_test_ = true; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void TearDown() override { grpc_shutdown(); } |
| 63 |
|
| 64 |
bool do_not_test_{false}; |
| 65 |
}; |
| 66 |
|
| 67 |
#ifndef GPR_WINDOWS |
| 68 |
// the test fails with too many wakeups on windows opt build |
| 69 |
// the mechanism by which that happens is described in |
| 70 |
// https://github.com/grpc/grpc/issues/20436 |
| 71 |
TEST_F(TimerTest, NoTimers) { |
| 72 |
MAYBE_SKIP_TEST; |
| 73 |
grpc_core::ExecCtx exec_ctx; |
| 74 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500)); |
| 75 |
|
| 76 |
// We expect to get 1 wakeup per second. Sometimes we also get a wakeup |
| 77 |
// during initialization, so in 1.5 seconds we expect to get 1 or 2 wakeups. |
| 78 |
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly(); |
| 79 |
GPR_ASSERT(wakeups == 1 || wakeups == 2); |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
TEST_F(TimerTest, OneTimerExpires) { |
| 84 |
MAYBE_SKIP_TEST; |
| 85 |
grpc_core::ExecCtx exec_ctx; |
| 86 |
grpc_timer timer; |
| 87 |
int timer_fired = 0; |
| 88 |
grpc_timer_init( |
| 89 |
&timer, |
| 90 |
grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(500), |
| 91 |
GRPC_CLOSURE_CREATE( |
| 92 |
[](void* arg, grpc_error_handle) { |
| 93 |
int* timer_fired = static_cast<int*>(arg); |
| 94 |
++*timer_fired; |
| 95 |
}, |
| 96 |
&timer_fired, grpc_schedule_on_exec_ctx)); |
| 97 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500)); |
| 98 |
GPR_ASSERT(1 == timer_fired); |
| 99 |
|
| 100 |
// We expect to get 1 wakeup/second + 1 wakeup for the expired timer + maybe 1 |
| 101 |
// wakeup during initialization. i.e. in 1.5 seconds we expect 2 or 3 wakeups. |
| 102 |
// Actual number of wakeups is more due to bug |
| 103 |
// https://github.com/grpc/grpc/issues/19947 |
| 104 |
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly(); |
| 105 |
gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups); |
| 106 |
} |
| 107 |
|
| 108 |
TEST_F(TimerTest, MultipleTimersExpire) { |
| 109 |
MAYBE_SKIP_TEST; |
| 110 |
grpc_core::ExecCtx exec_ctx; |
| 111 |
const int kNumTimers = 10; |
| 112 |
grpc_timer timers[kNumTimers]; |
| 113 |
int timer_fired = 0; |
| 114 |
for (int i = 0; i < kNumTimers; ++i) { |
| 115 |
grpc_timer_init(&timers[i], |
| 116 |
grpc_core::Timestamp::Now() + |
| 117 |
grpc_core::Duration::Milliseconds(500) + |
| 118 |
grpc_core::Duration::Milliseconds(i), |
| 119 |
GRPC_CLOSURE_CREATE( |
| 120 |
[](void* arg, grpc_error_handle) { |
| 121 |
int* timer_fired = static_cast<int*>(arg); |
| 122 |
++*timer_fired; |
| 123 |
}, |
| 124 |
&timer_fired, grpc_schedule_on_exec_ctx)); |
| 125 |
} |
| 126 |
|
| 127 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500)); |
| 128 |
GPR_ASSERT(kNumTimers == timer_fired); |
| 129 |
|
| 130 |
// We expect to get 1 wakeup/second + 1 wakeup for per timer fired + maybe 1 |
| 131 |
// wakeup during initialization. i.e. in 1.5 seconds we expect 11 or 12 |
| 132 |
// wakeups. Actual number of wakeups is more due to bug |
| 133 |
// https://github.com/grpc/grpc/issues/19947 |
| 134 |
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly(); |
| 135 |
gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups); |
| 136 |
} |
| 137 |
|
| 138 |
TEST_F(TimerTest, CancelSomeTimers) { |
| 139 |
MAYBE_SKIP_TEST; |
| 140 |
grpc_core::ExecCtx exec_ctx; |
| 141 |
const int kNumTimers = 10; |
| 142 |
grpc_timer timers[kNumTimers]; |
| 143 |
std::atomic<int> timer_fired{0}; |
| 144 |
grpc_core::ExecCtx::Get()->InvalidateNow(); |
| 145 |
for (int i = 0; i < kNumTimers; ++i) { |
| 146 |
// Set a large firing time for timers which are bound to be cancelled |
| 147 |
// and set a small firing time for timers which need to execute. |
| 148 |
grpc_timer_init( |
| 149 |
&timers[i], |
| 150 |
grpc_core::Timestamp::Now() + |
| 151 |
((i < kNumTimers / 2) ? grpc_core ::Duration::Milliseconds(60000) |
| 152 |
: grpc_core ::Duration::Milliseconds(100) + |
| 153 |
grpc_core::Duration::Milliseconds(i)), |
| 154 |
GRPC_CLOSURE_CREATE( |
| 155 |
[](void* arg, grpc_error_handle error) { |
| 156 |
if (error == absl::CancelledError()) { |
| 157 |
return; |
| 158 |
} |
| 159 |
std::atomic<int>* timer_fired = |
| 160 |
static_cast<std::atomic<int>*>(arg); |
| 161 |
++*timer_fired; |
| 162 |
}, |
| 163 |
&timer_fired, grpc_schedule_on_exec_ctx)); |
| 164 |
} |
| 165 |
for (int i = 0; i < kNumTimers / 2; ++i) { |
| 166 |
grpc_timer_cancel(&timers[i]); |
| 167 |
} |
| 168 |
|
| 169 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500)); |
| 170 |
GPR_ASSERT(kNumTimers / 2 == timer_fired); |
| 171 |
|
| 172 |
// We expect to get 1 wakeup/second + 1 wakeup per timer fired + maybe 1 |
| 173 |
// wakeup during initialization. i.e. in 1.5 seconds we expect 6 or 7 wakeups. |
| 174 |
// Actual number of wakeups is more due to bug |
| 175 |
// https://github.com/grpc/grpc/issues/19947 |
| 176 |
int64_t wakeups = grpc_timer_manager_get_wakeups_testonly(); |
| 177 |
gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups); |
| 178 |
} |
| 179 |
|
| 180 |
// Enable the following test after |
| 181 |
// https://github.com/grpc/grpc/issues/20049 has been fixed. |
| 182 |
TEST_F(TimerTest, DISABLED_TimerNotCanceled) { |
| 183 |
grpc_core::ExecCtx exec_ctx; |
| 184 |
grpc_timer timer; |
| 185 |
grpc_timer_init( |
| 186 |
&timer, grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(10), |
| 187 |
GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr, |
| 188 |
grpc_schedule_on_exec_ctx)); |
| 189 |
} |
| 190 |
|
| 191 |
// Enable the following test after |
| 192 |
// https://github.com/grpc/grpc/issues/20064 has been fixed. |
| 193 |
TEST_F(TimerTest, DISABLED_CancelRace) { |
| 194 |
MAYBE_SKIP_TEST; |
| 195 |
grpc_core::ExecCtx exec_ctx; |
| 196 |
const int kNumTimers = 10; |
| 197 |
grpc_timer timers[kNumTimers]; |
| 198 |
for (int i = 0; i < kNumTimers; ++i) { |
| 199 |
grpc_timer* arg = (i != 0) ? &timers[i - 1] : nullptr; |
| 200 |
grpc_timer_init( |
| 201 |
&timers[i], |
| 202 |
grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(100), |
| 203 |
GRPC_CLOSURE_CREATE( |
| 204 |
[](void* arg, grpc_error_handle /*error*/) { |
| 205 |
grpc_timer* timer = static_cast<grpc_timer*>(arg); |
| 206 |
if (timer) { |
| 207 |
grpc_timer_cancel(timer); |
| 208 |
} |
| 209 |
}, |
| 210 |
arg, grpc_schedule_on_exec_ctx)); |
| 211 |
} |
| 212 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 213 |
} |
| 214 |
|
| 215 |
// Enable the following test after |
| 216 |
// https://github.com/grpc/grpc/issues/20066 has been fixed. |
| 217 |
TEST_F(TimerTest, DISABLED_CancelNextTimer) { |
| 218 |
MAYBE_SKIP_TEST; |
| 219 |
grpc_core::ExecCtx exec_ctx; |
| 220 |
const int kNumTimers = 10; |
| 221 |
grpc_timer timers[kNumTimers]; |
| 222 |
|
| 223 |
for (int i = 0; i < kNumTimers; ++i) { |
| 224 |
grpc_timer_init_unset(&timers[i]); |
| 225 |
} |
| 226 |
|
| 227 |
for (int i = 0; i < kNumTimers; ++i) { |
| 228 |
grpc_timer* arg = nullptr; |
| 229 |
if (i < kNumTimers - 1) { |
| 230 |
arg = &timers[i + 1]; |
| 231 |
} |
| 232 |
grpc_timer_init( |
| 233 |
&timers[i], |
| 234 |
grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(100), |
| 235 |
GRPC_CLOSURE_CREATE( |
| 236 |
[](void* arg, grpc_error_handle /*error*/) { |
| 237 |
grpc_timer* timer = static_cast<grpc_timer*>(arg); |
| 238 |
if (timer) { |
| 239 |
grpc_timer_cancel(timer); |
| 240 |
} |
| 241 |
}, |
| 242 |
arg, grpc_schedule_on_exec_ctx)); |
| 243 |
} |
| 244 |
grpc_timer_cancel(&timers[0]); |
| 245 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 246 |
} |
| 247 |
|
| 248 |
int main(int argc, char** argv) { |
| 249 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 250 |
::testing::InitGoogleTest(&argc, argv); |
| 251 |
return RUN_ALL_TESTS(); |
| 252 |
} |