Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory reporting for UI tests and exit for E4Testable on OOM #2433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading