Skip to content

Commit 387b944

Browse files
committed
SOLR-17618: Add unit tests for org.apache.solr.util.TimeOut
This class did not appear to have any unit tests yet.
1 parent 440e70d commit 387b944

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.solr.util;
18+
19+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
22+
23+
import org.apache.solr.SolrTestCase;
24+
import org.apache.solr.common.util.TimeSource;
25+
26+
public class TimeOutTest extends SolrTestCase {
27+
public void testHasTimedOut() {
28+
TimeSource mockTimeSource = mock(TimeSource.class);
29+
when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50));
30+
31+
TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource);
32+
assertTrue(timeOut.hasTimedOut());
33+
}
34+
35+
public void testHasNotTimedOut() {
36+
TimeSource mockTimeSource = mock(TimeSource.class);
37+
when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(11));
38+
39+
TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource);
40+
assertFalse(timeOut.hasTimedOut());
41+
}
42+
43+
public void testTimeLeft() {
44+
TimeSource mockTimeSource = mock(TimeSource.class);
45+
when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(15));
46+
47+
TimeOut timeOut = new TimeOut(90, NANOSECONDS, mockTimeSource);
48+
assertEquals(timeOut.timeLeft(NANOSECONDS), 85);
49+
}
50+
51+
public void testTimeElapsed() {
52+
TimeSource mockTimeSource = mock(TimeSource.class);
53+
when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(25));
54+
55+
TimeOut timeOut = new TimeOut(70, NANOSECONDS, mockTimeSource);
56+
assertEquals(timeOut.timeElapsed(NANOSECONDS), 15);
57+
}
58+
}

0 commit comments

Comments
 (0)