From 97a0a5a9c22751fe91cd89fda69e1965a95b1935 Mon Sep 17 00:00:00 2001 From: Per Malmberg Date: Tue, 13 Mar 2018 01:23:50 -0700 Subject: [PATCH] Handles jumps of >=3h --- libcron/Cron.h | 4 ++-- test/CronTest.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libcron/Cron.h b/libcron/Cron.h index 9cbb953..252a20d 100644 --- a/libcron/Cron.h +++ b/libcron/Cron.h @@ -114,7 +114,7 @@ namespace libcron { first_tick = false; } - else if (now - last_tick < hours{3}) + else if (now > last_tick && now - last_tick <= hours{3}) { // Reschedule all tasks. for (auto& t : tasks.get_tasks()) @@ -122,7 +122,7 @@ namespace libcron t.calculate_next(now); } } - else if(now < last_tick && now >= last_tick - hours{3}) + else if(now < last_tick && now - last_tick < hours{3}) { // Prevent tasks from running until the clock has reached current 'last_tick'. for (auto& t : tasks.get_tasks()) diff --git a/test/CronTest.cpp b/test/CronTest.cpp index 49b4d3a..cda69a3 100644 --- a/test/CronTest.cpp +++ b/test/CronTest.cpp @@ -248,7 +248,7 @@ SCENARIO("Clock changes") REQUIRE(c.tick() == 1); REQUIRE(c.tick() == 0); REQUIRE(c.tick() == 0); - clock.add(minutes{30}); // 01:30 + clock.add(minutes{30}); // 01:30 REQUIRE(c.tick() == 0); clock.add(minutes{15}); // 01:45 REQUIRE(c.tick() == 0); @@ -261,16 +261,16 @@ SCENARIO("Clock changes") THEN("Task are rescheduled, not run") { REQUIRE(c.tick() == 1); - std::cout << c << std::endl; + std::cout << "0: " << c << std::endl; clock.add(hours{3}); // 03:00 REQUIRE(c.tick() == 1); // Rescheduled - std::cout << c << std::endl; + std::cout << "1: " << c << std::endl; clock.add(minutes{15}); // 03:15 - REQUIRE(c.tick() == 1); - std::cout << c << std::endl; + REQUIRE(c.tick() == 0); + std::cout << "2: " << c << std::endl; clock.add(minutes{45}); // 04:00 REQUIRE(c.tick() == 1); - std::cout << c << std::endl; + std::cout << "3: " << c << std::endl; } }