|
11 | 11 | import org.elasticsearch.common.xcontent.XContentParser; |
12 | 12 | import org.elasticsearch.common.xcontent.json.JsonXContent; |
13 | 13 |
|
| 14 | +import java.time.ZoneOffset; |
| 15 | +import java.time.ZonedDateTime; |
| 16 | + |
14 | 17 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
15 | 18 | import static org.hamcrest.Matchers.arrayWithSize; |
16 | 19 | import static org.hamcrest.Matchers.hasItemInArray; |
17 | 20 | import static org.hamcrest.Matchers.instanceOf; |
18 | 21 | import static org.hamcrest.Matchers.is; |
| 22 | +import static org.hamcrest.Matchers.not; |
19 | 23 |
|
20 | 24 | public class CronScheduleTests extends ScheduleTestCase { |
21 | 25 | public void testInvalid() throws Exception { |
@@ -54,18 +58,25 @@ public void testParseMultiple() throws Exception { |
54 | 58 | assertThat(crons, hasItemInArray("0 0/3 * * * ?")); |
55 | 59 | } |
56 | 60 |
|
| 61 | + public void testMultipleCronsNextScheduledAfter() { |
| 62 | + CronSchedule schedule = new CronSchedule("0 5 9 1 1 ? 2019", "0 5 9 1 1 ? 2020", "0 5 9 1 1 ? 2017"); |
| 63 | + ZonedDateTime start2019 = ZonedDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); |
| 64 | + ZonedDateTime start2020 = ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); |
| 65 | + long firstSchedule = schedule.nextScheduledTimeAfter(0, start2019.toInstant().toEpochMilli()); |
| 66 | + long secondSchedule = schedule.nextScheduledTimeAfter(0, start2020.toInstant().toEpochMilli()); |
| 67 | + |
| 68 | + assertThat(firstSchedule, is(not(-1L))); |
| 69 | + assertThat(secondSchedule, is(not(-1L))); |
| 70 | + assertThat(firstSchedule, is(not(secondSchedule))); |
| 71 | + } |
| 72 | + |
57 | 73 | public void testParseInvalidBadExpression() throws Exception { |
58 | 74 | XContentBuilder builder = jsonBuilder().value("0 0/5 * * ?"); |
59 | 75 | BytesReference bytes = BytesReference.bytes(builder); |
60 | 76 | XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); |
61 | 77 | parser.nextToken(); |
62 | | - try { |
63 | | - new CronSchedule.Parser().parse(parser); |
64 | | - fail("expected cron parsing to fail when using invalid cron expression"); |
65 | | - } catch (ElasticsearchParseException pe) { |
66 | | - // expected |
67 | | - assertThat(pe.getCause(), instanceOf(IllegalArgumentException.class)); |
68 | | - } |
| 78 | + ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> new CronSchedule.Parser().parse(parser)); |
| 79 | + assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); |
69 | 80 | } |
70 | 81 |
|
71 | 82 | public void testParseInvalidEmpty() throws Exception { |
|
0 commit comments