[K/N] Fix timer tests

This commit is contained in:
Alexander Shabalin
2021-09-28 17:12:23 +03:00
committed by Space
parent 00134fddf9
commit 43f64bc7b4
@@ -12,30 +12,44 @@
using namespace kotlin; using namespace kotlin;
TEST(RepeatedTimerTest, WillNotExecuteImmediately) {
std::atomic<int> counter = 0;
RepeatedTimer timer(std::chrono::minutes(10), [&counter]() {
++counter;
return std::chrono::minutes(10);
});
// The function is not executed immediately.
EXPECT_THAT(counter.load(), 0);
}
TEST(RepeatedTimerTest, WillRun) { TEST(RepeatedTimerTest, WillRun) {
std::atomic<int> counter = 0; std::atomic<int> counter = 0;
RepeatedTimer timer(std::chrono::milliseconds(10), [&counter]() { RepeatedTimer timer(std::chrono::milliseconds(10), [&counter]() {
++counter; ++counter;
return std::chrono::milliseconds(10); return std::chrono::milliseconds(10);
}); });
// The function is not executed immediately. // Wait until the counter increases at least twice.
EXPECT_THAT(counter.load(), 0);
// And now wait until the counter increases at least twice
while (counter < 2) { while (counter < 2) {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
} }
TEST(RepeatedTimerTest, WillStop) { TEST(RepeatedTimerTest, WillStopInDestructor) {
std::atomic<int> counter = 0; std::atomic<int> counter = 0;
{ {
RepeatedTimer timer(std::chrono::minutes(1), [&counter]() { RepeatedTimer timer(std::chrono::milliseconds(1), [&counter]() {
// This lambda will only get executed once.
EXPECT_THAT(counter.load(), 0);
++counter; ++counter;
return std::chrono::minutes(10); return std::chrono::minutes(10);
}); });
// Wait until the counter increases once.
while (counter < 1) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} }
// The function was never executed. // The destructor was fired and cancelled the timer without executing the function.
EXPECT_THAT(counter.load(), 0); EXPECT_THAT(counter.load(), 1);
} }
TEST(RepeatedTimerTest, AdjustInterval) { TEST(RepeatedTimerTest, AdjustInterval) {