Index: lucene/core/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java =================================================================== --- lucene/core/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java (revision 1350558) +++ lucene/core/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java (working copy) @@ -19,6 +19,10 @@ import java.io.IOException; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; @@ -31,6 +35,7 @@ import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.ThreadInterruptedException; import org.apache.lucene.util._TestUtil; @@ -456,41 +461,102 @@ dir.close(); } - static class DelayedIndexAndCloseRunnable extends Thread { - private final Directory dir; - boolean failed = false; - Throwable failure = null; - private final CountDownLatch startIndexing = new CountDownLatch(1); - private CountDownLatch iwConstructed; + static class DelayedIndexAndCloseRunnable extends Thread { + private final Directory dir; + boolean failed = false; + Throwable failure = null; + private final CountDownLatch startIndexing = new CountDownLatch(1); + private CountDownLatch iwConstructed; - public DelayedIndexAndCloseRunnable(Directory dir, - CountDownLatch iwConstructed) { - this.dir = dir; - this.iwConstructed = iwConstructed; - } + public DelayedIndexAndCloseRunnable(Directory dir, + CountDownLatch iwConstructed) { + this.dir = dir; + this.iwConstructed = iwConstructed; + } - public void startIndexing() { - this.startIndexing.countDown(); - } + public void startIndexing() { + this.startIndexing.countDown(); + } - @Override - public void run() { - try { - Document doc = new Document(); - Field field = newTextField("field", "testData", Field.Store.YES); - doc.add(field); - IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( - TEST_VERSION_CURRENT, new MockAnalyzer(random()))); - iwConstructed.countDown(); - startIndexing.await(); - writer.addDocument(doc); - writer.close(); - } catch (Throwable e) { - failed = true; - failure = e; - failure.printStackTrace(System.out); - return; - } - } - } + @Override + public void run() { + try { + Document doc = new Document(); + Field field = newTextField("field", "testData", Field.Store.YES); + doc.add(field); + IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + iwConstructed.countDown(); + startIndexing.await(); + writer.addDocument(doc); + writer.close(); + } catch (Throwable e) { + failed = true; + failure = e; + failure.printStackTrace(System.out); + return; + } + } + } + + // LUCENE-4147 + public void testRollbackAndCommitWithThreads() throws Exception { + final Directory d = newFSDirectory(_TestUtil.getTempDir("RollbackAndCommitWithThreads")); + final int threadCount = _TestUtil.nextInt(random(), 2, 6); + + final AtomicReference writerRef = new AtomicReference(); + writerRef.set(new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())))); + final LineFileDocs docs = new LineFileDocs(random()); + final Thread[] threads = new Thread[threadCount]; + final int iters = atLeast(1000); + final AtomicBoolean failed = new AtomicBoolean(); + final Lock rollbackLock = new ReentrantLock(); + for(int threadID=0;threadID