Skip to content

Commit

Permalink
[ILM] TEST: fix long overflow in TimeValueScheduleTests (elastic#36384)
Browse files Browse the repository at this point in the history
  • Loading branch information
talevy committed Dec 10, 2018
1 parent bfc1c5b commit 27487c9
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.junit.Before;

import java.util.concurrent.TimeUnit;

public class TimeValueScheduleTests extends ESTestCase {

private long start;
private TimeValue interval;

public TimeValueSchedule createRandomInstance() {
return new TimeValueSchedule(createRandomTimeValue());
}
Expand All @@ -22,7 +26,15 @@ private TimeValue createRandomTimeValue() {
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++) {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
instance -> new TimeValueSchedule(instance.getInterval()),
Expand All @@ -31,34 +43,26 @@ public void testHascodeAndEquals() {
}

public void testNextScheduledTimeFirstTriggerNotReached() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + interval.millis();
long now = start + randomLongBetween(0, interval.millis() - 1);
TimeValueSchedule schedule = new TimeValueSchedule(interval);
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, now));
}

public void testNextScheduledTimeAtFirstInterval() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + 2 * interval.millis();
long now = start + interval.millis();
TimeValueSchedule schedule = new TimeValueSchedule(interval);
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, now));
}

public void testNextScheduledTimeAtStartTime() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long triggerTime = start + interval.millis();
TimeValueSchedule schedule = new TimeValueSchedule(interval);
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, start));
}

public void testNextScheduledTimeAfterFirstTrigger() {
long start = randomNonNegativeLong();
TimeValue interval = createRandomTimeValue();
long numberIntervalsPassed = randomLongBetween(0, 10000);
long triggerTime = start + (numberIntervalsPassed + 1) * interval.millis();
long now = start
Expand Down

0 comments on commit 27487c9

Please sign in to comment.