LUCENE-3463: Add @UseNoMemoryExpensiveCodec annotation

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1179210 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-10-05 13:16:22 +00:00
parent 86124e2667
commit 9ae47eaa35
7 changed files with 58 additions and 15 deletions

View File

@ -48,7 +48,7 @@ public class RandomCodecProvider extends CodecProvider {
private Map<String,Codec> previousMappings = new HashMap<String,Codec>(); private Map<String,Codec> previousMappings = new HashMap<String,Codec>();
private final int perFieldSeed; private final int perFieldSeed;
public RandomCodecProvider(Random random) { public RandomCodecProvider(Random random, boolean useNoMemoryExpensiveCodec) {
this.perFieldSeed = random.nextInt(); this.perFieldSeed = random.nextInt();
// TODO: make it possible to specify min/max iterms per // TODO: make it possible to specify min/max iterms per
// block via CL: // block via CL:
@ -61,8 +61,10 @@ public class RandomCodecProvider extends CodecProvider {
minItemsPerBlock = _TestUtil.nextInt(random, 2, 100); minItemsPerBlock = _TestUtil.nextInt(random, 2, 100);
maxItemsPerBlock = 2*(Math.max(1, minItemsPerBlock-1)) + random.nextInt(100); maxItemsPerBlock = 2*(Math.max(1, minItemsPerBlock-1)) + random.nextInt(100);
register(new PulsingCodec( 1 + random.nextInt(20), minItemsPerBlock, maxItemsPerBlock)); register(new PulsingCodec( 1 + random.nextInt(20), minItemsPerBlock, maxItemsPerBlock));
register(new SimpleTextCodec()); if (!useNoMemoryExpensiveCodec) {
register(new MemoryCodec()); register(new SimpleTextCodec());
register(new MemoryCodec());
}
Collections.shuffle(knownCodecs, random); Collections.shuffle(knownCodecs, random);
} }

View File

@ -21,9 +21,11 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited; import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -346,7 +348,7 @@ public abstract class LuceneTestCase extends Assert {
if (random.nextInt(4) == 0) { // preflex-only setup if (random.nextInt(4) == 0) { // preflex-only setup
codec = installTestCodecs("PreFlex", CodecProvider.getDefault()); codec = installTestCodecs("PreFlex", CodecProvider.getDefault());
} else { // per-field setup } else { // per-field setup
CodecProvider.setDefault(new RandomCodecProvider(random)); CodecProvider.setDefault(new RandomCodecProvider(random, useNoMemoryExpensiveCodec));
codec = installTestCodecs(TEST_CODEC, CodecProvider.getDefault()); codec = installTestCodecs(TEST_CODEC, CodecProvider.getDefault());
} }
} else { // ordinary setup } else { // ordinary setup
@ -636,6 +638,17 @@ public abstract class LuceneTestCase extends Assert {
if (!testsFailed) { if (!testsFailed) {
assertTrue("ensure your tearDown() calls super.tearDown()!!!", (s == State.INITIAL || s == State.TEARDOWN)); assertTrue("ensure your tearDown() calls super.tearDown()!!!", (s == State.INITIAL || s == State.TEARDOWN));
} }
if (useNoMemoryExpensiveCodec) {
final String defCodec = CodecProvider.getDefault().getDefaultFieldCodec();
// Stupid: assumeFalse in setUp() does not print any information, because
// TestWatchman does not watch test during setUp() - getName() is also not defined...
// => print info directly and use assume without message:
if ("SimpleText".equals(defCodec) || "Memory".equals(defCodec)) {
System.err.println("NOTE: A test method in " + getClass().getSimpleName() + " was ignored, as it uses too much memory with " + defCodec + ".");
Assume.assumeTrue(false);
}
}
} }
/** /**
@ -1397,6 +1410,9 @@ public abstract class LuceneTestCase extends Assert {
return context; return context;
} }
// initialized by the TestRunner
static boolean useNoMemoryExpensiveCodec;
// recorded seed: for beforeClass // recorded seed: for beforeClass
private static long staticSeed; private static long staticSeed;
// seed for individual test methods, changed in @before // seed for individual test methods, changed in @before
@ -1415,6 +1431,15 @@ public abstract class LuceneTestCase extends Assert {
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Nightly {} public @interface Nightly {}
/**
* Annotation for test classes that should only use codecs that are not memory expensive (avoid SimpleText, MemoryCodec).
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface UseNoMemoryExpensiveCodec {}
@Ignore("just a hack") @Ignore("just a hack")
public final void alwaysIgnoredTestMethod() {} public final void alwaysIgnoredTestMethod() {}
} }

View File

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import org.apache.lucene.util.LuceneTestCase.Nightly; import org.apache.lucene.util.LuceneTestCase.Nightly;
import org.apache.lucene.util.LuceneTestCase.UseNoMemoryExpensiveCodec;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.Description; import org.junit.runner.Description;
@ -156,6 +157,19 @@ public class LuceneTestCaseRunner extends BlockJUnit4ClassRunner {
public LuceneTestCaseRunner(Class<?> clazz) throws InitializationError { public LuceneTestCaseRunner(Class<?> clazz) throws InitializationError {
super(clazz); super(clazz);
// This TestRunner can handle only LuceneTestCase subclasses
if (!LuceneTestCase.class.isAssignableFrom(clazz)) {
throw new UnsupportedOperationException("LuceneTestCaseRunner can only be used with LuceneTestCase.");
}
final boolean useNoMemoryExpensiveCodec = LuceneTestCase.useNoMemoryExpensiveCodec =
clazz.isAnnotationPresent(UseNoMemoryExpensiveCodec.class);
if (useNoMemoryExpensiveCodec) {
System.err.println("NOTE: Using no memory expensive codecs (Memory, SimpleText) for " +
clazz.getSimpleName() + ".");
}
// evil we cannot init our random here, because super() calls computeTestMethods!!!!; // evil we cannot init our random here, because super() calls computeTestMethods!!!!;
Filter f = new Filter() { Filter f = new Filter() {

View File

@ -33,9 +33,11 @@ import org.apache.lucene.index.codecs.CodecProvider;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.UseNoMemoryExpensiveCodec;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util._TestUtil; import org.apache.lucene.util._TestUtil;
@UseNoMemoryExpensiveCodec
public class TestLongPostings extends LuceneTestCase { public class TestLongPostings extends LuceneTestCase {
// Produces a realistic unicode random string that // Produces a realistic unicode random string that
@ -76,9 +78,6 @@ public class TestLongPostings extends LuceneTestCase {
} }
public void testLongPostings() throws Exception { public void testLongPostings() throws Exception {
assumeFalse("Too slow with SimpleText codec at night", TEST_NIGHTLY && CodecProvider.getDefault().getFieldCodec("field").equals("SimpleText"));
assumeFalse("Too slow with Memory codec at night", TEST_NIGHTLY && CodecProvider.getDefault().getFieldCodec("field").equals("Memory"));
// Don't use _TestUtil.getTempDir so that we own the // Don't use _TestUtil.getTempDir so that we own the
// randomness (ie same seed will point to same dir): // randomness (ie same seed will point to same dir):
Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong())); Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong()));
@ -271,8 +270,6 @@ public class TestLongPostings extends LuceneTestCase {
} }
public void doTestLongPostingsNoPositions(IndexOptions options) throws Exception { public void doTestLongPostingsNoPositions(IndexOptions options) throws Exception {
assumeFalse("Too slow with SimpleText codec at night", TEST_NIGHTLY && CodecProvider.getDefault().getFieldCodec("field").equals("SimpleText"));
assumeFalse("Too slow with Memory codec at night", TEST_NIGHTLY && CodecProvider.getDefault().getFieldCodec("field").equals("Memory"));
// Don't use _TestUtil.getTempDir so that we own the // Don't use _TestUtil.getTempDir so that we own the
// randomness (ie same seed will point to same dir): // randomness (ie same seed will point to same dir):
Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong())); Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong()));

View File

@ -40,12 +40,14 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LineFileDocs;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.UseNoMemoryExpensiveCodec;
import org.apache.lucene.util._TestUtil; import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.BasicAutomata; import org.apache.lucene.util.automaton.BasicAutomata;
import org.apache.lucene.util.automaton.CompiledAutomaton; import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.apache.lucene.util.automaton.DaciukMihovAutomatonBuilder; import org.apache.lucene.util.automaton.DaciukMihovAutomatonBuilder;
@UseNoMemoryExpensiveCodec
public class TestTermsEnum extends LuceneTestCase { public class TestTermsEnum extends LuceneTestCase {
public void test() throws Exception { public void test() throws Exception {

View File

@ -19,6 +19,7 @@ package org.apache.lucene.util.automaton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -30,12 +31,12 @@ import org.apache.lucene.util._TestUtil;
public class TestCompiledAutomaton extends LuceneTestCase { public class TestCompiledAutomaton extends LuceneTestCase {
private CompiledAutomaton build(String... strings) { private CompiledAutomaton build(String... strings) {
final List<Automaton> as = new ArrayList<Automaton>(); final List<BytesRef> terms = new ArrayList<BytesRef>();
for(String s : strings) { for(String s : strings) {
as.add(BasicAutomata.makeString(s)); terms.add(new BytesRef(s));
} }
Automaton a = BasicOperations.union(as); Collections.sort(terms);
a.determinize(); final Automaton a = DaciukMihovAutomatonBuilder.build(terms);
return new CompiledAutomaton(a, true, false); return new CompiledAutomaton(a, true, false);
} }
@ -93,7 +94,7 @@ public class TestCompiledAutomaton extends LuceneTestCase {
} }
public void testRandom() throws Exception { public void testRandom() throws Exception {
final int numTerms = atLeast(1000); final int numTerms = atLeast(400);
final Set<String> terms = new HashSet<String>(); final Set<String> terms = new HashSet<String>();
while(terms.size() != numTerms) { while(terms.size() != numTerms) {
terms.add(randomString()); terms.add(randomString());
@ -107,7 +108,7 @@ public class TestCompiledAutomaton extends LuceneTestCase {
} }
public void testBasic() throws Exception { public void testBasic() throws Exception {
CompiledAutomaton c = build("foo", "fob", "goo"); CompiledAutomaton c = build("fob", "foo", "goo");
testFloor(c, "goo", "goo"); testFloor(c, "goo", "goo");
testFloor(c, "ga", "foo"); testFloor(c, "ga", "foo");
testFloor(c, "g", "foo"); testFloor(c, "g", "foo");

View File

@ -53,10 +53,12 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IntsRef; import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LineFileDocs;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.UseNoMemoryExpensiveCodec;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util._TestUtil; import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.fst.FST.Arc; import org.apache.lucene.util.fst.FST.Arc;
@UseNoMemoryExpensiveCodec
public class TestFSTs extends LuceneTestCase { public class TestFSTs extends LuceneTestCase {
private MockDirectoryWrapper dir; private MockDirectoryWrapper dir;