From 86262d5ca5f4eede2d53b253666b614e9b36f3fe Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Sat, 9 May 2015 10:56:35 -0400 Subject: [PATCH] [TEST] Fix the MonthlyScheduleTests Move deduping to `validMonthTimes` This change dedups the `MonthTimes` in `validMonthTimes` to support this two new methods on `MonthTimes` are added `contains(int,DayTime)` which detects if a given day and DayTime combination is contained in the MonthTime and `intersects(MonthTimes)` which detects if a `MonthTimes` has any day/DayTime combinations contained in this `MonthTimes`. Original commit: elastic/x-pack-elasticsearch@f68039bc3909155e10139232bc46cb14b602c40b --- .../trigger/schedule/support/MonthTimes.java | 23 +++++++++++++++++++ .../schedule/MonthlyScheduleTests.java | 4 ++-- .../trigger/schedule/ScheduleTestCase.java | 11 ++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java index c950171b3ec..f24af450813 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java @@ -101,6 +101,29 @@ public class MonthTimes implements Times { return "days [" + Ints.join(",", days) + "], times [" + Joiner.on(",").join(times) + "]"; } + public boolean contains(int day, DayTimes dayTimes) { + if (Arrays.binarySearch(days, day) == -1) { //days are already sorted + return false; + } + for (DayTimes dayTimes1 : this.times()) { + if (dayTimes.equals(dayTimes1)) { + return true; + } + } + return false; + } + + public boolean intersects(MonthTimes testTimes) { + for (int day : testTimes.days()) { + for (DayTimes dayTimes : testTimes.times()) { + if (contains(day, dayTimes)) { + return true; + } + } + } + return false; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java index 4ed2aba45f8..78acfef4616 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java @@ -53,8 +53,8 @@ public class MonthlyScheduleTests extends ScheduleTestCase { MonthlySchedule schedule = new MonthlySchedule(times); String[] crons = expressions(schedule); int count = 0; - for (int i = 0; i < times.length; i++) { - count += times[i].times().length; + for (MonthTimes time : times) { + count += time.times().length; } assertThat(crons, arrayWithSize(count)); for (MonthTimes monthTimes : times) { diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java index 5c26b37b496..646b3d227fc 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java @@ -108,7 +108,16 @@ public abstract class ScheduleTestCase extends ElasticsearchTestCase { int count = randomIntBetween(2, 5); Set times = new HashSet<>(); for (int i = 0; i < count; i++) { - times.add(validMonthTime()); + MonthTimes testMonthTimes = validMonthTime(); + boolean intersectsExistingMonthTimes = false; + for (MonthTimes validMonthTimes : times) { + if (validMonthTimes.intersects(testMonthTimes)) { + intersectsExistingMonthTimes = true; + } + } + if (!intersectsExistingMonthTimes) { + times.add(testMonthTimes); + } } return times.toArray(new MonthTimes[times.size()]); }