From 4b294f35bb637fb236ddfc5cd9d8382c7c854fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Tue, 3 Dec 2024 10:58:51 +0100 Subject: [PATCH] Debug tests: trying to get stable * currentTimeMillis is not guaranteed to be monotonic -> use nanoTime * added missing volatile on variables that are waited on * uniformly sleep 1 ms during wait loops --- .../org/eclipse/debug/jdi/tests/TestAll.java | 8 +-- .../IOConsoleOutputActionDelegate.java | 4 +- .../debug/tests/console/IOConsoleTests.java | 8 +-- .../JavaDebugStackTraceConsoleTest.java | 17 ++++--- .../TestBufferredOutputActionDelegate.java | 4 +- .../debug/testplugin/DebugEventWaiter.java | 6 +-- .../jdt/debug/tests/AbstractDebugTest.java | 14 ++--- .../org/eclipse/jdt/debug/tests/TestUtil.java | 14 ++--- .../ConditionalBreakpointsTests.java | 6 +-- .../JavaThreadEventHandlerTests.java | 7 ++- .../breakpoints/ThreadNameChangeTests.java | 8 +-- .../performance/PerfBreakpointTests.java | 6 +-- .../debug/tests/ui/AbstractDebugUiTests.java | 12 ++--- .../tests/ui/AbstractDebugViewTests.java | 4 +- .../jdt/debug/tests/ui/DebugHoverTests.java | 4 +- .../debug/tests/ui/JavaSnippetEditorTest.java | 4 +- .../tests/variables/DetailFormatterTests.java | 51 +++++++------------ .../tests/variables/TestAnonymousInspect.java | 8 +-- 18 files changed, 84 insertions(+), 101 deletions(-) diff --git a/org.eclipse.jdt.debug.jdi.tests/tests/org/eclipse/debug/jdi/tests/TestAll.java b/org.eclipse.jdt.debug.jdi.tests/tests/org/eclipse/debug/jdi/tests/TestAll.java index 16697d03ad..88303384bc 100644 --- a/org.eclipse.jdt.debug.jdi.tests/tests/org/eclipse/debug/jdi/tests/TestAll.java +++ b/org.eclipse.jdt.debug.jdi.tests/tests/org/eclipse/debug/jdi/tests/TestAll.java @@ -163,11 +163,11 @@ private static AbstractJDITest run(junit.framework.TestResult result, Class t // Run test System.out.println("\n" + new java.util.Date()); System.out.println("Begin testing " + test.getName() + "..."); - long startTime= System.currentTimeMillis(); + long startNanos = System.nanoTime(); test.suite().run(result); - long endTime= System.currentTimeMillis(); - long runTime= endTime-startTime; - System.out.println("\nTime: "+runTime/1000+"."+runTime%1000); + long endNanos = System.nanoTime(); + long runNanos = endNanos - startNanos; + System.out.println("\nTime in ms: " + runNanos / 1_000_000); System.out.println("Done testing " + test.getName() + "."); return test; diff --git a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleOutputActionDelegate.java b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleOutputActionDelegate.java index b54bdca3fd..185510068a 100644 --- a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleOutputActionDelegate.java +++ b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleOutputActionDelegate.java @@ -67,14 +67,14 @@ public void run(IAction action) { Runnable r = new Runnable() { @Override public void run() { - long start = System.currentTimeMillis(); + long start = System.nanoTime(); for (int i = 0; i < 1000; i++) { stream.print(Integer.toString(i)); stream.print(": Testing..."); //$NON-NLS-1$ stream.print("one..."); //$NON-NLS-1$ stream.println("two.... three...."); //$NON-NLS-1$ } - stream.println("Total time: " + (System.currentTimeMillis()-start)); //$NON-NLS-1$ + stream.println("Total time ms: " + (System.nanoTime() - start) / 1_000_000); //$NON-NLS-1$ } }; new Thread(r).start(); diff --git a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleTests.java b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleTests.java index 73b8dad578..60a8d7b0b2 100644 --- a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleTests.java +++ b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/IOConsoleTests.java @@ -29,7 +29,7 @@ public class IOConsoleTests extends AbstractDebugTest implements IPatternMatchListener { private int fMatchCount; - private boolean fDisconnected = false; + private volatile boolean fDisconnected; /** * Constructor @@ -53,10 +53,10 @@ public void testPatternMatchListener() throws Exception { stream.println(); stream.print("two foo bar"); - long endTime = System.currentTimeMillis() + 1500; - while (!fDisconnected && System.currentTimeMillis() < endTime) { + long timeoutNanos = System.nanoTime() + 1500 * 1_000_000; + while (!fDisconnected && System.nanoTime() < timeoutNanos) { synchronized(this) { - wait(500); + wait(1); } } diff --git a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/JavaDebugStackTraceConsoleTest.java b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/JavaDebugStackTraceConsoleTest.java index 5349110b61..7a0e617593 100644 --- a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/JavaDebugStackTraceConsoleTest.java +++ b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/JavaDebugStackTraceConsoleTest.java @@ -18,6 +18,8 @@ import static org.junit.Assert.assertArrayEquals; +import java.util.concurrent.atomic.AtomicReference; + import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.debug.testplugin.JavaProjectHelper; @@ -222,18 +224,17 @@ private static String getSelectedText(IEditorPart editor) { private IEditorPart waitForEditorOpen() throws Exception { waitForJobs(); - IEditorPart[] editor = new IEditorPart[1]; - sync(() -> editor[0] = getActivePage().getActiveEditor()); - long timeout = 30_000; - long start = System.currentTimeMillis(); - while (editor[0] == null && System.currentTimeMillis() - start < timeout) { + AtomicReference editor = new AtomicReference<>(); + sync(() -> editor.set(getActivePage().getActiveEditor())); + long timeoutNanos = System.nanoTime() + 30_000 * 1_000_000; + while (editor.get() == null && System.nanoTime() < timeoutNanos) { waitForJobs(); - sync(() -> editor[0] = getActivePage().getActiveEditor()); + sync(() -> editor.set(getActivePage().getActiveEditor())); } - if (editor[0] == null) { + if (editor.get() == null) { throw new AssertionError("Timeout occurred while waiting for editor to open"); } - return editor[0]; + return editor.get(); } private void waitForJobs() throws Exception { diff --git a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/TestBufferredOutputActionDelegate.java b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/TestBufferredOutputActionDelegate.java index 6394abf1b5..1752bcab65 100644 --- a/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/TestBufferredOutputActionDelegate.java +++ b/org.eclipse.jdt.debug.tests/console tests/org/eclipse/jdt/debug/tests/console/TestBufferredOutputActionDelegate.java @@ -79,14 +79,14 @@ public void run(IAction action) { Runnable r = new Runnable() { @Override public void run() { - long start = System.currentTimeMillis(); + long startNano = System.nanoTime(); for (int i = 0; i < 1000; i++) { stream.print(Integer.toString(i)); stream.print(": Testing..."); //$NON-NLS-1$ stream.print("one..."); //$NON-NLS-1$ stream.println("two.... three...."); //$NON-NLS-1$ } - stream.println("Total time: " + (System.currentTimeMillis()-start)); //$NON-NLS-1$ + stream.println("Total time ms: " + (System.nanoTime() - startNano) / 1_000_000); //$NON-NLS-1$ } }; new Thread(r).start(); diff --git a/org.eclipse.jdt.debug.tests/test plugin/org/eclipse/jdt/debug/testplugin/DebugEventWaiter.java b/org.eclipse.jdt.debug.tests/test plugin/org/eclipse/jdt/debug/testplugin/DebugEventWaiter.java index 9324f1f102..f49a854503 100644 --- a/org.eclipse.jdt.debug.tests/test plugin/org/eclipse/jdt/debug/testplugin/DebugEventWaiter.java +++ b/org.eclipse.jdt.debug.tests/test plugin/org/eclipse/jdt/debug/testplugin/DebugEventWaiter.java @@ -155,9 +155,9 @@ public synchronized Object waitForEvent() { if (fEvent == null) { try { if (enableUIEventLoopProcessing && Display.getCurrent() != null) { - long endTime = System.currentTimeMillis() + fTimeout; - while (fEvent == null && System.currentTimeMillis() < endTime) { - wait(10); + long timeoutNanos = System.nanoTime() + fTimeout * 1_000_000; + while (fEvent == null && System.nanoTime() < timeoutNanos) { + wait(1); TestUtil.runEventLoop(); } } else { diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java index f0c51d4b21..110c43c563 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java @@ -2394,9 +2394,9 @@ public void evaluationComplete(IEvaluationResult result) { ASTEvaluationEngine engine = new ASTEvaluationEngine(getProjectContext(), (IJavaDebugTarget) thread.getDebugTarget()); try { engine.evaluate(snippet, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false); - long timeout = System.currentTimeMillis()+DEFAULT_TIMEOUT; - while(listener.fResult == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); + long timeoutNanos = System.nanoTime() + DEFAULT_TIMEOUT * 1_000_000; + while (listener.fResult == null && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); } return listener.fResult; } @@ -2857,7 +2857,7 @@ protected IValue doEval(IJavaThread thread, String snippet) throws Exception{ */ protected IValue doEval(IJavaThread thread, StackFrameSupplier frameSupplier, String snippet) throws Exception { class Listener implements IEvaluationListener { - IEvaluationResult fResult; + volatile IEvaluationResult fResult; @Override public void evaluationComplete(IEvaluationResult result) { @@ -2874,9 +2874,9 @@ public IEvaluationResult getResult() { ASTEvaluationEngine engine = new ASTEvaluationEngine(getProjectContext(), (IJavaDebugTarget) thread.getDebugTarget()); try { engine.evaluate(snippet, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false); - long timeout = System.currentTimeMillis() + 5000; - while(listener.getResult() == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); + long timeoutNanos = System.nanoTime() + 5000 * 1_000_000; + while (listener.getResult() == null && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); } IEvaluationResult result = listener.getResult(); assertNotNull("The evaluation should have result: ", result); diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java index e34d7aae77..d2884bc334 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java @@ -88,12 +88,12 @@ public static void runEventLoop() { } } } else { - long start = System.currentTimeMillis(); + long timeoutNanos = System.nanoTime() + AbstractDebugTest.DEFAULT_TIMEOUT * 1_000_000; AtomicBoolean stop = new AtomicBoolean(); Display.getDefault().asyncExec(() -> stop.set(true)); - while (!stop.get() && System.currentTimeMillis() - start < AbstractDebugTest.DEFAULT_TIMEOUT) { + while (!stop.get() && System.nanoTime() < timeoutNanos) { try { - Thread.sleep(10); + Thread.sleep(1); } catch (InterruptedException e) { break; } @@ -137,11 +137,11 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, throw new IllegalArgumentException("Max time is smaller as min time!"); } wakeUpSleepingJobs(null); - final long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start < minTimeMs) { + final long startNanos = System.nanoTime(); + while (System.nanoTime() - startNanos < minTimeMs * 1_000_000) { runEventLoop(); try { - Thread.sleep(Math.min(10, minTimeMs)); + Thread.sleep(1); } catch (InterruptedException e) { // Uninterruptable } @@ -165,7 +165,7 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, return true; } - if (System.currentTimeMillis() - start >= maxTimeMs) { + if (System.nanoTime() - startNanos >= maxTimeMs * 1_000_000) { dumpRunningOrWaitingJobs(owner, jobs); return true; } diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java index 367cb06750..81d1f2497d 100755 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java @@ -200,9 +200,9 @@ public void testSuspendLongRunningCondition() throws Exception { assertNotNull("Missing top frame", top); thread.resume(); // wait for evaluation to start - long start = System.currentTimeMillis(); - while ((System.currentTimeMillis() - start) <= DEFAULT_TIMEOUT && !thread.isPerformingEvaluation()) { - Thread.sleep(10); + long timeoutNanos = System.nanoTime() + DEFAULT_TIMEOUT * 1_000_000; + while (!thread.isPerformingEvaluation() && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); } assertTrue("Expected evaluation for second breakpoint", thread.isPerformingEvaluation()); /* diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/JavaThreadEventHandlerTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/JavaThreadEventHandlerTests.java index f554850092..d5d1f47083 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/JavaThreadEventHandlerTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/JavaThreadEventHandlerTests.java @@ -200,10 +200,9 @@ public IEvaluationResult getResult() { try { assertTrue(thread.isSuspended()); engine.evaluate(snippet, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false); - long timeout = System.currentTimeMillis() + 5000; - while (thread.isSuspended() && System.currentTimeMillis() < timeout) { - System.out.println("Waiting for evaluation to start.."); - Thread.sleep(10); + long timeoutNanos = System.nanoTime() + 5000 * 1_000_000; + while (thread.isSuspended() && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); } // evaluation must be running now diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ThreadNameChangeTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ThreadNameChangeTests.java index 8f9cf70832..48d95ec0dc 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ThreadNameChangeTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ThreadNameChangeTests.java @@ -158,7 +158,7 @@ private IThread findThread(IJavaThread thread, String name) throws DebugExceptio return findThread(thread, name, 5_000); } - private IThread findThread(IJavaThread thread, String name, long timeout) throws DebugException { + private IThread findThread(IJavaThread thread, String name, long timeoutMs) throws DebugException { Predicate predicate = x -> { try { return x.getName().equals(name); @@ -170,8 +170,8 @@ private IThread findThread(IJavaThread thread, String name, long timeout) throws IThread[] threads = {}; // Wait until timeout or JDIDebugTarget.ThreadStartHandler has added the thread. - long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start <= timeout) { + long timeoutNanos = System.nanoTime() + timeoutMs * 1_000_000; + while (System.nanoTime() <= timeoutNanos) { threads = thread.getDebugTarget().getThreads(); Optional match = Arrays.stream(threads).filter(predicate).findFirst(); if (match.isPresent()) { @@ -179,7 +179,7 @@ private IThread findThread(IJavaThread thread, String name, long timeout) throws } } - throw new AssertionError("Timeout of " + timeout + "ms reached, thread with name \"" + name + "\" not found in set of threads: " + throw new AssertionError("Timeout of " + timeoutMs + "ms reached, thread with name \"" + name + "\" not found in set of threads: " + Arrays.asList(threads)); } diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/performance/PerfBreakpointTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/performance/PerfBreakpointTests.java index f9e172d9a0..0f94d0d87c 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/performance/PerfBreakpointTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/performance/PerfBreakpointTests.java @@ -34,7 +34,7 @@ */ public class PerfBreakpointTests extends AbstractDebugPerformanceTest implements IBreakpointListener { - int breakpointCount = 0; + volatile int breakpointCount = 0; /** * Constructor @@ -247,8 +247,8 @@ public void testWatchpointCreation() throws Exception { * Waits for the specified breakpoint count to be hit */ private synchronized void waitForBreakpointCount(int i) throws Exception { - long end = System.currentTimeMillis() + 60000; - while (breakpointCount != i && System.currentTimeMillis() < end) { + long timeoutNanos = System.nanoTime() + 60000 * 1_000_000; + while (breakpointCount != i && System.nanoTime() < timeoutNanos) { wait(30000); } assertEquals("Expected " + i + " breakpoints, notified of " + breakpointCount, i, breakpointCount); diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugUiTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugUiTests.java index e60d2ef6e8..8e4b381b9f 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugUiTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugUiTests.java @@ -164,13 +164,13 @@ public static void processUiEvents() throws RuntimeException { /** * Process all queued UI events. If called from background thread, just waits * - * @param millis - * max wait time to process events + * @param timeoutMs + * max wait time in milliseconds to process events */ - public static void processUiEvents(final long millis) throws RuntimeException { + public static void processUiEvents(final long timeoutMs) throws RuntimeException { sync(() -> { - long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start < millis) { + long timeoutNanos = System.nanoTime() + timeoutMs * 1_000_000; + while (System.nanoTime() < timeoutNanos) { Display display = Display.getCurrent(); if (display != null && !display.isDisposed()) { while (display.readAndDispatch()) { @@ -178,7 +178,7 @@ public static void processUiEvents(final long millis) throws RuntimeException { } } else { try { - Thread.sleep(10); + Thread.sleep(1); } catch (InterruptedException e) { break; } diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugViewTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugViewTests.java index 6875dff31b..49fc726c43 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugViewTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/AbstractDebugViewTests.java @@ -240,10 +240,10 @@ protected TreeItem[] getSelectedItemsFromDebugView(boolean wait) throws Exceptio if (!wait) { return selected; } - long start = System.currentTimeMillis(); + long timeoutNanos = System.nanoTime() + 10000 * 1_000_000; // At least on GTK3 it takes some time until we see the viewer selection propagated to the SWT tree - while (selected.length != 1 && System.currentTimeMillis() - start < 10000) { + while (selected.length != 1 && System.nanoTime() < timeoutNanos) { TreeViewer treeViewer = (TreeViewer) debugView.getViewer(); treeViewer.refresh(true); processUiEvents(500); diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/DebugHoverTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/DebugHoverTests.java index c0274050a1..0f25850623 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/DebugHoverTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/DebugHoverTests.java @@ -1473,12 +1473,12 @@ private void selectFrame(CompilationUnitEditor editor, IStackFrame frame) throws final int timeoutms = 10000; int selectedLineNumer = sync(() -> { int lineNumber; - long endtime = System.currentTimeMillis() + timeoutms; + long timeoutNanos = System.nanoTime() + timeoutms * 1_000_000; debugView.getViewer().setSelection(newSelection, true); do { TestUtil.runEventLoop(); lineNumber = ((ITextSelection) editor.getSelectionProvider().getSelection()).getStartLine(); - } while (lineNumber != targetLineNumber && System.currentTimeMillis() < endtime); + } while (lineNumber != targetLineNumber && System.nanoTime() < timeoutNanos); return lineNumber; }); assertEquals("After waiting " + timeoutms diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/JavaSnippetEditorTest.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/JavaSnippetEditorTest.java index 8a520c56fe..ef3bf58801 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/JavaSnippetEditorTest.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/JavaSnippetEditorTest.java @@ -91,8 +91,8 @@ public void testEvaluation() throws Exception { }); // Evaluation runs in a separated thread (not a job), so let wait for it - long start = System.currentTimeMillis(); - while (snippetEditor.isEvaluating() && System.currentTimeMillis() - start < 60_000) { + long timeoutNanos = System.nanoTime() + 60_000 * 1_000_000; + while (snippetEditor.isEvaluating() && System.nanoTime() < timeoutNanos) { processUiEvents(1000); } diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/DetailFormatterTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/DetailFormatterTests.java index 0e9db61e2d..69a8f2ad67 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/DetailFormatterTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/DetailFormatterTests.java @@ -32,8 +32,8 @@ public class DetailFormatterTests extends AbstractDebugTest { static class TestListener implements IValueDetailListener { - IValue value; - String result; + volatile IValue value; + volatile String result; @Override public void detailComputed(IValue value, String result) { @@ -98,10 +98,7 @@ public void testCompoundDetails1() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -134,10 +131,7 @@ public void testCompoundDetails2() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -168,10 +162,7 @@ public void testSimpleDetails1() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -202,10 +193,7 @@ public void testSimpleDetails2() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -236,10 +224,7 @@ public void testInfixDetails1() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -253,6 +238,13 @@ public void testInfixDetails1() throws Exception { } } + private void waitForListenerValue() throws InterruptedException { + long timeoutNanos = System.nanoTime() + 5000 * 1_000_000; + while (fListener.value == null && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); + } + } + /** * Tests a detail formatter made from an infix expression * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=403028" @@ -272,10 +264,7 @@ public void testInfixDetails2() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -307,10 +296,7 @@ public void testInfixDetails3() throws Exception { IJavaVariable var = thread.findVariable("map"); assertNotNull("the variable 'map' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis()+5000; - while(fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertEquals("The map should be an instance of java.util.LinkedHashMap", "java.util.LinkedHashMap", Signature.getTypeErasure(fListener.value.getReferenceTypeName())); assertNotNull("The computed value of the detail should not be null", fListener.result); @@ -344,10 +330,7 @@ public void testHoverWithNoTypeArguments() throws Exception { IJavaVariable var = thread.findVariable("coll"); assertNotNull("the variable 'coll' must exist in the frame", var); jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener); - long timeout = System.currentTimeMillis() + 5000; - while (fListener.value == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); - } + waitForListenerValue(); assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value); assertNotNull("The computed value of the detail should not be null", fListener.result); } diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/TestAnonymousInspect.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/TestAnonymousInspect.java index cd3d2919b4..9ad107f309 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/TestAnonymousInspect.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/variables/TestAnonymousInspect.java @@ -33,7 +33,7 @@ public class TestAnonymousInspect extends AbstractDebugTest { static final String SNIPPET = "getchar()"; static class Listener implements IEvaluationListener { - IEvaluationResult fResult; + volatile IEvaluationResult fResult; @Override public void evaluationComplete(IEvaluationResult result) { @@ -72,9 +72,9 @@ IValue doEval(IJavaThread thread) throws Exception{ ASTEvaluationEngine engine = new ASTEvaluationEngine(get14Project(), (IJavaDebugTarget) thread.getDebugTarget()); try { engine.evaluate(SNIPPET, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false); - long timeout = System.currentTimeMillis()+5000; - while(listener.fResult == null && System.currentTimeMillis() < timeout) { - Thread.sleep(100); + long timeoutNanos = System.nanoTime() + 5000 * 1_000_000; + while (listener.fResult == null && System.nanoTime() < timeoutNanos) { + Thread.sleep(1); } assertFalse("The evaluation should not have errors", listener.fResult.hasErrors()); return listener.fResult.getValue();