Skip to content

Commit

Permalink
Add memory reporting for UI tests and exit for E4Testable on OOM
Browse files Browse the repository at this point in the history
Maybe this could help understanding OOM errors on jenkins.

Note: moved OpenCloseTest to the begin, just in case if that causes OOM.

See eclipse-platform#2432
  • Loading branch information
iloveeclipse committed Oct 21, 2024
1 parent 9f38380 commit cc079cf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.e4.ui.internal.workbench.swt;

import java.util.Locale;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.jobs.Job;
Expand Down Expand Up @@ -115,24 +116,61 @@ public void testingStarting() {
@Override
public void runTest(Runnable testRunnable) {
Assert.isNotNull(workbench);
display.syncExec(
() -> {
display.addListener(SWT.Dispose,
e -> {
// lambda block to allow breakpoint for debugging
displayCheck = new Exception("Display is disposed");
});

// Run actual test
testRunnable.run();

while (display.readAndDispatch()) {
// process all pending tasks
}

// Display should be still there
checkDisplay();
});
display.syncExec(new TestExecutionRunnable(testRunnable));
}

private final class TestExecutionRunnable implements Runnable {
private final Runnable testRunnable;

private TestExecutionRunnable(Runnable testRunnable) {
this.testRunnable = testRunnable;
}

@Override
public void run() {
display.addListener(SWT.Dispose,
e -> {
// lambda block to allow breakpoint for debugging
displayCheck = new Exception("Display is disposed");
});

try {
// Run actual test
testRunnable.run();
} catch (OutOfMemoryError e) {
try {
e.printStackTrace(System.out);
printMemoryUse();
} finally {
System.out.println("Calling System.exit() after OutOfMemoryError");
System.exit(1);
}
}

while (display.readAndDispatch()) {
// process all pending tasks
}

// Display should be still there
checkDisplay();
}
}

private static void printMemoryUse() {
System.gc();
System.runFinalization();
System.gc();
System.runFinalization();
long max = Runtime.getRuntime().maxMemory();
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
long used = total - free;
System.out.print("\n########### Memory usage reported by JVM ########");
System.out.printf(Locale.GERMAN, "%n%,16d bytes max heap", max);
System.out.printf(Locale.GERMAN, "%n%,16d bytes heap allocated", total);
System.out.printf(Locale.GERMAN, "%n%,16d bytes free heap", free);
System.out.printf(Locale.GERMAN, "%n%,16d bytes used heap", used);
System.out.println("\n#################################################\n");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -212,6 +213,7 @@ protected void doSetUp() throws Exception {
public final void tearDown() throws Exception {
String name = runningTest != null ? runningTest : this.getName();
trace(TestRunLogUtil.formatTestFinishedMessage(name));

prefMemento.resetPreferences();
doTearDown();

Expand All @@ -227,8 +229,28 @@ public final void tearDown() throws Exception {
fWorkbench = null;
assertEquals("Test leaked modal shell: [" + String.join(", ", leakedModalShellTitles) + "]", 0,
leakedModalShellTitles.size());
printMemoryUse();
}

protected void printMemoryUse() {
System.gc();
System.runFinalization();
System.gc();
System.runFinalization();
long nax = Runtime.getRuntime().maxMemory();
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
long used = total - free;
System.out.print("\n#################################################");
System.out.print("\n" + getClass().getName());
System.out.print("\n########### Memory usage reported by JVM ########");
System.out.printf(Locale.GERMAN, "%n%,16d bytes max heap", nax);
System.out.printf(Locale.GERMAN, "%n%,16d bytes heap allocated", total);
System.out.printf(Locale.GERMAN, "%n%,16d bytes free heap", free);
System.out.printf(Locale.GERMAN, "%n%,16d bytes used heap", used);
System.out.println("\n#################################################\n");
}

/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
StartupTest.class,
OpenCloseTest.class,
UIAutomatedSuite.class,
ApiTestSuite.class,
NavigatorTestSuite.class,
Expand Down Expand Up @@ -108,8 +109,7 @@
OpenSystemInPlaceEditorTest.class,
WorkbenchDatabindingTest.class,
ChooseWorkspaceDialogTests.class,
ViewerItemsLimitTest.class,
OpenCloseTest.class
ViewerItemsLimitTest.class
})
public class UiTestSuite {
}

0 comments on commit cc079cf

Please sign in to comment.