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.util.FieldCacheSanityChecker.Insanity;
import org.junit.*;
import org.junit.rules.MethodRule;
import org.junit.rules.TestWatchman;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
/**
* Base class for all Lucene unit tests, Junit3 or Junit4 variant.
@ -592,6 +594,29 @@ public abstract class LuceneTestCase extends Assert {
}
};
/**
* 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
public void setUp() throws Exception {
seed = "random".equals(TEST_SEED) ? seedRand.nextLong() : ThreeLongs.fromString(TEST_SEED).l2;
@ -635,6 +660,15 @@ public abstract class LuceneTestCase extends Assert {
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
public void tearDown() throws Exception {
State oldState = state; // capture test execution state

View File

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