You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2012/09/12 10:22:01 UTC
svn commit: r1383842 - in /lucene/dev/branches/branch_4x/lucene: CHANGES.txt
test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java
Author: dweiss
Date: Wed Sep 12 08:22:01 2012
New Revision: 1383842
URL: http://svn.apache.org/viewvc?rev=1383842&view=rev
Log:
LUCENE-4252: Detect/Fail tests when they leak RAM in static fields.
Modified:
lucene/dev/branches/branch_4x/lucene/CHANGES.txt
lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java
Modified: lucene/dev/branches/branch_4x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/CHANGES.txt?rev=1383842&r1=1383841&r2=1383842&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/lucene/CHANGES.txt Wed Sep 12 08:22:01 2012
@@ -155,6 +155,9 @@ Optimizations
Build
+* LUCENE-4252: Detect/Fail tests when they leak RAM in static fields
+ (Robert Muir, Dawid Weiss)
+
* LUCENE-4360: Support running the same test suite multiple times in
parallel (Dawid Weiss)
Modified: lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java?rev=1383842&r1=1383841&r2=1383842&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java Wed Sep 12 08:22:01 2012
@@ -53,6 +53,7 @@ import com.carrotsearch.randomizedtestin
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.carrotsearch.randomizedtesting.rules.NoClassHooksShadowingRule;
import com.carrotsearch.randomizedtesting.rules.NoInstanceHooksOverridesRule;
+import com.carrotsearch.randomizedtesting.rules.StaticFieldsInvariantRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesInvariantRule;
import static com.carrotsearch.randomizedtesting.RandomizedTest.systemPropertyAsBoolean;
@@ -366,6 +367,18 @@ public abstract class LuceneTestCase ext
}
/**
+ * Max 10mb of static data stored in a test suite class after the suite is complete.
+ * Prevents static data structures leaking and causing OOMs in subsequent tests.
+ */
+ private final static long STATIC_LEAK_THRESHOLD = 10 * 1024 * 1024;
+
+ /** By-name list of ignored types like loggers etc. */
+ private final static Set<String> STATIC_LEAK_IGNORED_TYPES =
+ Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+ "org.slf4j.Logger",
+ "org.apache.solr.SolrLogFormatter")));
+
+ /**
* This controls how suite-level rules are nested. It is important that _all_ rules declared
* in {@link LuceneTestCase} are executed in proper order if they depend on each
* other.
@@ -376,6 +389,14 @@ public abstract class LuceneTestCase ext
.around(ignoreAfterMaxFailures)
.around(suiteFailureMarker)
.around(new TestRuleAssertionsRequired())
+ .around(new StaticFieldsInvariantRule(STATIC_LEAK_THRESHOLD, true) {
+ protected boolean accept(java.lang.reflect.Field field) {
+ if (STATIC_LEAK_IGNORED_TYPES.contains(field.getType().getName())) {
+ return false;
+ }
+ return super.accept(field);
+ }
+ })
.around(new NoClassHooksShadowingRule())
.around(new NoInstanceHooksOverridesRule() {
@Override
Modified: lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java?rev=1383842&r1=1383841&r2=1383842&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java (original)
+++ lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleAssertionsRequired.java Wed Sep 12 08:22:01 2012
@@ -32,7 +32,10 @@ public class TestRuleAssertionsRequired
public void evaluate() throws Throwable {
try {
assert false;
- throw new Exception("Test class requires assertions, enable assertions globally (-ea) or for Solr/Lucene subpackages only.");
+ String msg = "Test class requires enabled assertions, enable globally (-ea)" +
+ " or for Solr/Lucene subpackages only: " + description.getClassName();
+ System.err.println(msg);
+ throw new Exception(msg);
} catch (AssertionError e) {
// Ok, enabled.
}