public virtual void TestAddDocumentOnDiskFull() { bool debug = false; for (int pass = 0; pass < 3; pass++) { if (debug) System.Console.Out.WriteLine("TEST: pass="******"TEST: cycle: diskFree=" + diskFree); MockRAMDirectory dir = new MockRAMDirectory(); dir.SetMaxSizeInBytes(diskFree); IndexWriter writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true); MergeScheduler ms = writer.GetMergeScheduler(); if (ms is ConcurrentMergeScheduler) // This test intentionally produces exceptions // in the threads that CMS launches; we don't // want to pollute test output with these. ((ConcurrentMergeScheduler)ms).SetSuppressExceptions_ForNUnitTest(); bool hitError = false; try { for (int i = 0; i < 200; i++) { AddDoc(writer); } } catch (System.IO.IOException e) { if (debug) { System.Console.Out.WriteLine("TEST: exception on addDoc"); System.Console.Out.WriteLine(e.StackTrace); } hitError = true; } if (hitError) { if (doAbort) { writer.Abort(); } else { try { writer.Close(); } catch (System.IO.IOException e) { if (debug) { System.Console.Out.WriteLine("TEST: exception on close"); System.Console.Out.WriteLine(e.StackTrace); } dir.SetMaxSizeInBytes(0); writer.Close(); } } _TestUtil.SyncConcurrentMerges(ms); AssertNoUnreferencedFiles(dir, "after disk full during addDocument with autoCommit=" + autoCommit); // Make sure reader can open the index: IndexReader.Open(dir).Close(); dir.Close(); // Now try again w/ more space: diskFree += 500; } else { _TestUtil.SyncConcurrentMerges(writer); dir.Close(); break; } } } }
public virtual void TestCommitOnCloseAbort() { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); writer.SetMaxBufferedDocs(10); for (int i = 0; i < 14; i++) { AddDoc(writer); } writer.Close(); Term searchTerm = new Term("content", "aaa"); IndexSearcher searcher = new IndexSearcher(dir); Hits hits = searcher.Search(new TermQuery(searchTerm)); Assert.AreEqual(14, hits.Length(), "first number of hits"); searcher.Close(); writer = new IndexWriter(dir, false, new WhitespaceAnalyzer(), false); writer.SetMaxBufferedDocs(10); for (int j = 0; j < 17; j++) { AddDoc(writer); } // Delete all docs: writer.DeleteDocuments(searchTerm); searcher = new IndexSearcher(dir); hits = searcher.Search(new TermQuery(searchTerm)); Assert.AreEqual(14, hits.Length(), "reader incorrectly sees changes from writer with autoCommit disabled"); searcher.Close(); // Now, close the writer: writer.Abort(); AssertNoUnreferencedFiles(dir, "unreferenced files remain after abort()"); searcher = new IndexSearcher(dir); hits = searcher.Search(new TermQuery(searchTerm)); Assert.AreEqual(14, hits.Length(), "saw changes after writer.abort"); searcher.Close(); // Now make sure we can re-open the index, add docs, // and all is good: writer = new IndexWriter(dir, false, new WhitespaceAnalyzer(), false); writer.SetMaxBufferedDocs(10); for (int i = 0; i < 12; i++) { for (int j = 0; j < 17; j++) { AddDoc(writer); } searcher = new IndexSearcher(dir); hits = searcher.Search(new TermQuery(searchTerm)); Assert.AreEqual(14, hits.Length(), "reader incorrectly sees changes from writer with autoCommit disabled"); searcher.Close(); } writer.Close(); searcher = new IndexSearcher(dir); hits = searcher.Search(new TermQuery(searchTerm)); Assert.AreEqual(218, hits.Length(), "didn't see changes after close"); searcher.Close(); dir.Close(); }
public virtual void TestCommitOnCloseOptimize() { RAMDirectory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); writer.SetMaxBufferedDocs(10); for (int j = 0; j < 17; j++) { AddDocWithIndex(writer, j); } writer.Close(); writer = new IndexWriter(dir, false, new WhitespaceAnalyzer(), false); writer.Optimize(); // Open a reader before closing (commiting) the writer: IndexReader reader = IndexReader.Open(dir); // Reader should see index as unoptimized at this // point: Assert.IsFalse(reader.IsOptimized(), "Reader incorrectly sees that the index is optimized"); reader.Close(); // Abort the writer: writer.Abort(); AssertNoUnreferencedFiles(dir, "aborted writer after optimize"); // Open a reader after aborting writer: reader = IndexReader.Open(dir); // Reader should still see index as unoptimized: Assert.IsFalse(reader.IsOptimized(), "Reader incorrectly sees that the index is optimized"); reader.Close(); writer = new IndexWriter(dir, false, new WhitespaceAnalyzer(), false); writer.Optimize(); writer.Close(); AssertNoUnreferencedFiles(dir, "aborted writer after optimize"); // Open a reader after aborting writer: reader = IndexReader.Open(dir); // Reader should still see index as unoptimized: Assert.IsTrue(reader.IsOptimized(), "Reader incorrectly sees that the index is unoptimized"); reader.Close(); }
public virtual void TestExceptionsDuringCommit() { MockRAMDirectory dir = new MockRAMDirectory(); FailOnlyInCommit failure = new FailOnlyInCommit(); IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.Add(new Field("field", "a field", Field.Store.YES, Field.Index.ANALYZED)); w.AddDocument(doc); dir.FailOn(failure); try { w.Close(); Assert.Fail(); } catch (System.IO.IOException ioe) { Assert.Fail("expected only RuntimeException"); } catch (System.SystemException re) { // Expected } Assert.IsTrue(failure.fail1 && failure.fail2); w.Abort(); dir.Close(); }