From ccccb0b377f4ecd95297412a2d9b4be1e04ba15a Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 2 May 2014 02:12:16 +0000 Subject: [PATCH] improve block document exception testing git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1591804 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/index/TestIndexWriter.java | 34 +++++++++++ .../index/TestIndexWriterExceptions2.java | 60 ++++++++++++++----- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java index 962661c6e5f..bc6c6d0cc2a 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2187,6 +2187,40 @@ public class TestIndexWriter extends LuceneTestCase { w.shutdown(); IOUtils.close(reader, dir); } + + @AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-5611") + public void testIterableThrowsException2() throws IOException { + Directory dir = newDirectory(); + IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + try { + w.addDocuments(new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + + @Override + public boolean hasNext() { + return true; + } + + @Override + public Document next() { + throw new RuntimeException("boom"); + } + + @Override + public void remove() { assert false; } + }; + } + }); + } catch (Exception e) { + assertNotNull(e.getMessage()); + assertEquals("boom", e.getMessage()); + } + w.shutdown(); + IOUtils.close(dir); + } private static class RandomFailingFieldIterable implements Iterable { private final List docList; diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java index c602cfec8b5..658f8b48d3c 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java @@ -19,6 +19,7 @@ package org.apache.lucene.index; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.Arrays; import java.util.Random; import org.apache.lucene.analysis.Analyzer; @@ -119,25 +120,52 @@ public class TestIndexWriterExceptions2 extends LuceneTestCase { ft.setStoreTermVectors(true); doc.add(newField("text_vectors", TestUtil.randomAnalysisString(random(), 6, true), ft)); - try { - iw.addDocument(doc); - // we made it, sometimes delete our doc, or update a dv - int thingToDo = random().nextInt(4); - if (thingToDo == 0) { - iw.deleteDocuments(new Term("id", Integer.toString(i))); - } else if (thingToDo == 1 && defaultCodecSupportsFieldUpdates()) { - iw.updateNumericDocValue(new Term("id", Integer.toString(i)), "dv", i+1L); - } else if (thingToDo == 2 && defaultCodecSupportsFieldUpdates()) { - iw.updateBinaryDocValue(new Term("id", Integer.toString(i)), "dv2", new BytesRef(Integer.toString(i+1))); + if (random().nextInt(10) > 0) { + // single doc + try { + iw.addDocument(doc); + // we made it, sometimes delete our doc, or update a dv + int thingToDo = random().nextInt(4); + if (thingToDo == 0) { + iw.deleteDocuments(new Term("id", Integer.toString(i))); + } else if (thingToDo == 1 && defaultCodecSupportsFieldUpdates()) { + iw.updateNumericDocValue(new Term("id", Integer.toString(i)), "dv", i+1L); + } else if (thingToDo == 2 && defaultCodecSupportsFieldUpdates()) { + iw.updateBinaryDocValue(new Term("id", Integer.toString(i)), "dv2", new BytesRef(Integer.toString(i+1))); + } + } catch (Exception e) { + if (e.getMessage() != null && e.getMessage().startsWith("Fake IOException")) { + exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); + e.printStackTrace(exceptionStream); + } else { + Rethrow.rethrow(e); + } } - } catch (Exception e) { - if (e.getMessage() != null && e.getMessage().startsWith("Fake IOException")) { - exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); - e.printStackTrace(exceptionStream); - } else { - Rethrow.rethrow(e); + } else { + // block docs + Document doc2 = new Document(); + doc2.add(newStringField("id", Integer.toString(-i), Field.Store.NO)); + doc2.add(newTextField("text1", TestUtil.randomAnalysisString(random(), 20, true), Field.Store.NO)); + doc2.add(new StoredField("stored1", "foo")); + doc2.add(new StoredField("stored1", "bar")); + doc2.add(newField("text_vectors", TestUtil.randomAnalysisString(random(), 6, true), ft)); + + try { + iw.addDocuments(Arrays.asList(doc, doc2)); + // we made it, sometimes delete our docs + if (random().nextBoolean()) { + iw.deleteDocuments(new Term("id", Integer.toString(i)), new Term("id", Integer.toString(-i))); + } + } catch (Exception e) { + if (e.getMessage() != null && e.getMessage().startsWith("Fake IOException")) { + exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); + e.printStackTrace(exceptionStream); + } else { + Rethrow.rethrow(e); + } } } + if (random().nextInt(10) == 0) { // trigger flush: try {