Skip to content

Commit

Permalink
Add missing support for month day/month/week day
Browse files Browse the repository at this point in the history
  • Loading branch information
kinoute committed Jun 9, 2024
1 parent 36d01ab commit a2d5428
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/common/cron.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ bool Cron::IsTimeMatch(const tm *tm) {
return false;
}
for (const auto &st : schedulers_) {
bool minuteMatch = (st.minute == -1 || tm->tm_min == st.minute || (st.minute > 0 && tm->tm_min % st.minute == 0));
bool hourMatch = (st.hour == -1 || tm->tm_hour == st.hour || (st.hour > 0 && tm->tm_hour % st.hour == 0));
bool mdayMatch = (st.mday == -1 || tm->tm_mday == st.mday);
bool monthMatch = (st.month == -1 || (tm->tm_mon + 1) == st.month);
bool wdayMatch = (st.wday == -1 || tm->tm_wday == st.wday);
bool minuteMatch = (st.minute == -1 || tm->tm_min == st.minute ||
(st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0) ||
(st.minute == 0 && tm->tm_min == 0));
bool hourMatch = (st.hour == -1 || tm->tm_hour == st.hour ||
(st.hour > 0 && st.hour <= 23 && tm->tm_hour % st.hour == 0) ||
(st.hour == 0 && tm->tm_hour == 0));
bool mdayMatch = (st.mday == -1 || tm->tm_mday == st.mday ||
(st.mday > 0 && st.mday <= 31 && tm->tm_mday % st.mday == 0) ||
(st.mday == 0 && tm->tm_mday == 1));
bool monthMatch = (st.month == -1 || tm->tm_mon == st.month ||
(st.month > 0 && st.month <= 12 && (tm->tm_mon - 1) % st.month == 0));
bool wdayMatch = (st.wday == -1 || tm->tm_wday == st.wday ||
(st.wday >= 0 && st.wday <= 6 && tm->tm_wday % st.wday == 0));

if (minuteMatch && hourMatch && mdayMatch && monthMatch && wdayMatch) {
last_tm_ = *tm;
Expand Down
163 changes: 158 additions & 5 deletions tests/cppunit/cron_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,56 @@ TEST_F(CronTest, ToString) {
ASSERT_EQ("* 3 * * *", got);
}

class CronTestInterval : public testing::Test {
class CronTestMinInterval : public testing::Test {
protected:
explicit CronTestInterval() {
explicit CronTestMinInterval() {
cron_ = std::make_unique<Cron>();
std::vector<std::string> schedule{"*/4", "*", "*", "*", "*"};
auto s = cron_->SetScheduleTime(schedule);
EXPECT_TRUE(s.IsOK());
}
~CronTestMinInterval() override = default;

std::unique_ptr<Cron> cron_;
};

TEST_F(CronTestMinInterval, IsTimeMatch) {
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
now->tm_hour = 0;
now->tm_min = 0;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_min = 4;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_min = 8;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_min = 12;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_min = 3;
ASSERT_FALSE(cron_->IsTimeMatch(now));
now->tm_min = 99;
ASSERT_FALSE(cron_->IsTimeMatch(now));
}

TEST_F(CronTestMinInterval, ToString) {
std::string got = cron_->ToString();
ASSERT_EQ("*/4 * * * *", got);
}

class CronTestHourInterval : public testing::Test {
protected:
explicit CronTestHourInterval() {
cron_ = std::make_unique<Cron>();
std::vector<std::string> schedule{"0", "*/4", "*", "*", "*"};
auto s = cron_->SetScheduleTime(schedule);
EXPECT_TRUE(s.IsOK());
}
~CronTestInterval() override = default;
~CronTestHourInterval() override = default;

std::unique_ptr<Cron> cron_;
};

TEST_F(CronTestInterval, IsTimeMatch) {
TEST_F(CronTestHourInterval, IsTimeMatch) {
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
now->tm_hour = 0;
Expand All @@ -78,9 +114,126 @@ TEST_F(CronTestInterval, IsTimeMatch) {
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 3;
ASSERT_FALSE(cron_->IsTimeMatch(now));
now->tm_hour = 55;
ASSERT_FALSE(cron_->IsTimeMatch(now));
}

TEST_F(CronTestInterval, ToString) {
TEST_F(CronTestHourInterval, ToString) {
std::string got = cron_->ToString();
ASSERT_EQ("0 */4 * * *", got);
}

class CronTestMonthDayInterval : public testing::Test {
protected:
explicit CronTestMonthDayInterval() {
cron_ = std::make_unique<Cron>();
std::vector<std::string> schedule{"0", "*", "*/4", "*", "*"};
auto s = cron_->SetScheduleTime(schedule);
EXPECT_TRUE(s.IsOK());
}
~CronTestMonthDayInterval() override = default;

std::unique_ptr<Cron> cron_;
};

TEST_F(CronTestMonthDayInterval, IsTimeMatch) {
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
now->tm_hour = 0;
now->tm_min = 0;
now->tm_mday = 0;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 3;
now->tm_mday = 4;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 5;
now->tm_mday = 8;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 6;
now->tm_mday = 12;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 1;
now->tm_mday = 3;
ASSERT_FALSE(cron_->IsTimeMatch(now));
now->tm_hour = 1;
now->tm_mday = 99;
ASSERT_FALSE(cron_->IsTimeMatch(now));
}

TEST_F(CronTestMonthDayInterval, ToString) {
std::string got = cron_->ToString();
ASSERT_EQ("0 * */4 * *", got);
}

class CronTestMonthInterval : public testing::Test {
protected:
explicit CronTestMonthInterval() {
cron_ = std::make_unique<Cron>();
std::vector<std::string> schedule{"0", "*", "*", "*/4", "*"};
auto s = cron_->SetScheduleTime(schedule);
EXPECT_TRUE(s.IsOK());
}
~CronTestMonthInterval() override = default;

std::unique_ptr<Cron> cron_;
};

TEST_F(CronTestMonthInterval, IsTimeMatch) {
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
now->tm_hour = 0;
now->tm_min = 0;
now->tm_mon = 5;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 5;
now->tm_mon = 9;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 1;
now->tm_mon = 3;
ASSERT_FALSE(cron_->IsTimeMatch(now));
now->tm_hour = 1;
now->tm_mon = 99;
ASSERT_FALSE(cron_->IsTimeMatch(now));
}

TEST_F(CronTestMonthInterval, ToString) {
std::string got = cron_->ToString();
ASSERT_EQ("0 * * */4 *", got);
}

class CronTestWeekDayInterval : public testing::Test {
protected:
explicit CronTestWeekDayInterval() {
cron_ = std::make_unique<Cron>();
std::vector<std::string> schedule{"0", "*", "*", "*", "*/4"};
auto s = cron_->SetScheduleTime(schedule);
EXPECT_TRUE(s.IsOK());
}
~CronTestWeekDayInterval() override = default;

std::unique_ptr<Cron> cron_;
};

TEST_F(CronTestWeekDayInterval, IsTimeMatch) {
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
now->tm_hour = 0;
now->tm_min = 0;
now->tm_hour = 3;
now->tm_wday = 4;
ASSERT_TRUE(cron_->IsTimeMatch(now));
now->tm_hour = 5;
now->tm_wday = 3;
ASSERT_FALSE(cron_->IsTimeMatch(now));
now->tm_hour = 1;
now->tm_wday = 99;
ASSERT_FALSE(cron_->IsTimeMatch(now));
}

TEST_F(CronTestWeekDayInterval, ToString) {
std::string got = cron_->ToString();
ASSERT_EQ("0 * * * */4", got);
}



0 comments on commit a2d5428

Please sign in to comment.