mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-04-22 00:13:01 -05:00
Fixed problem where multiple ticks within the same second causes task to expire multiple times that second.
This commit is contained in:
parent
392948cdee
commit
efeb1e4769
@ -68,6 +68,7 @@ namespace libcron
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Queue tasks{};
|
||||
ClockType clock{};
|
||||
bool first_tick = true;
|
||||
@ -112,6 +113,21 @@ namespace libcron
|
||||
{
|
||||
size_t res = 0;
|
||||
|
||||
if(!first_tick)
|
||||
{
|
||||
// Only allow time to flow if at least one second has passed since the last tick,
|
||||
// either forward or backward.
|
||||
auto diff = now - last_tick;
|
||||
|
||||
constexpr auto one_second = std::chrono::seconds{1};
|
||||
|
||||
if(diff < one_second && diff > -one_second)
|
||||
{
|
||||
now = last_tick;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (first_tick)
|
||||
{
|
||||
first_tick = false;
|
||||
|
@ -240,11 +240,11 @@ SCENARIO("Clock changes")
|
||||
|
||||
WHEN("Clock changes <3h forward")
|
||||
{
|
||||
THEN("Task expires accordingly")
|
||||
{
|
||||
REQUIRE(c.tick() == 1);
|
||||
clock.add(minutes{ 30 }); // 00:30
|
||||
REQUIRE(c.tick() == 0);
|
||||
THEN("Task expires accordingly")
|
||||
{
|
||||
REQUIRE(c.tick() == 1);
|
||||
clock.add(minutes{30}); // 00:30
|
||||
REQUIRE(c.tick() == 0);
|
||||
clock.add(minutes{30}); // 01:00
|
||||
REQUIRE(c.tick() == 1);
|
||||
REQUIRE(c.tick() == 0);
|
||||
@ -297,3 +297,41 @@ SCENARIO("Clock changes")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO("Multiple ticks per second")
|
||||
{
|
||||
Cron<TestClock> c{};
|
||||
auto& clock = c.get_clock();
|
||||
|
||||
auto now = sys_days{2018_y / 05 / 05};
|
||||
clock.set(now);
|
||||
|
||||
int run_count = 0;
|
||||
|
||||
// Every 10 seconds
|
||||
REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count]()
|
||||
{
|
||||
run_count++;
|
||||
})
|
||||
);
|
||||
|
||||
c.tick(now);
|
||||
|
||||
REQUIRE(run_count == 1);
|
||||
|
||||
WHEN("Many ticks during one seconds")
|
||||
{
|
||||
for(auto i = 0; i < 10; ++i)
|
||||
{
|
||||
clock.add(std::chrono::microseconds{1});
|
||||
c.tick();
|
||||
}
|
||||
|
||||
THEN("Run count has not increased")
|
||||
{
|
||||
REQUIRE(run_count == 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user