Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ void execute(Terminal terminal, String expression, int count) throws Exception {

Cron cron = new Cron(expression);
long time = date.getMillis();

for (int i = 0; i < count; i++) {
long prevTime = time;
time = cron.getNextValidTimeAfter(time);
if (time < 0) {
throw new UserException(ExitCodes.OK, (i + 1) + ".\t Could not compute future times since ["
+ formatter.print(prevTime) + "] " + "(perhaps the cron expression only points to times in the past?)");
if (i == 0) {
throw new UserException(ExitCodes.OK, "Could not compute future times since ["
+ formatter.print(prevTime) + "] " + "(perhaps the cron expression only points to times in the past?)");
}
break;
}
terminal.println((i+1) + ".\t" + formatter.print(time));
terminal.println((i + 1) + ".\t" + formatter.print(time));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

public class CronEvalToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
Expand All @@ -18,6 +25,27 @@ public void testParse() throws Exception {
String countOption = randomBoolean() ? "-c" : "--count";
int count = randomIntBetween(1, 100);
String output = execute(countOption, Integer.toString(count), "0 0 0 1-6 * ?");
assertTrue(output, output.contains("Here are the next " + count + " times this cron expression will trigger"));
assertThat(output, containsString("Here are the next " + count + " times this cron expression will trigger"));
}

public void testGetNextValidTimes() throws Exception {
final int year = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT).get(Calendar.YEAR) + 1;
{
String output = execute("0 3 23 8 9 ? " + year);
assertThat(output, containsString("Here are the next 10 times this cron expression will trigger:"));
assertThat(output, not(containsString("ERROR")));
assertThat(output, not(containsString("2.\t")));
}
{
String output = execute("0 3 23 */4 9 ? " + year);
assertThat(output, containsString("Here are the next 10 times this cron expression will trigger:"));
assertThat(output, not(containsString("ERROR")));
}
{
Exception expectThrows = expectThrows(Exception.class, () -> execute("0 3 23 */4 9 ? 2017"));
String message = expectThrows.getMessage();
assertThat(message, containsString("Could not compute future times since"));
assertThat(message, containsString("(perhaps the cron expression only points to times in the past?)"));
}
}
}