[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@f68039bc39
This commit is contained in:
parent
9367cd23f2
commit
86262d5ca5
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -108,7 +108,16 @@ public abstract class ScheduleTestCase extends ElasticsearchTestCase {
|
|||
int count = randomIntBetween(2, 5);
|
||||
Set<MonthTimes> 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()]);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue