Skip to content

Commit ed7afd1

Browse files
authored
[ILM] TEST: fix long overflow in TimeValueScheduleTests (#36384)
Closes #35948.
1 parent 58a5ad1 commit ed7afd1

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeValueScheduleTests.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
import org.elasticsearch.common.unit.TimeValue;
1010
import org.elasticsearch.test.ESTestCase;
1111
import org.elasticsearch.test.EqualsHashCodeTestUtils;
12+
import org.junit.Before;
1213

1314
import java.util.concurrent.TimeUnit;
1415

1516
public class TimeValueScheduleTests extends ESTestCase {
1617

18+
private long start;
19+
private TimeValue interval;
20+
1721
public TimeValueSchedule createRandomInstance() {
1822
return new TimeValueSchedule(createRandomTimeValue());
1923
}
@@ -22,7 +26,15 @@ private TimeValue createRandomTimeValue() {
2226
return new TimeValue(randomLongBetween(1, 10000), randomFrom(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS));
2327
}
2428

25-
public void testHascodeAndEquals() {
29+
@Before
30+
public void setUpStartAndInterval() {
31+
// start with random epoch between 1/1/1970 and 31/12/2035 so that start is not
32+
// so large such that (start + interval) > Long.MAX
33+
start = randomLongBetween(0, 2082672000000L);
34+
interval = createRandomTimeValue();
35+
}
36+
37+
public void testHashcodeAndEquals() {
2638
for (int i = 0; i < 20; i++) {
2739
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
2840
instance -> new TimeValueSchedule(instance.getInterval()),
@@ -31,34 +43,26 @@ public void testHascodeAndEquals() {
3143
}
3244

3345
public void testNextScheduledTimeFirstTriggerNotReached() {
34-
long start = randomNonNegativeLong();
35-
TimeValue interval = createRandomTimeValue();
3646
long triggerTime = start + interval.millis();
3747
long now = start + randomLongBetween(0, interval.millis() - 1);
3848
TimeValueSchedule schedule = new TimeValueSchedule(interval);
3949
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, now));
4050
}
4151

4252
public void testNextScheduledTimeAtFirstInterval() {
43-
long start = randomNonNegativeLong();
44-
TimeValue interval = createRandomTimeValue();
4553
long triggerTime = start + 2 * interval.millis();
4654
long now = start + interval.millis();
4755
TimeValueSchedule schedule = new TimeValueSchedule(interval);
4856
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, now));
4957
}
5058

5159
public void testNextScheduledTimeAtStartTime() {
52-
long start = randomNonNegativeLong();
53-
TimeValue interval = createRandomTimeValue();
5460
long triggerTime = start + interval.millis();
5561
TimeValueSchedule schedule = new TimeValueSchedule(interval);
5662
assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, start));
5763
}
5864

5965
public void testNextScheduledTimeAfterFirstTrigger() {
60-
long start = randomNonNegativeLong();
61-
TimeValue interval = createRandomTimeValue();
6266
long numberIntervalsPassed = randomLongBetween(0, 10000);
6367
long triggerTime = start + (numberIntervalsPassed + 1) * interval.millis();
6468
long now = start

0 commit comments

Comments
 (0)