Skip to content

Commit 2dca213

Browse files
committed
Test that timers theoretically will fire on time (HR/non-HR)
Signed-off-by: Otto van der Schaaf <[email protected]>
1 parent 9f00ad8 commit 2dca213

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

test/common/event/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ envoy_cc_test(
1818
"//source/common/stats:isolated_store_lib",
1919
"//test/mocks:common_lib",
2020
"//test/mocks/stats:stats_mocks",
21+
"//test/test_common:simulated_time_system_lib",
2122
"//test/test_common:utility_lib",
2223
],
2324
)

test/common/event/dispatcher_impl_test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "test/mocks/common.h"
1212
#include "test/mocks/stats/mocks.h"
13+
#include "test/test_common/simulated_time_system.h"
1314
#include "test/test_common/utility.h"
1415

1516
#include "gmock/gmock.h"
@@ -315,6 +316,52 @@ TEST(TimerImplTest, TimerEnabledDisabled) {
315316
EXPECT_FALSE(timer->enabled());
316317
}
317318

319+
class TimerImplTimingTest : public testing::Test {
320+
public:
321+
std::chrono::nanoseconds getTimerTiming(Event::SimulatedTimeSystem& time_system,
322+
Dispatcher& dispatcher, Event::Timer& timer) {
323+
const auto start = time_system.monotonicTime();
324+
EXPECT_TRUE(timer.enabled());
325+
while (true) {
326+
dispatcher.run(Dispatcher::RunType::NonBlock);
327+
if (timer.enabled()) {
328+
time_system.sleep(std::chrono::microseconds(1));
329+
} else {
330+
break;
331+
}
332+
}
333+
return time_system.monotonicTime() - start;
334+
}
335+
};
336+
337+
// Test the timer with a series of timings and measure they fire accurately
338+
// using simulated time. enableTimer() should be precise at the millisecond
339+
// level, whereas enableHRTimer should be precise at the microsecond level.
340+
// For good measure, also check that '0'/immediate does what it says on the tin.
341+
TEST_F(TimerImplTimingTest, TheoreticalTimerTiming) {
342+
Event::SimulatedTimeSystem time_system;
343+
Api::ApiPtr api = Api::createApiForTest(time_system);
344+
DispatcherPtr dispatcher(api->allocateDispatcher());
345+
Event::TimerPtr timer = dispatcher->createTimer([&dispatcher] { dispatcher->exit(); });
346+
347+
const uint64_t timings[] = {0, 10, 50, 1234};
348+
for (const uint64_t timing : timings) {
349+
std::chrono::milliseconds ms(timing);
350+
timer->enableTimer(ms);
351+
EXPECT_EQ(std::chrono::duration_cast<std::chrono::milliseconds>(
352+
getTimerTiming(time_system, *dispatcher, *timer))
353+
.count(),
354+
timing);
355+
356+
std::chrono::microseconds us(timing);
357+
timer->enableHRTimer(us);
358+
EXPECT_EQ(std::chrono::duration_cast<std::chrono::microseconds>(
359+
getTimerTiming(time_system, *dispatcher, *timer))
360+
.count(),
361+
timing);
362+
}
363+
}
364+
318365
TEST(TimerImplTest, TimerValueConversion) {
319366
timeval tv;
320367
std::chrono::milliseconds msecs;

0 commit comments

Comments
 (0)