LUCENE-3437: Detect the test thread by reference, not by name.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1171082 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2011-09-15 12:52:13 +00:00
parent 0d45fb20da
commit 4d6e8ad0d3
2 changed files with 37 additions and 4 deletions

View File

@ -64,9 +64,11 @@ import org.apache.lucene.store.MockDirectoryWrapper;
import org.apache.lucene.store.MockDirectoryWrapper.Throttling; import org.apache.lucene.store.MockDirectoryWrapper.Throttling;
import org.apache.lucene.util.FieldCacheSanityChecker.Insanity; import org.apache.lucene.util.FieldCacheSanityChecker.Insanity;
import org.junit.*; import org.junit.*;
import org.junit.rules.MethodRule;
import org.junit.rules.TestWatchman; import org.junit.rules.TestWatchman;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
/** /**
* Base class for all Lucene unit tests, Junit3 or Junit4 variant. * Base class for all Lucene unit tests, Junit3 or Junit4 variant.
@ -591,6 +593,29 @@ public abstract class LuceneTestCase extends Assert {
super.starting(method); super.starting(method);
} }
}; };
/**
* The thread executing the current test case.
* @see #isTestThread()
*/
volatile Thread testCaseThread;
/** @see #testCaseThread */
@Rule
public final MethodRule setTestThread = new MethodRule() {
public Statement apply(final Statement s, FrameworkMethod fm, Object target) {
return new Statement() {
public void evaluate() throws Throwable {
try {
LuceneTestCase.this.testCaseThread = Thread.currentThread();
s.evaluate();
} finally {
LuceneTestCase.this.testCaseThread = null;
}
}
};
}
};
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -635,6 +660,15 @@ public abstract class LuceneTestCase extends Assert {
return getClass().getName() + "." + getName(); return getClass().getName() + "." + getName();
} }
/**
* Returns true if and only if the calling thread is the primary thread
* executing the test case.
*/
protected boolean isTestThread() {
assertNotNull("Test case thread not set?", testCaseThread);
return Thread.currentThread() == testCaseThread;
}
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
State oldState = state; // capture test execution state State oldState = state; // capture test execution state
@ -1373,7 +1407,7 @@ public abstract class LuceneTestCase extends Assert {
static final Random seedRand = new Random(); static final Random seedRand = new Random();
protected static final SmartRandom random = new SmartRandom(0); protected static final SmartRandom random = new SmartRandom(0);
private String name = "<unknown>"; private String name = "<unknown>";
/** /**

View File

@ -30,7 +30,7 @@ import org.apache.lucene.util.LuceneTestCase;
public class TestConcurrentMergeScheduler extends LuceneTestCase { public class TestConcurrentMergeScheduler extends LuceneTestCase {
private static class FailOnlyOnFlush extends MockDirectoryWrapper.Failure { private class FailOnlyOnFlush extends MockDirectoryWrapper.Failure {
boolean doFail; boolean doFail;
boolean hitExc; boolean hitExc;
@ -46,8 +46,7 @@ public class TestConcurrentMergeScheduler extends LuceneTestCase {
@Override @Override
public void eval(MockDirectoryWrapper dir) throws IOException { public void eval(MockDirectoryWrapper dir) throws IOException {
if (doFail && (Thread.currentThread().getName().equals("main") if (doFail && isTestThread()) {
|| Thread.currentThread().getName().equals("Main Thread"))) {
boolean isDoFlush = false; boolean isDoFlush = false;
boolean isClose = false; boolean isClose = false;
StackTraceElement[] trace = new Exception().getStackTrace(); StackTraceElement[] trace = new Exception().getStackTrace();