public virtual void TestCloseUnderException() { Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(Random(), TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); writer.Commit(); writer.Dispose(); DirectoryReader open = DirectoryReader.Open(dir); bool throwOnClose = !Rarely(); AtomicReader wrap = SlowCompositeReaderWrapper.Wrap(open); FilterAtomicReader reader = new FilterAtomicReaderAnonymousInnerClassHelper(this, wrap, throwOnClose); IList<IndexReader.ReaderClosedListener> listeners = new List<IndexReader.ReaderClosedListener>(); int listenerCount = Random().Next(20); AtomicInteger count = new AtomicInteger(); bool faultySet = false; for (int i = 0; i < listenerCount; i++) { if (Rarely()) { faultySet = true; reader.AddReaderClosedListener(new FaultyListener()); } else { count.IncrementAndGet(); reader.AddReaderClosedListener(new CountListener(count)); } } if (!faultySet && !throwOnClose) { reader.AddReaderClosedListener(new FaultyListener()); } try { reader.Dispose(); Assert.Fail("expected Exception"); } catch (InvalidOperationException ex) { if (throwOnClose) { Assert.AreEqual("BOOM!", ex.Message); } else { Assert.AreEqual("GRRRRRRRRRRRR!", ex.Message); } } try { var aaa = reader.Fields; Assert.Fail("we are closed"); } catch (AlreadyClosedException ex) { } if (Random().NextBoolean()) { reader.Dispose(); // call it again } Assert.AreEqual(0, count.Get()); wrap.Dispose(); dir.Dispose(); }
public virtual void TestApplyDeletesOnFlush() { Directory dir = NewDirectory(); // Cannot use RandomIndexWriter because we don't want to // ever call commit() for this test: AtomicInteger docsInSegment = new AtomicInteger(); AtomicBoolean closing = new AtomicBoolean(); AtomicBoolean sawAfterFlush = new AtomicBoolean(); IndexWriter w = new IndexWriterAnonymousInnerClassHelper(this, dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetRAMBufferSizeMB(0.5).SetMaxBufferedDocs(-1).SetMergePolicy(NoMergePolicy.NO_COMPOUND_FILES).SetReaderPooling(false), docsInSegment, closing, sawAfterFlush); int id = 0; while (true) { StringBuilder sb = new StringBuilder(); for (int termIDX = 0; termIDX < 100; termIDX++) { sb.Append(' ').Append(TestUtil.RandomRealisticUnicodeString(Random())); } if (id == 500) { w.DeleteDocuments(new Term("id", "0")); } Document doc = new Document(); doc.Add(NewStringField("id", "" + id, Field.Store.NO)); doc.Add(NewTextField("body", sb.ToString(), Field.Store.NO)); w.UpdateDocument(new Term("id", "" + id), doc); docsInSegment.IncrementAndGet(); // TODO: fix this test if (SlowFileExists(dir, "_0_1.del") || SlowFileExists(dir, "_0_1.liv")) { if (VERBOSE) { Console.WriteLine("TEST: deletes created @ id=" + id); } break; } id++; } closing.Set(true); Assert.IsTrue(sawAfterFlush.Get()); w.Dispose(); dir.Dispose(); }