Skip to content

Commit

Permalink
SOLR-17618: Add unit tests for org.apache.solr.util.TimeOut
Browse files Browse the repository at this point in the history
This class did not appear to have any unit tests yet.
  • Loading branch information
sandbergja committed Jan 11, 2025
1 parent 440e70d commit d69e8f4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions solr/core/src/test/org/apache/solr/util/TimeOutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.apache.solr.util;

import org.apache.solr.SolrTestCase;
import org.apache.solr.common.util.TimeSource;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import static java.util.Arrays.asList;

public class TimeOutTest extends SolrTestCase {
public void testHasTimedOut() {
ArrayList<Long> times = new ArrayList<Long>(asList(Long.valueOf(10), Long.valueOf(50)));
TimeSource mockTimeSource = new MockTimeSource(times);

TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource);
assertTrue(timeOut.hasTimedOut());
}

public void testHasNotTimedOut() {
ArrayList<Long> times = new ArrayList<Long>(asList(Long.valueOf(10), Long.valueOf(11)));
TimeSource mockTimeSource = new MockTimeSource(times);

TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource);
assertFalse(timeOut.hasTimedOut());
}

public void testTimeLeft() {
ArrayList<Long> times = new ArrayList<Long>(asList(Long.valueOf(10), Long.valueOf(15)));
TimeSource mockTimeSource = new MockTimeSource(times);

TimeOut timeOut = new TimeOut(90, NANOSECONDS, mockTimeSource);
assertEquals(timeOut.timeLeft(NANOSECONDS), 85);
}

public void testTimeElapsed() {
ArrayList<Long> times = new ArrayList<Long>(asList(Long.valueOf(10), Long.valueOf(25)));
TimeSource mockTimeSource = new MockTimeSource(times);

TimeOut timeOut = new TimeOut(70, NANOSECONDS, mockTimeSource);
assertEquals(timeOut.timeElapsed(NANOSECONDS), 15);
}

private class MockTimeSource extends TimeSource {
private ArrayList<Long> times;

MockTimeSource(ArrayList<Long> times) {
this.times = times;
}

public long getTimeNs() {
return this.times.remove(0);
}

public long getEpochTimeNs() { throw new UnsupportedOperationException("Unimplemented method 'getEpochTimeNs'"); }
public long[] getTimeAndEpochNs() { throw new UnsupportedOperationException("Unimplemented method 'getTimeAndEpochNs'"); }
public void sleep(long ms) throws InterruptedException { throw new UnsupportedOperationException("Unimplemented method 'sleep'"); }
public long convertDelay(TimeUnit fromUnit, long value, TimeUnit toUnit) { throw new UnsupportedOperationException("Unimplemented method 'convertDelay'"); }
}
}

0 comments on commit d69e8f4

Please sign in to comment.