Skip to content

Commit 55d06ea

Browse files
committed
Add memory reporting for UI tests and exit for E4Testable on OOM
Maybe this could help understanding OOM errors on jenkins. See #2432
1 parent 54479d7 commit 55d06ea

File tree

2 files changed

+78
-18
lines changed
  • bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt
  • tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util

2 files changed

+78
-18
lines changed

bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/E4Testable.java

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.e4.ui.internal.workbench.swt;
1515

16+
import java.util.Locale;
1617
import org.eclipse.core.runtime.Assert;
1718
import org.eclipse.core.runtime.OperationCanceledException;
1819
import org.eclipse.core.runtime.jobs.Job;
@@ -115,24 +116,61 @@ public void testingStarting() {
115116
@Override
116117
public void runTest(Runnable testRunnable) {
117118
Assert.isNotNull(workbench);
118-
display.syncExec(
119-
() -> {
120-
display.addListener(SWT.Dispose,
121-
e -> {
122-
// lambda block to allow breakpoint for debugging
123-
displayCheck = new Exception("Display is disposed");
124-
});
125-
126-
// Run actual test
127-
testRunnable.run();
128-
129-
while (display.readAndDispatch()) {
130-
// process all pending tasks
131-
}
132-
133-
// Display should be still there
134-
checkDisplay();
135-
});
119+
display.syncExec(new TestExecutionRunnable(testRunnable));
120+
}
121+
122+
private final class TestExecutionRunnable implements Runnable {
123+
private final Runnable testRunnable;
124+
125+
private TestExecutionRunnable(Runnable testRunnable) {
126+
this.testRunnable = testRunnable;
127+
}
128+
129+
@Override
130+
public void run() {
131+
display.addListener(SWT.Dispose,
132+
e -> {
133+
// lambda block to allow breakpoint for debugging
134+
displayCheck = new Exception("Display is disposed");
135+
});
136+
137+
try {
138+
// Run actual test
139+
testRunnable.run();
140+
} catch (OutOfMemoryError e) {
141+
try {
142+
e.printStackTrace(System.out);
143+
printMemoryUse();
144+
} finally {
145+
System.out.println("Calling System.exit() after OutOfMemoryError");
146+
System.exit(1);
147+
}
148+
}
149+
150+
while (display.readAndDispatch()) {
151+
// process all pending tasks
152+
}
153+
154+
// Display should be still there
155+
checkDisplay();
156+
}
157+
}
158+
159+
private static void printMemoryUse() {
160+
System.gc();
161+
System.runFinalization();
162+
System.gc();
163+
System.runFinalization();
164+
long max = Runtime.getRuntime().maxMemory();
165+
long total = Runtime.getRuntime().totalMemory();
166+
long free = Runtime.getRuntime().freeMemory();
167+
long used = total - free;
168+
System.out.print("\n########### Memory usage reported by JVM ########");
169+
System.out.printf(Locale.GERMAN, "%n%,16d bytes max heap", max);
170+
System.out.printf(Locale.GERMAN, "%n%,16d bytes heap allocated", total);
171+
System.out.printf(Locale.GERMAN, "%n%,16d bytes free heap", free);
172+
System.out.printf(Locale.GERMAN, "%n%,16d bytes used heap", used);
173+
System.out.println("\n#################################################\n");
136174
}
137175

138176
/**

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/UITestCase.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.PrintStream;
2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.Locale;
2425
import java.util.Set;
2526
import java.util.concurrent.atomic.AtomicBoolean;
2627

@@ -212,6 +213,8 @@ protected void doSetUp() throws Exception {
212213
public final void tearDown() throws Exception {
213214
String name = runningTest != null ? runningTest : this.getName();
214215
trace(TestRunLogUtil.formatTestFinishedMessage(name));
216+
printMemoryUse();
217+
215218
prefMemento.resetPreferences();
216219
doTearDown();
217220

@@ -229,6 +232,25 @@ public final void tearDown() throws Exception {
229232
leakedModalShellTitles.size());
230233
}
231234

235+
protected void printMemoryUse() {
236+
System.gc();
237+
System.runFinalization();
238+
System.gc();
239+
System.runFinalization();
240+
long nax = Runtime.getRuntime().maxMemory();
241+
long total = Runtime.getRuntime().totalMemory();
242+
long free = Runtime.getRuntime().freeMemory();
243+
long used = total - free;
244+
System.out.print("\n#################################################");
245+
System.out.print("\n" + getClass().getName());
246+
System.out.print("\n########### Memory usage reported by JVM ########");
247+
System.out.printf(Locale.GERMAN, "%n%,16d bytes max heap", nax);
248+
System.out.printf(Locale.GERMAN, "%n%,16d bytes heap allocated", total);
249+
System.out.printf(Locale.GERMAN, "%n%,16d bytes free heap", free);
250+
System.out.printf(Locale.GERMAN, "%n%,16d bytes used heap", used);
251+
System.out.println("\n#################################################\n");
252+
}
253+
232254
/**
233255
* Tears down the fixture, for example, close a network connection.
234256
* This method is called after a test is executed.

0 commit comments

Comments
 (0)