Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #858. #859

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.BitSet;
import java.util.Calendar;

import static java.time.Duration.between;

/**
* Schedule class represents a parsed crontab expression.
*
Expand Down Expand Up @@ -138,7 +140,7 @@ public static CronExpression create(String expression) {
boolean daysOfWeekStartAsterisk = token.startsWith("*");

if (token.length() == 2 && token.endsWith("l")) {
if(cronExpression.isLastDayOfMonth) {
if (cronExpression.isLastDayOfMonth) {
throw new InvalidCronExpressionException("You can only specify the last day of month week in either the DAY field or in the DAY_OF_WEEK field, not both.");
}
if (!daysToken.equalsIgnoreCase("*")) {
Expand Down Expand Up @@ -378,9 +380,10 @@ private BitSet getUpdatedDays(int year, int month) {

public void validateSchedule() {
Instant base = Instant.EPOCH;
Instant fiveSeconds = base.plusSeconds(SMALLEST_SCHEDULE_IN_SECONDS);

if (next(base, base, ZoneOffset.UTC).isBefore(fiveSeconds)) {
Instant i1 = next(base, base, ZoneOffset.UTC);
Instant i2 = next(base, i1, ZoneOffset.UTC);
if (between(i1, i2).getSeconds() < SMALLEST_SCHEDULE_IN_SECONDS) {
throw new IllegalArgumentException(String.format("The smallest interval for recurring jobs is %d seconds. Please also make sure that your 'pollIntervalInSeconds' configuration matches the smallest recurring job interval.", SMALLEST_SCHEDULE_IN_SECONDS));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import static java.time.LocalDateTime.now;
import static java.time.ZoneId.systemDefault;
import static java.time.ZoneOffset.UTC;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class CronExpressionTest {
Expand Down Expand Up @@ -166,6 +165,23 @@ void invalidCronExpressionThrowsExceptionIfBothLastDayOfMonth() {
.hasMessage("You can only specify the last day of month week in either the DAY field or in the DAY_OF_WEEK field, not both.");
}

@Test
void validateScheduleWorks() {
assertThatCode(() -> CronExpression.create("1,16,32 * * * * *").validateSchedule())
.doesNotThrowAnyException();

assertThatCode(() -> CronExpression.create("1 0 0 1 1 *").validateSchedule())
.doesNotThrowAnyException();

assertThatThrownBy(() -> CronExpression.create("1,4 * * * * *").validateSchedule())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The smallest interval for recurring jobs is 5 seconds. Please also make sure that your 'pollIntervalInSeconds' configuration matches the smallest recurring job interval.");

assertThatThrownBy(() -> CronExpression.create("5-59 * * * * *").validateSchedule())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The smallest interval for recurring jobs is 5 seconds. Please also make sure that your 'pollIntervalInSeconds' configuration matches the smallest recurring job interval.");
}

static Stream<Arguments> startInstantCronExpressionAndResultInstant() {
return Stream.of(
arguments("* * * * * *", "2019-01-01 00:00:00", "2019-01-01 00:00:01"),
Expand Down Expand Up @@ -716,7 +732,7 @@ static Stream<Arguments> startInstantCronExpressionAndResultInstant() {
arguments("0 0 0 29 2 5", "2019-01-01 00:00:00", "2019-02-01 00:00:00"),

// github issue 31
arguments("36 9 * * *","2020-09-08 09:40:00", "2020-09-09 09:36:00"),
arguments("36 9 * * *", "2020-09-08 09:40:00", "2020-09-09 09:36:00"),

// last day of month
arguments("0 0 0 * * 4l", "2019-01-01 00:00:00", "2019-01-31 00:00:00"),
Expand Down