Skip to content

Commit

Permalink
Add explicit gc and memory reporting for UI tests
Browse files Browse the repository at this point in the history
Also exit E4Testable on OOM.

This is supposed to workaround and to understand OOM errors on jenkins.

See eclipse-platform#2432
  • Loading branch information
iloveeclipse committed Oct 22, 2024
1 parent 0a3c1fd commit 820b875
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 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());
runGcAndPrintMemoryUse(name);
}

public static void runGcAndPrintMemoryUse(String testName) {
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" + testName);
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 @@ -41,8 +41,12 @@
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.tests.harness.util.FileUtil;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

/**
* Test opening and closing of items.
Expand All @@ -56,6 +60,8 @@ public class OpenCloseTest {
private IWorkbench workbench;
private IWorkbenchPage page;

@Rule
public TestName testName = new TestName();

@Before
public void setup() {
Expand All @@ -76,6 +82,10 @@ public void setup() {
assertNotNull(page);
}

@After
public void teardown() {
UITestCase.runGcAndPrintMemoryUse(testName.getMethodName());
}

/**
* Test the opening and closing of a file.
Expand Down

0 comments on commit 820b875

Please sign in to comment.