Skip to content

Commit

Permalink
Debug tests: trying to get stable
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
EcljpseB0T committed Dec 3, 2024
1 parent a9a5f80 commit 4b294f3
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class IOConsoleTests extends AbstractDebugTest implements IPatternMatchListener {

private int fMatchCount;
private boolean fDisconnected = false;
private volatile boolean fDisconnected;

/**
* Constructor
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IEditorPart> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IThread> predicate = x -> {
try {
return x.getName().equals(name);
Expand All @@ -170,16 +170,16 @@ 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<IThread> match = Arrays.stream(threads).filter(predicate).findFirst();
if (match.isPresent()) {
return match.get();
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public class PerfBreakpointTests extends AbstractDebugPerformanceTest implements IBreakpointListener {

int breakpointCount = 0;
volatile int breakpointCount = 0;

/**
* Constructor
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,21 @@ 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()) {
// loop until the queue is empty
}
} else {
try {
Thread.sleep(10);
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading

0 comments on commit 4b294f3

Please sign in to comment.