From 387b94480f413e379c5da3f5315e0ce0925ecc3c Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Sat, 11 Jan 2025 14:17:53 -0800 Subject: [PATCH 1/4] SOLR-17618: Add unit tests for org.apache.solr.util.TimeOut This class did not appear to have any unit tests yet. --- .../org/apache/solr/util/TimeOutTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 solr/core/src/test/org/apache/solr/util/TimeOutTest.java diff --git a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java new file mode 100644 index 00000000000..3b59644e082 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.util; + +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.solr.SolrTestCase; +import org.apache.solr.common.util.TimeSource; + +public class TimeOutTest extends SolrTestCase { + public void testHasTimedOut() { + TimeSource mockTimeSource = mock(TimeSource.class); + when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50)); + + TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); + assertTrue(timeOut.hasTimedOut()); + } + + public void testHasNotTimedOut() { + TimeSource mockTimeSource = mock(TimeSource.class); + when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(11)); + + TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); + assertFalse(timeOut.hasTimedOut()); + } + + public void testTimeLeft() { + TimeSource mockTimeSource = mock(TimeSource.class); + when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(15)); + + TimeOut timeOut = new TimeOut(90, NANOSECONDS, mockTimeSource); + assertEquals(timeOut.timeLeft(NANOSECONDS), 85); + } + + public void testTimeElapsed() { + TimeSource mockTimeSource = mock(TimeSource.class); + when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(25)); + + TimeOut timeOut = new TimeOut(70, NANOSECONDS, mockTimeSource); + assertEquals(timeOut.timeElapsed(NANOSECONDS), 15); + } +} From 51f7b0c0b97bbdd901f3a20cdc19ff9f9827e63d Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Sat, 11 Jan 2025 22:22:28 -0800 Subject: [PATCH 2/4] SOLR-17618: Add assumeWorkingMockito --- .../src/test/org/apache/solr/util/TimeOutTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java index 3b59644e082..d6defa00c95 100644 --- a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java +++ b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java @@ -20,10 +20,17 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.apache.solr.SolrTestCase; +import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.TimeSource; +import org.junit.BeforeClass; + +public class TimeOutTest extends SolrTestCaseJ4 { + + @BeforeClass + public static void setUpOnce() { + assumeWorkingMockito(); + } -public class TimeOutTest extends SolrTestCase { public void testHasTimedOut() { TimeSource mockTimeSource = mock(TimeSource.class); when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50)); From c445b5afd684be61bda297deabe860b8f16a04a9 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Sun, 12 Jan 2025 08:51:09 -0800 Subject: [PATCH 3/4] SOLR-17618: Incorporate feedback from review * Add @Test annotations * Move repeated mock setup to Before method --- .../test/org/apache/solr/util/TimeOutTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java index d6defa00c95..b316affa2be 100644 --- a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java +++ b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java @@ -23,42 +23,41 @@ import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.TimeSource; import org.junit.BeforeClass; +import org.junit.Test; public class TimeOutTest extends SolrTestCaseJ4 { + private static TimeSource mockTimeSource; @BeforeClass public static void setUpOnce() { assumeWorkingMockito(); + mockTimeSource = mock(TimeSource.class); } + @Test public void testHasTimedOut() { - TimeSource mockTimeSource = mock(TimeSource.class); when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50)); - TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); assertTrue(timeOut.hasTimedOut()); } + @Test public void testHasNotTimedOut() { - TimeSource mockTimeSource = mock(TimeSource.class); when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(11)); - TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); assertFalse(timeOut.hasTimedOut()); } + @Test public void testTimeLeft() { - TimeSource mockTimeSource = mock(TimeSource.class); when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(15)); - TimeOut timeOut = new TimeOut(90, NANOSECONDS, mockTimeSource); assertEquals(timeOut.timeLeft(NANOSECONDS), 85); } + @Test public void testTimeElapsed() { - TimeSource mockTimeSource = mock(TimeSource.class); when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(25)); - TimeOut timeOut = new TimeOut(70, NANOSECONDS, mockTimeSource); assertEquals(timeOut.timeElapsed(NANOSECONDS), 15); } From c4ffd27680e14e40d8ae323e2e8b088fdceb71b5 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Sun, 12 Jan 2025 09:51:02 -0800 Subject: [PATCH 4/4] SOLR-17618: Use the same mock data in a @Before for all tests --- .../org/apache/solr/util/TimeOutTest.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java index b316affa2be..c7d38fa7923 100644 --- a/solr/core/src/test/org/apache/solr/util/TimeOutTest.java +++ b/solr/core/src/test/org/apache/solr/util/TimeOutTest.java @@ -22,6 +22,7 @@ import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.TimeSource; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -31,34 +32,35 @@ public class TimeOutTest extends SolrTestCaseJ4 { @BeforeClass public static void setUpOnce() { assumeWorkingMockito(); + } + + @Before + public void timeSourceMock() { mockTimeSource = mock(TimeSource.class); + when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50)); } @Test public void testHasTimedOut() { - when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(50)); TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); assertTrue(timeOut.hasTimedOut()); } @Test public void testHasNotTimedOut() { - when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(11)); - TimeOut timeOut = new TimeOut(20, NANOSECONDS, mockTimeSource); + TimeOut timeOut = new TimeOut(100, NANOSECONDS, mockTimeSource); assertFalse(timeOut.hasTimedOut()); } @Test public void testTimeLeft() { - when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(15)); - TimeOut timeOut = new TimeOut(90, NANOSECONDS, mockTimeSource); - assertEquals(timeOut.timeLeft(NANOSECONDS), 85); + TimeOut timeOut = new TimeOut(240, NANOSECONDS, mockTimeSource); + assertEquals(timeOut.timeLeft(NANOSECONDS), 200); } @Test public void testTimeElapsed() { - when(mockTimeSource.getTimeNs()).thenReturn(Long.valueOf(10)).thenReturn(Long.valueOf(25)); - TimeOut timeOut = new TimeOut(70, NANOSECONDS, mockTimeSource); - assertEquals(timeOut.timeElapsed(NANOSECONDS), 15); + TimeOut timeOut = new TimeOut(100, NANOSECONDS, mockTimeSource); + assertEquals(timeOut.timeElapsed(NANOSECONDS), 40); } }