Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-17618: Add unit tests for org.apache.solr.util.TimeOut #3026

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 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,65 @@
/*
* 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.SolrTestCaseJ4;
import org.apache.solr.common.util.TimeSource;
import org.junit.BeforeClass;

public class TimeOutTest extends SolrTestCaseJ4 {

@BeforeClass
public static void setUpOnce() {
assumeWorkingMockito();
}

public void testHasTimedOut() {
madrob marked this conversation as resolved.
Show resolved Hide resolved
TimeSource mockTimeSource = mock(TimeSource.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could go in a Before method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, good point! Thanks, @madrob !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do Before, not BeforeClass for it, and also put the when/thenReturn line in there as well.

Then in the tests adjust the timeouts to test various combinations if we have the same mock time source each time. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! I think I get it now. Thanks for working with me on this @madrob !

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);
}
}
Loading