From 53490804cd976d4ebfb2a82a7e4d1ba37270fba1 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 27 Mar 2024 09:57:56 +0100 Subject: [PATCH] [test framework] using more predictable timer in `TestTimeOut` GitOrigin-RevId: e2289c27ac09d33261d98799f9bbc61887cbaff2 --- .../testSrc/com/intellij/util/TestTimeOut.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/util/TestTimeOut.java b/platform/platform-tests/testSrc/com/intellij/util/TestTimeOut.java index f2aadfaf6c81..4ac51f2cae3e 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/TestTimeOut.java +++ b/platform/platform-tests/testSrc/com/intellij/util/TestTimeOut.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.util; import org.jetbrains.annotations.Contract; @@ -7,16 +7,15 @@ import org.jetbrains.annotations.NotNull; import java.util.concurrent.TimeUnit; public final class TestTimeOut { - private final long timeOutMs; + private final long endTime; - private TestTimeOut(long ms) { - timeOutMs = ms; + private TestTimeOut(long endTime) { + this.endTime = endTime; } - @NotNull @Contract(pure = true) - public static TestTimeOut setTimeout(long timeout, @NotNull TimeUnit unit) { - return new TestTimeOut(System.currentTimeMillis() + unit.toMillis(timeout)); + public static @NotNull TestTimeOut setTimeout(long timeout, @NotNull TimeUnit unit) { + return new TestTimeOut(System.nanoTime() + unit.toNanos(timeout)); } public boolean timedOut() { @@ -25,13 +24,13 @@ public final class TestTimeOut { public boolean timedOut(Object workProgress) { if (isTimedOut()) { - System.err.println("Timed out. Stopped at "+workProgress); + System.err.println("Timed out. Stopped at " + workProgress); return true; } return false; } public boolean isTimedOut() { - return System.currentTimeMillis() > timeOutMs; + return System.nanoTime() > endTime; } }