2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
19 #include <folly/io/async/HHWheelTimer.h>
20 #include <folly/io/async/EventBase.h>
21 #include <folly/io/async/test/UndelayedDestruction.h>
22 #include <folly/io/async/test/Util.h>
24 #include <gtest/gtest.h>
27 using namespace folly;
28 using std::chrono::milliseconds;
30 typedef UndelayedDestruction<HHWheelTimer> StackWheelTimer;
32 class TestTimeout : public HHWheelTimer::Callback {
35 TestTimeout(HHWheelTimer* t, milliseconds timeout) {
36 t->scheduleTimeout(this, timeout);
39 void timeoutExpired() noexcept override {
40 timestamps.emplace_back();
46 void callbackCanceled() noexcept override {
47 canceledTimestamps.emplace_back();
53 std::deque<TimePoint> timestamps;
54 std::deque<TimePoint> canceledTimestamps;
55 std::function<void()> fn;
59 class TestTimeoutDelayed : public TestTimeout {
61 std::chrono::milliseconds getCurTime() override {
62 return std::chrono::duration_cast<std::chrono::milliseconds>(
63 std::chrono::steady_clock::now().time_since_epoch()) -
68 struct HHWheelTimerTest : public ::testing::Test {
73 * Test firing some simple timeouts that are fired once and never rescheduled
75 TEST_F(HHWheelTimerTest, FireOnce) {
76 StackWheelTimer t(&eventBase, milliseconds(1));
82 ASSERT_EQ(t.count(), 0);
84 t.scheduleTimeout(&t1, milliseconds(5));
85 t.scheduleTimeout(&t2, milliseconds(5));
86 // Verify scheduling it twice cancels, then schedules.
87 // Should only get one callback.
88 t.scheduleTimeout(&t2, milliseconds(5));
89 t.scheduleTimeout(&t3, milliseconds(10));
91 ASSERT_EQ(t.count(), 3);
97 ASSERT_EQ(t1.timestamps.size(), 1);
98 ASSERT_EQ(t2.timestamps.size(), 1);
99 ASSERT_EQ(t3.timestamps.size(), 1);
101 ASSERT_EQ(t.count(), 0);
103 T_CHECK_TIMEOUT(start, t1.timestamps[0], milliseconds(5));
104 T_CHECK_TIMEOUT(start, t2.timestamps[0], milliseconds(5));
105 T_CHECK_TIMEOUT(start, t3.timestamps[0], milliseconds(10));
106 T_CHECK_TIMEOUT(start, end, milliseconds(10));
110 * Test scheduling a timeout from another timeout callback.
112 TEST_F(HHWheelTimerTest, TestSchedulingWithinCallback) {
113 HHWheelTimer& t = eventBase.timer();
116 // Delayed to simulate the steady_clock counter lagging
117 TestTimeoutDelayed t2;
119 t.scheduleTimeout(&t1, milliseconds(500));
120 t1.fn = [&] { t.scheduleTimeout(&t2, milliseconds(1)); };
121 // If t is in an inconsistent state, detachEventBase should fail.
122 t2.fn = [&] { t.detachEventBase(); };
124 ASSERT_EQ(t.count(), 1);
128 ASSERT_EQ(t.count(), 0);
129 ASSERT_EQ(t1.timestamps.size(), 1);
130 ASSERT_EQ(t2.timestamps.size(), 1);
134 * Test cancelling a timeout when it is scheduled to be fired right away.
137 TEST_F(HHWheelTimerTest, CancelTimeout) {
138 HHWheelTimer& t = eventBase.timer();
140 // Create several timeouts that will all fire in 5ms.
141 TestTimeout t5_1(&t, milliseconds(5));
142 TestTimeout t5_2(&t, milliseconds(5));
143 TestTimeout t5_3(&t, milliseconds(5));
144 TestTimeout t5_4(&t, milliseconds(5));
145 TestTimeout t5_5(&t, milliseconds(5));
147 // Also create a few timeouts to fire in 10ms
148 TestTimeout t10_1(&t, milliseconds(10));
149 TestTimeout t10_2(&t, milliseconds(10));
150 TestTimeout t10_3(&t, milliseconds(10));
152 TestTimeout t20_1(&t, milliseconds(20));
153 TestTimeout t20_2(&t, milliseconds(20));
155 // Have t5_1 cancel t5_2 and t5_4.
157 // Cancelling t5_2 will test cancelling a timeout that is at the head of the
158 // list and ready to be fired.
160 // Cancelling t5_4 will test cancelling a timeout in the middle of the list
162 t5_2.cancelTimeout();
163 t5_4.cancelTimeout();
166 // Have t5_3 cancel t5_5.
167 // This will test cancelling the last remaining timeout.
169 // Then have t5_3 reschedule itself.
171 t5_5.cancelTimeout();
172 // Reset our function so we won't continually reschedule ourself
173 std::function<void()> fnDtorGuard;
174 t5_3.fn.swap(fnDtorGuard);
175 t.scheduleTimeout(&t5_3, milliseconds(5));
177 // Also test cancelling timeouts in another timeset that isn't ready to
180 // Cancel the middle timeout in ts10.
181 t10_2.cancelTimeout();
182 // Cancel both the timeouts in ts20.
183 t20_1.cancelTimeout();
184 t20_2.cancelTimeout();
191 ASSERT_EQ(t5_1.timestamps.size(), 1);
192 T_CHECK_TIMEOUT(start, t5_1.timestamps[0], milliseconds(5));
194 ASSERT_EQ(t5_3.timestamps.size(), 2);
195 T_CHECK_TIMEOUT(start, t5_3.timestamps[0], milliseconds(5));
196 T_CHECK_TIMEOUT(t5_3.timestamps[0], t5_3.timestamps[1], milliseconds(5));
198 ASSERT_EQ(t10_1.timestamps.size(), 1);
199 T_CHECK_TIMEOUT(start, t10_1.timestamps[0], milliseconds(10));
200 ASSERT_EQ(t10_3.timestamps.size(), 1);
201 T_CHECK_TIMEOUT(start, t10_3.timestamps[0], milliseconds(10));
203 // Cancelled timeouts
204 ASSERT_EQ(t5_2.timestamps.size(), 0);
205 ASSERT_EQ(t5_4.timestamps.size(), 0);
206 ASSERT_EQ(t5_5.timestamps.size(), 0);
207 ASSERT_EQ(t10_2.timestamps.size(), 0);
208 ASSERT_EQ(t20_1.timestamps.size(), 0);
209 ASSERT_EQ(t20_2.timestamps.size(), 0);
211 T_CHECK_TIMEOUT(start, end, milliseconds(10));
215 * Test destroying a HHWheelTimer with timeouts outstanding
218 TEST_F(HHWheelTimerTest, DestroyTimeoutSet) {
219 HHWheelTimer::UniquePtr t(
220 HHWheelTimer::newTimer(&eventBase, milliseconds(1)));
222 TestTimeout t5_1(t.get(), milliseconds(5));
223 TestTimeout t5_2(t.get(), milliseconds(5));
224 TestTimeout t5_3(t.get(), milliseconds(5));
226 TestTimeout t10_1(t.get(), milliseconds(10));
227 TestTimeout t10_2(t.get(), milliseconds(10));
229 // Have t5_2 destroy t
230 // Note that this will call destroy() inside t's timeoutExpired()
233 t5_3.cancelTimeout();
234 t5_1.cancelTimeout();
235 t10_1.cancelTimeout();
236 t10_2.cancelTimeout();
243 ASSERT_EQ(t5_1.timestamps.size(), 1);
244 T_CHECK_TIMEOUT(start, t5_1.timestamps[0], milliseconds(5));
245 ASSERT_EQ(t5_2.timestamps.size(), 1);
246 T_CHECK_TIMEOUT(start, t5_2.timestamps[0], milliseconds(5));
248 ASSERT_EQ(t5_3.timestamps.size(), 0);
249 ASSERT_EQ(t10_1.timestamps.size(), 0);
250 ASSERT_EQ(t10_2.timestamps.size(), 0);
252 T_CHECK_TIMEOUT(start, end, milliseconds(5));
256 * Test the tick interval parameter
258 TEST_F(HHWheelTimerTest, AtMostEveryN) {
260 // Create a timeout set with a 10ms interval, to fire no more than once
262 milliseconds interval(25);
263 milliseconds atMostEveryN(6);
264 StackWheelTimer t(&eventBase, atMostEveryN);
265 t.setCatchupEveryN(70);
267 // Create 60 timeouts to be added to ts1 at 1ms intervals.
268 uint32_t numTimeouts = 60;
269 std::vector<TestTimeout> timeouts(numTimeouts);
271 // Create a scheduler timeout to add the timeouts 1ms apart.
273 StackWheelTimer ts1(&eventBase, milliseconds(1));
274 TestTimeout scheduler(&ts1, milliseconds(1));
276 if (index >= numTimeouts) {
279 // Call timeoutExpired() on the timeout so it will record a timestamp.
280 // This is done only so we can record when we scheduled the timeout.
281 // This way if ts1 starts to fall behind a little over time we will still
282 // be comparing the ts1 timeouts to when they were first scheduled (rather
283 // than when we intended to schedule them). The scheduler may fall behind
284 // eventually since we don't really schedule it once every millisecond.
285 // Each time it finishes we schedule it for 1 millisecond in the future.
286 // The amount of time it takes to run, and any delays it encounters
287 // getting scheduled may eventually add up over time.
288 timeouts[index].timeoutExpired();
290 // Schedule the new timeout
291 t.scheduleTimeout(&timeouts[index], interval);
292 // Reschedule ourself
293 ts1.scheduleTimeout(&scheduler, milliseconds(1));
297 // Go ahead and schedule the first timeout now.
304 // This should take roughly 2*60 + 25 ms to finish. If it takes more than
305 // 250 ms to finish the system is probably heavily loaded, so skip.
306 if (std::chrono::duration_cast<std::chrono::milliseconds>(
307 end.getTime() - start.getTime()).count() > 250) {
308 LOG(WARNING) << "scheduling all timeouts takes too long";
312 // We scheduled timeouts 1ms apart, when the HHWheelTimer is only allowed
313 // to wake up at most once every 3ms. It will therefore wake up every 3ms
314 // and fire groups of approximately 3 timeouts at a time.
316 // This is "approximately 3" since it may get slightly behind and fire 4 in
317 // one interval, etc. T_CHECK_TIMEOUT normally allows a few milliseconds of
318 // tolerance. We have to add the same into our checking algorithm here.
319 for (uint32_t idx = 0; idx < numTimeouts; ++idx) {
320 ASSERT_EQ(timeouts[idx].timestamps.size(), 2);
322 TimePoint scheduledTime(timeouts[idx].timestamps[0]);
323 TimePoint firedTime(timeouts[idx].timestamps[1]);
325 // Assert that the timeout fired at roughly the right time.
326 // T_CHECK_TIMEOUT() normally has a tolerance of 5ms. Allow an additional
328 milliseconds tolerance = milliseconds(5) + interval;
329 T_CHECK_TIMEOUT(scheduledTime, firedTime, atMostEveryN, tolerance);
331 // Assert that the difference between the previous timeout and now was
332 // either very small (fired in the same event loop), or larger than
338 TimePoint prev(timeouts[idx - 1].timestamps[1]);
340 auto delta = (firedTime.getTimeStart() - prev.getTimeEnd()) -
341 (firedTime.getTimeWaiting() - prev.getTimeWaiting());
342 if (delta > milliseconds(1)) {
343 T_CHECK_TIMEOUT(prev, firedTime, atMostEveryN);
349 * Test an event loop that is blocking
352 TEST_F(HHWheelTimerTest, SlowLoop) {
353 HHWheelTimer& t = eventBase.timer();
358 ASSERT_EQ(t.count(), 0);
360 eventBase.runInLoop([](){usleep(10000);});
361 t.scheduleTimeout(&t1, milliseconds(5));
363 ASSERT_EQ(t.count(), 1);
369 ASSERT_EQ(t1.timestamps.size(), 1);
370 ASSERT_EQ(t.count(), 0);
372 // Check that the timeout was delayed by sleep
373 T_CHECK_TIMEOUT(start, t1.timestamps[0], milliseconds(15), milliseconds(1));
374 T_CHECK_TIMEOUT(start, end, milliseconds(15), milliseconds(1));
376 // Try it again, this time with catchup timing every loop
377 t.setCatchupEveryN(1);
379 eventBase.runInLoop([](){usleep(10000);});
380 t.scheduleTimeout(&t2, milliseconds(5));
382 ASSERT_EQ(t.count(), 1);
388 ASSERT_EQ(t2.timestamps.size(), 1);
389 ASSERT_EQ(t.count(), 0);
391 // Check that the timeout was NOT delayed by sleep
392 T_CHECK_TIMEOUT(start2, t2.timestamps[0], milliseconds(10), milliseconds(1));
393 T_CHECK_TIMEOUT(start2, end2, milliseconds(10), milliseconds(1));
397 * Test scheduling a mix of timers with default timeout and variable timeout.
399 TEST_F(HHWheelTimerTest, DefaultTimeout) {
400 milliseconds defaultTimeout(milliseconds(5));
401 StackWheelTimer t(&eventBase,
403 AsyncTimeout::InternalEnum::NORMAL,
409 ASSERT_EQ(t.count(), 0);
410 ASSERT_EQ(t.getDefaultTimeout(), defaultTimeout);
412 t.scheduleTimeout(&t1);
413 t.scheduleTimeout(&t2, milliseconds(10));
415 ASSERT_EQ(t.count(), 2);
421 ASSERT_EQ(t1.timestamps.size(), 1);
422 ASSERT_EQ(t2.timestamps.size(), 1);
424 ASSERT_EQ(t.count(), 0);
426 T_CHECK_TIMEOUT(start, t1.timestamps[0], defaultTimeout);
427 T_CHECK_TIMEOUT(start, t2.timestamps[0], milliseconds(10));
428 T_CHECK_TIMEOUT(start, end, milliseconds(10));
431 TEST_F(HHWheelTimerTest, lambda) {
432 HHWheelTimer& t = eventBase.timer();
434 t.scheduleTimeoutFn([&]{ count++; }, milliseconds(1));
439 // shouldn't crash because we swallow and log the error (you'll have to look
440 // at the console to confirm logging)
441 TEST_F(HHWheelTimerTest, lambdaThrows) {
442 HHWheelTimer& t = eventBase.timer();
443 t.scheduleTimeoutFn([&]{ throw std::runtime_error("expected"); },
448 TEST_F(HHWheelTimerTest, cancelAll) {
449 HHWheelTimer& t = eventBase.timer();
451 t.scheduleTimeout(&tt, std::chrono::minutes(1));
452 EXPECT_EQ(1, t.cancelAll());
453 EXPECT_EQ(1, tt.canceledTimestamps.size());
456 TEST_F(HHWheelTimerTest, IntrusivePtr) {
457 HHWheelTimer::UniquePtr t(
458 HHWheelTimer::newTimer(&eventBase, milliseconds(1)));
464 ASSERT_EQ(t->count(), 0);
466 t->scheduleTimeout(&t1, milliseconds(5));
467 t->scheduleTimeout(&t2, milliseconds(5));
469 DelayedDestruction::IntrusivePtr<HHWheelTimer> s(t);
471 s->scheduleTimeout(&t3, milliseconds(10));
473 ASSERT_EQ(t->count(), 3);
475 // Kill the UniquePtr, but the SharedPtr keeps it alive
482 ASSERT_EQ(t1.timestamps.size(), 1);
483 ASSERT_EQ(t2.timestamps.size(), 1);
484 ASSERT_EQ(t3.timestamps.size(), 1);
486 ASSERT_EQ(s->count(), 0);
488 T_CHECK_TIMEOUT(start, t1.timestamps[0], milliseconds(5));
489 T_CHECK_TIMEOUT(start, t2.timestamps[0], milliseconds(5));
490 T_CHECK_TIMEOUT(start, t3.timestamps[0], milliseconds(10));
491 T_CHECK_TIMEOUT(start, end, milliseconds(10));