-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Watcher: Properly find next valid date in cron expressions #32734
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
Merged
spinscale
merged 3 commits into
elastic:master
from
spinscale:1808-fix-cron-expression-next-valid-date
Aug 20, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,15 @@ | |
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
|
|
||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; | ||
| import static org.hamcrest.Matchers.arrayWithSize; | ||
| import static org.hamcrest.Matchers.hasItemInArray; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.hamcrest.Matchers.not; | ||
|
|
||
| public class CronScheduleTests extends ScheduleTestCase { | ||
| public void testInvalid() throws Exception { | ||
|
|
@@ -54,18 +58,25 @@ public void testParseMultiple() throws Exception { | |
| assertThat(crons, hasItemInArray("0 0/3 * * * ?")); | ||
| } | ||
|
|
||
| public void testMultipleCronsNextScheduledAfter() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add an expired cron entry to the list here too just to make sure?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| CronSchedule schedule = new CronSchedule("0 5 9 1 1 ? 2019", "0 5 9 1 1 ? 2020", "0 5 9 1 1 ? 2017"); | ||
| ZonedDateTime start2019 = ZonedDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); | ||
| ZonedDateTime start2020 = ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); | ||
| long firstSchedule = schedule.nextScheduledTimeAfter(0, start2019.toInstant().toEpochMilli()); | ||
| long secondSchedule = schedule.nextScheduledTimeAfter(0, start2020.toInstant().toEpochMilli()); | ||
|
|
||
| assertThat(firstSchedule, is(not(-1L))); | ||
| assertThat(secondSchedule, is(not(-1L))); | ||
| assertThat(firstSchedule, is(not(secondSchedule))); | ||
| } | ||
|
|
||
| public void testParseInvalidBadExpression() throws Exception { | ||
| XContentBuilder builder = jsonBuilder().value("0 0/5 * * ?"); | ||
| BytesReference bytes = BytesReference.bytes(builder); | ||
| XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); | ||
| parser.nextToken(); | ||
| try { | ||
| new CronSchedule.Parser().parse(parser); | ||
| fail("expected cron parsing to fail when using invalid cron expression"); | ||
| } catch (ElasticsearchParseException pe) { | ||
| // expected | ||
| assertThat(pe.getCause(), instanceOf(IllegalArgumentException.class)); | ||
| } | ||
| ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> new CronSchedule.Parser().parse(parser)); | ||
| assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); | ||
| } | ||
|
|
||
| public void testParseInvalidEmpty() throws Exception { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im curious, does the comparator not put these at either the begin or end of the sorted list here? should we just short circuit the loop once we find one of these outliers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cron comparator is a string based comparator (which I don't think makes a ton of sense in context of a cron expression, maybe we can just remove it), and thus the sorting could be wrong in regards to finding the next valid time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example:
0 6 9 1 1 ? 2020and1 5 9 1 1 ? 2020. The sorting puts the first expression in front, but the second one is actually triggered first