Skip to content

Commit 820b875

Browse files
committed
Add explicit gc and memory reporting for UI tests
Also exit E4Testable on OOM. This is supposed to workaround and to understand OOM errors on jenkins. See #2432
1 parent 0a3c1fd commit 820b875

File tree

3 files changed

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

3 files changed

+88
-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,7 @@ 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+
215217
prefMemento.resetPreferences();
216218
doTearDown();
217219

@@ -227,8 +229,28 @@ public final void tearDown() throws Exception {
227229
fWorkbench = null;
228230
assertEquals("Test leaked modal shell: [" + String.join(", ", leakedModalShellTitles) + "]", 0,
229231
leakedModalShellTitles.size());
232+
runGcAndPrintMemoryUse(name);
230233
}
231234

235+
public static void runGcAndPrintMemoryUse(String testName) {
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" + testName);
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.

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/stress/OpenCloseTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
import org.eclipse.ui.intro.IIntroPart;
4242
import org.eclipse.ui.part.FileEditorInput;
4343
import org.eclipse.ui.tests.harness.util.FileUtil;
44+
import org.eclipse.ui.tests.harness.util.UITestCase;
45+
import org.junit.After;
4446
import org.junit.Before;
47+
import org.junit.Rule;
4548
import org.junit.Test;
49+
import org.junit.rules.TestName;
4650

4751
/**
4852
* Test opening and closing of items.
@@ -56,6 +60,8 @@ public class OpenCloseTest {
5660
private IWorkbench workbench;
5761
private IWorkbenchPage page;
5862

63+
@Rule
64+
public TestName testName = new TestName();
5965

6066
@Before
6167
public void setup() {
@@ -76,6 +82,10 @@ public void setup() {
7682
assertNotNull(page);
7783
}
7884

85+
@After
86+
public void teardown() {
87+
UITestCase.runGcAndPrintMemoryUse(testName.getMethodName());
88+
}
7989

8090
/**
8191
* Test the opening and closing of a file.

0 commit comments

Comments
 (0)