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

Avoid usage of the non monotonic clock System.currentTimeMillis() in favor of System.nanoTime() #6392

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public LinkedBlockingDeque<OutputFrame> getFrames() {
* @param predicate a predicate to test against each frame
*/
public void waitUntil(Predicate<OutputFrame> predicate) throws TimeoutException {
// ~2.9 million centuries ought to be enough for anyone
// ~2.9 thousands centuries ought to be enough for anyone
waitUntil(predicate, Long.MAX_VALUE, 1);
}

Expand Down Expand Up @@ -73,14 +73,18 @@ public void waitUntil(Predicate<OutputFrame> predicate, int limit, TimeUnit limi
*/
public void waitUntil(Predicate<OutputFrame> predicate, long limit, TimeUnit limitUnit, int times)
throws TimeoutException {
long expiry = limitUnit.toMillis(limit) + System.currentTimeMillis();
long timeoutLimitInNanos = limitUnit.toNanos(limit);

waitUntil(predicate, expiry, times);
waitUntil(predicate, timeoutLimitInNanos, times);
}

private void waitUntil(Predicate<OutputFrame> predicate, long expiry, int times) throws TimeoutException {
private void waitUntil(Predicate<OutputFrame> predicate, long timeoutLimitInNanos, int times)
throws TimeoutException {
int numberOfMatches = 0;
while (System.currentTimeMillis() < expiry) {

final long startTime = System.nanoTime();

while (System.nanoTime() - startTime < timeoutLimitInNanos) {
try {
final OutputFrame frame = frames.pollLast(100, TimeUnit.MILLISECONDS);

Expand Down Expand Up @@ -128,13 +132,13 @@ public void waitUntilEnd() {
* @param limitUnit maximum time to wait (units)
*/
public void waitUntilEnd(long limit, TimeUnit limitUnit) throws TimeoutException {
long expiry = limitUnit.toMillis(limit) + System.currentTimeMillis();
long expiry = limitUnit.toNanos(limit) + System.nanoTime();

waitUntilEnd(expiry);
}

private void waitUntilEnd(Long expiry) throws TimeoutException {
while (System.currentTimeMillis() < expiry) {
while (System.nanoTime() < expiry) {
try {
OutputFrame frame = frames.pollLast(100, TimeUnit.MILLISECONDS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected static void writeStringToFile(File contentFolder, String filename, Str
@Test
@Ignore //TODO investigate intermittent failures
public void failFastWhenContainerHaltsImmediately() {
long startingTimeMs = System.currentTimeMillis();
long startingTimeNano = System.nanoTime();
final GenericContainer failsImmediately = new GenericContainer<>(TestImages.ALPINE_IMAGE)
.withCommand("/bin/sh", "-c", "return false")
.withMinimumRunningDuration(Duration.ofMillis(100));
Expand All @@ -345,11 +345,11 @@ public void failFastWhenContainerHaltsImmediately() {

// Check how long it took, to verify that we ARE bailing out early.
// Want to strike a balance here; too short and this test will fail intermittently
// on slow systems and/or due to GC variation, too long and we won't properly test
// on slow systems and/or due to GC variation, too long, and we won't properly test
// what we're intending to test.
int allowedSecondsToFailure = GenericContainer.CONTAINER_RUNNING_TIMEOUT_SEC / 2;
long completedTimeMs = System.currentTimeMillis();
assertThat(completedTimeMs - startingTimeMs < 1000L * allowedSecondsToFailure)
long completedTimeNano = System.nanoTime();
assertThat(completedTimeNano - startingTimeNano < TimeUnit.SECONDS.toNanos(allowedSecondsToFailure))
.as("container should not take long to start up")
.isTrue();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.script.ScriptException;

Expand Down Expand Up @@ -273,7 +274,7 @@ public static void executeDatabaseScript(
LOGGER.info("Executing database script from " + scriptPath);
}

long startTime = System.currentTimeMillis();
long startTime = System.nanoTime();
List<String> statements = new LinkedList<>();

if (separator == null) {
Expand Down Expand Up @@ -306,7 +307,7 @@ public static void executeDatabaseScript(
closeableDelegate.execute(statements, scriptPath, continueOnError, ignoreFailedDrops);
}

long elapsedTime = System.currentTimeMillis() - startTime;
long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Executed database script from " + scriptPath + " in " + elapsedTime + " ms.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -148,10 +149,10 @@ protected void waitUntilContainerStarted() {
);

// Repeatedly try and open a connection to the DB and execute a test query
long start = System.currentTimeMillis();
long start = System.nanoTime();

Exception lastConnectionException = null;
while (System.currentTimeMillis() < start + (1000 * startupTimeoutSeconds)) {
while ((System.nanoTime() - start) < TimeUnit.SECONDS.toNanos(startupTimeoutSeconds)) {
if (!isRunning()) {
Thread.sleep(100L);
} else {
Expand Down Expand Up @@ -238,9 +239,9 @@ public Connection createConnection(String queryString, Properties info)

SQLException lastException = null;
try {
long start = System.currentTimeMillis();
long start = System.nanoTime();
// give up if we hit the time limit or the container stops running for some reason
while (System.currentTimeMillis() < start + (1000 * connectTimeoutSeconds) && isRunning()) {
while ((System.nanoTime() - start < TimeUnit.SECONDS.toNanos(connectTimeoutSeconds)) && isRunning()) {
try {
logger()
.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand Down Expand Up @@ -181,10 +182,10 @@ private void checkCallWithLatency(
int expectedMinLatency,
long expectedMaxLatency
) {
final long start = System.currentTimeMillis();
final long start = System.nanoTime();
String s = jedis.get("somekey");
final long end = System.currentTimeMillis();
final long duration = end - start;
final long end = System.nanoTime();
final long duration = TimeUnit.NANOSECONDS.toMillis(end - start);

assertThat(s).as(String.format("access to the container %s works OK", description)).isEqualTo("somevalue");
assertThat(duration >= expectedMinLatency)
Expand Down
Loading