Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[test] Add unit tests for mbgl::util::Timer covering timer cancellati…
Browse files Browse the repository at this point in the history
…on at/after its expiration (#15621)
  • Loading branch information
mskurydin authored Sep 19, 2019
1 parent 097cf5f commit c7737fe
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion test/util/timer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/chrono.hpp>

#include <memory>

Expand Down Expand Up @@ -131,6 +130,73 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(DestroyShouldStop)) {
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}

TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StoppedDuringExpiration)) {
// The idea is to have original timer cancellation and expiration roughly at the same time.
// In this case some timer backens (e.g. asio::high_resolution_timer)
// may call the expiration callback with good status while the timer may not expect it.

RunLoop loop;

auto timer = std::make_unique<Timer>();
auto loopStopTimer = std::make_unique<Timer>();
auto expireTimeout = mbgl::Milliseconds(50);

auto timerCallback = [&] {
// we cannot expect much here as in some cases timer may be finished earlier
// than the loop stop timer (and thus the callback will be called)
};

auto loopStopTimerCallback = [&] {
timer->stop();
loop.stop();
};

auto first = mbgl::Clock::now();

loopStopTimer->start(expireTimeout, mbgl::Milliseconds(0), loopStopTimerCallback);
timer->start(expireTimeout, mbgl::Milliseconds(0), timerCallback);

loop.run();

auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);

EXPECT_GE(totalTime, expireTimeout * 0.8);
EXPECT_LE(totalTime, expireTimeout * 1.2);
}

TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StoppedAfterExpiration)) {
RunLoop loop;

auto timer = std::make_unique<Timer>();
auto loopStopTimer = std::make_unique<Timer>();
auto expireTimeout = mbgl::Milliseconds(50);

bool callbackFired = false;

auto timerCallback = [&] { callbackFired = true; };

auto first = mbgl::Clock::now();

timer->start(expireTimeout, mbgl::Milliseconds(0), timerCallback);

// poll until the timer expires
auto expireWaitInterval = expireTimeout * 2;
auto startWaitTime = mbgl::Clock::now();
auto waitDuration = mbgl::Duration::zero();
while (waitDuration < expireWaitInterval) {
waitDuration = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - startWaitTime);
}
timer->stop();

loop.runOnce();

auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);

EXPECT_TRUE(!callbackFired);
EXPECT_GE(totalTime, expireWaitInterval * 0.8);
EXPECT_LE(totalTime, expireWaitInterval * 1.2);
}

TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StartOverrides)) {
RunLoop loop;

Expand Down

0 comments on commit c7737fe

Please sign in to comment.