Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2014-11-12 13:48:48 +0000
committerChristian W. Damus2014-11-12 13:52:14 +0000
commit4de8197ae466eb9a02c7c4fc38b4dc0107712bc4 (patch)
tree51a4dd37b3adc384ce005d93abd572cd86a186aa /tests/junit/plugins/junit
parent9deff49e40598815d5ea14d3de973a0f8d3fe637 (diff)
downloadorg.eclipse.papyrus-4de8197ae466eb9a02c7c4fc38b4dc0107712bc4.tar.gz
org.eclipse.papyrus-4de8197ae466eb9a02c7c4fc38b4dc0107712bc4.tar.xz
org.eclipse.papyrus-4de8197ae466eb9a02c7c4fc38b4dc0107712bc4.zip
451013: [Tests] Intermittent failure in memory leak tests
https://bugs.eclipse.org/bugs/show_bug.cgi?id=451013 Put the debug messages behind a system property.
Diffstat (limited to 'tests/junit/plugins/junit')
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.framework/src/org/eclipse/papyrus/junit/framework/classification/rules/MemoryLeakRule.java42
1 files changed, 25 insertions, 17 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.framework/src/org/eclipse/papyrus/junit/framework/classification/rules/MemoryLeakRule.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.framework/src/org/eclipse/papyrus/junit/framework/classification/rules/MemoryLeakRule.java
index 82732b15950..cb08cbf3316 100644
--- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.framework/src/org/eclipse/papyrus/junit/framework/classification/rules/MemoryLeakRule.java
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.framework/src/org/eclipse/papyrus/junit/framework/classification/rules/MemoryLeakRule.java
@@ -52,6 +52,7 @@ import com.google.common.collect.Lists;
* @see SoftReferenceSensitive
*/
public class MemoryLeakRule extends TestWatcher {
+ private static final boolean DEBUG = Boolean.getBoolean("MemoryLeakRule.debug");
private static final int DEQUEUE_REF_ITERATIONS = 3;
@@ -84,7 +85,7 @@ public class MemoryLeakRule extends TestWatcher {
public void add(Object leak) {
assertThat("Cannot track null references for memory leaks.", leak, notNullValue());
- if(queue == null) {
+ if (queue == null) {
queue = new ReferenceQueue<Object>();
tracker = Lists.newArrayList();
factory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
@@ -104,7 +105,7 @@ public class MemoryLeakRule extends TestWatcher {
isSoftReferenceSensitive = description.getAnnotation(SoftReferenceSensitive.class) != null;
- if(isSoftReferenceSensitive && !isWarmedUp() && !warmingUp) {
+ if (isSoftReferenceSensitive && !isWarmedUp() && !warmingUp) {
// Warm up the soft-reference sensitive tests by running this one up-front, first,
// because the first such test to execute always results in a spurious failure
// (at least, such is the case on the Mac build of JRE 1.6)
@@ -120,16 +121,16 @@ public class MemoryLeakRule extends TestWatcher {
@Override
protected void succeeded(Description description) {
// If the test's assertions (if any) all succeeded, then check for leaks on the way out
- if(tracker == null) {
+ if (tracker == null) {
// No leaks to assert
return;
}
// Assert that our tracked objects are now all unreachable
- while(!tracker.isEmpty()) {
+ while (!tracker.isEmpty()) {
Reference<?> ref = dequeueTracker();
- for(int i = 0; ((ref == null) && isSoftReferenceSensitive) && (i < CLEAR_SOFT_REFS_ITERATIONS); i++) {
+ for (int i = 0; ((ref == null) && isSoftReferenceSensitive) && (i < CLEAR_SOFT_REFS_ITERATIONS); i++) {
// Maybe there are soft references retaining our objects? Desperation move.
// On some platforms, our simulated OOME doesn't actually purge all soft
// references (contrary to Java spec!), so we have to repeat
@@ -139,11 +140,11 @@ public class MemoryLeakRule extends TestWatcher {
ref = dequeueTracker();
}
- if(!tracker.remove(ref) && !tracker.isEmpty()) {
+ if (!tracker.remove(ref) && !tracker.isEmpty()) {
// The remaining tracked elements are leaked
final String leaks = Joiner.on('\n').join(Iterables.transform(tracker, label()));
if (warmingUp) {
- System.err.printf("[MEM] Warm-up detected leaks: %s%n", leaks.replace('\n', ' '));
+ debug("Warm-up detected leaks: %s%n", leaks.replace('\n', ' '));
}
fail("One or more objects leaked:\n" + leaks);
break; // Unreachable
@@ -160,7 +161,7 @@ public class MemoryLeakRule extends TestWatcher {
}
void disposeFactory() {
- if(factory != null) {
+ if (factory != null) {
factory.dispose();
factory = null;
}
@@ -170,7 +171,7 @@ public class MemoryLeakRule extends TestWatcher {
Reference<?> result = null;
try {
- for(int i = 0; (result == null) && (i < DEQUEUE_REF_ITERATIONS); i++) {
+ for (int i = 0; (result == null) && (i < DEQUEUE_REF_ITERATIONS); i++) {
// Try to force GC
collectGarbage();
@@ -187,6 +188,7 @@ public class MemoryLeakRule extends TestWatcher {
Function<WeakReference<?>, String> label() {
return new Function<WeakReference<?>, String>() {
+ @Override
public String apply(WeakReference<?> input) {
return label(input.get());
}
@@ -194,10 +196,10 @@ public class MemoryLeakRule extends TestWatcher {
}
String label(Object input) {
- IItemLabelProvider provider = (IItemLabelProvider)factory.adapt(input, IItemLabelProvider.class);
+ IItemLabelProvider provider = (IItemLabelProvider) factory.adapt(input, IItemLabelProvider.class);
String result = (provider == null) ? String.valueOf(input) : provider.getText(input);
- if(Strings.isNullOrEmpty(result)) {
+ if (Strings.isNullOrEmpty(result)) {
result = String.valueOf(input);
}
@@ -211,7 +213,7 @@ public class MemoryLeakRule extends TestWatcher {
Long usedMem = rt.totalMemory() - rt.freeMemory();
Long prevUsedMem = usedMem;
- for(int i = 0; (prevUsedMem <= usedMem) && (i < GC_ITERATIONS); i++) {
+ for (int i = 0; (prevUsedMem <= usedMem) && (i < GC_ITERATIONS); i++) {
rt.gc();
Thread.yield();
@@ -222,21 +224,21 @@ public class MemoryLeakRule extends TestWatcher {
void forceClearSoftReferenceCaches() {
// There are components in the Eclipse workbench that maintain soft references to objects for
- // performance caches. For example, the the Common Navigator Framework used by Model Explorer
+ // performance caches. For example, the the Common Navigator Framework used by Model Explorer
// caches mappings of elements in the tree to the content extensions that provided them using
// EvalutationReferences [sic] that are SoftReferences
// This is a really gross HACK and runs the risk that some other thread(s) also may see OOMEs!
try {
List<Object[]> hog = Lists.newLinkedList();
- for(;;) {
+ for (;;) {
hog.add(new Object[getLargeMemorySize()]);
}
} catch (OutOfMemoryError e) {
- // Good! The JVM guarantees that all soft references are cleared before throwing OOME,
+ // Good! The JVM guarantees that all soft references are cleared before throwing OOME,
// so we can be assured that they are now cleared
} finally {
- if(warmingUp) {
+ if (warmingUp) {
// We have successfully warmed up the soft-references hack
WARMED_UP_SUITES.put(testClass, true);
}
@@ -257,7 +259,7 @@ public class MemoryLeakRule extends TestWatcher {
// always fail, so run such a test once up-front. Call this a metahack
try {
- System.err.printf("[MEM] Warming up test suite: %s (%s)%n", testClass.getName(), testName);
+ debug("Warming up test suite: %s (%s)%n", testClass.getName(), testName);
new JUnitCore().run(Request.method(testClass, testName));
} catch (Exception e) {
// Fine, so the warm-up didn't work
@@ -265,6 +267,12 @@ public class MemoryLeakRule extends TestWatcher {
}
}
+ private static void debug(String format, Object... args) {
+ if (DEBUG) {
+ System.err.printf("[MEM] " + format, args);
+ }
+ }
+
//
// Nested types
//

Back to the top