[ILM] TEST: fix long overflow in TimeValueScheduleTests (#36384)

Closes #35948.
This commit is contained in:
Tal Levy 2018-12-10 09:28:17 -08:00 committed by GitHub
parent 58a5ad1f1e
commit ed7afd1a9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 9 deletions

View File

@ -9,11 +9,15 @@ package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.junit.Before;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class TimeValueScheduleTests extends ESTestCase { public class TimeValueScheduleTests extends ESTestCase {
private long start;
private TimeValue interval;
public TimeValueSchedule createRandomInstance() { public TimeValueSchedule createRandomInstance() {
return new TimeValueSchedule(createRandomTimeValue()); return new TimeValueSchedule(createRandomTimeValue());
} }
@ -22,7 +26,15 @@ public class TimeValueScheduleTests extends ESTestCase {
return new TimeValue(randomLongBetween(1, 10000), randomFrom(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS)); return new TimeValue(randomLongBetween(1, 10000), randomFrom(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS));
} }
public void testHascodeAndEquals() { @Before
public void setUpStartAndInterval() {
// start with random epoch between 1/1/1970 and 31/12/2035 so that start is not
// so large such that (start + interval) > Long.MAX
start = randomLongBetween(0, 2082672000000L);
interval = createRandomTimeValue();
}
public void testHashcodeAndEquals() {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
instance -> new TimeValueSchedule(instance.getInterval()), instance -> new TimeValueSchedule(instance.getInterval()),
@ -31,8 +43,6 @@ public class TimeValueScheduleTests extends ESTestCase {
} }
public void testNextScheduledTimeFirstTriggerNotReached() { public void testNextScheduledTimeFirstTriggerNotReached() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + interval.millis(); long triggerTime = start + interval.millis();
long now = start + randomLongBetween(0, interval.millis() - 1); long now = start + randomLongBetween(0, interval.millis() - 1);
TimeValueSchedule schedule = new TimeValueSchedule(interval); TimeValueSchedule schedule = new TimeValueSchedule(interval);
@ -40,8 +50,6 @@ public class TimeValueScheduleTests extends ESTestCase {
} }
public void testNextScheduledTimeAtFirstInterval() { public void testNextScheduledTimeAtFirstInterval() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + 2 * interval.millis(); long triggerTime = start + 2 * interval.millis();
long now = start + interval.millis(); long now = start + interval.millis();
TimeValueSchedule schedule = new TimeValueSchedule(interval); TimeValueSchedule schedule = new TimeValueSchedule(interval);
@ -49,16 +57,12 @@ public class TimeValueScheduleTests extends ESTestCase {
} }
public void testNextScheduledTimeAtStartTime() { public void testNextScheduledTimeAtStartTime() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + interval.millis(); long triggerTime = start + interval.millis();
TimeValueSchedule schedule = new TimeValueSchedule(interval); TimeValueSchedule schedule = new TimeValueSchedule(interval);
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, start)); assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, start));
} }
public void testNextScheduledTimeAfterFirstTrigger() { public void testNextScheduledTimeAfterFirstTrigger() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long numberIntervalsPassed = randomLongBetween(0, 10000); long numberIntervalsPassed = randomLongBetween(0, 10000);
long triggerTime = start + (numberIntervalsPassed + 1) * interval.millis(); long triggerTime = start + (numberIntervalsPassed + 1) * interval.millis();
long now = start long now = start