-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOLR-17618: Add unit tests for org.apache.solr.util.TimeOut
This class did not appear to have any unit tests yet.
- Loading branch information
1 parent
440e70d
commit d69e8f4
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'"); } | ||
} | ||
} |