Apache Solr open-source search software
Fix for SOLR-2975, fix tests that failed occasionally
Erick Erickson2208b499fa47
diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/MockSolrEntityProcessor.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/MockSolrEntityProcessor.javaindex 675fff017aa..d0dcb20a617 100644--- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/MockSolrEntityProcessor.java+++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/MockSolrEntityProcessor.java@@ -17,42 +17,47 @@ package org.apache.solr.handler.dataimport;* limitations under the License.*/+import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;+import java.util.List;+public class MockSolrEntityProcessor extends SolrEntityProcessor {-- private final String[][][] docsData;- private final int rows;++ private final List<SolrTestCaseJ4.Doc> docsData;+// private final int rows;private int queryCount = 0;-- public MockSolrEntityProcessor(String[][][] docsData) {++ private int rows;++ public MockSolrEntityProcessor(List<SolrTestCaseJ4.Doc> docsData) {this(docsData, ROWS_DEFAULT);}-- public MockSolrEntityProcessor(String[][][] docsData, int rows) {++ public MockSolrEntityProcessor(List<SolrTestCaseJ4.Doc> docsData, int rows) {this.docsData = docsData;this.rows = rows;}-+@Overrideprotected SolrDocumentList doQuery(int start) {queryCount++;return getDocs(start, rows);}-+private SolrDocumentList getDocs(int start, int rows) {SolrDocumentList docs = new SolrDocumentList();- docs.setNumFound(docsData.length);+ docs.setNumFound(docsData.size());docs.setStart(start);-+int endIndex = start + rows;- int end = docsData.length < endIndex ? docsData.length : endIndex;+ int end = docsData.size() < endIndex ? docsData.size() : endIndex;for (int i = start; i < end; i++) {SolrDocument doc = new SolrDocument();- for (String[] fields : docsData[i]) {- doc.addField(fields[0], fields[1]);- }+ SolrTestCaseJ4.Doc testDoc = docsData.get(i);+ doc.addField("id", testDoc.id);+ doc.addField("description", testDoc.getValues("description"));docs.add(doc);}return docs;diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorUnit.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorUnit.javaindex 57423e91038..4ebd68aa460 100644--- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorUnit.java+++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorUnit.java@@ -16,14 +16,12 @@*/package org.apache.solr.handler.dataimport;-import java.util.Arrays;-import java.util.HashMap;-import java.util.Map;-import java.util.Map.Entry;+import java.util.*;import java.util.concurrent.CountDownLatch;import java.util.concurrent.SynchronousQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicInteger;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -32,118 +30,126 @@ import org.slf4j.LoggerFactory;* Unit test of SolrEntityProcessor. A very basic test outside of the DIH.*/public class TestSolrEntityProcessorUnit extends AbstractDataImportHandlerTestCase {-+private static final Logger LOG = LoggerFactory.getLogger(TestSolrEntityProcessorUnit.class);private static final String ID = "id";-+public void testQuery() {- String[][][] docs = generateDocs(2);-+ List<Doc> docs = generateUniqueDocs(2);+MockSolrEntityProcessor processor = new MockSolrEntityProcessor(docs);-+assertExpectedDocs(docs, processor);assertEquals(1, processor.getQueryCount());}-+public void testNumDocsGreaterThanRows() {- String[][][] docs = generateDocs(44);-+ List<Doc> docs = generateUniqueDocs(44);+MockSolrEntityProcessor processor = new MockSolrEntityProcessor(docs, 10);assertExpectedDocs(docs, processor);assertEquals(5, processor.getQueryCount());}-+public void testMultiValuedFields() {- String[][][] docs = new String[1][2][2];- String[][] doc = new String[][] { {"id", "1"}, {"multi", "multi1"},- {"multi", "multi2"}, {"multi", "multi3"}};- docs[0] = doc;-+ List<Doc> docs = new ArrayList<Doc>();+ List<FldType> types = new ArrayList<FldType>();+ types.add(new FldType(ID, ONE_ONE, new SVal('A', 'Z', 4, 4)));+ types.add(new FldType("description", new IRange(3, 3), new SVal('a', 'c', 1, 1)));+ Doc testDoc = createDoc(types);+ docs.add(testDoc);+MockSolrEntityProcessor processor = new MockSolrEntityProcessor(docs);-- Map<String,Object> next = processor.nextRow();+ Map<String, Object> next = processor.nextRow();assertNotNull(next);- assertEquals(doc[0][1], next.get(doc[0][0]));-- String[] multiValued = {"multi1", "multi2", "multi3"};- assertEquals(Arrays.asList(multiValued), next.get(doc[1][0]));++ @SuppressWarnings("unchecked")+ List<Comparable> multiField = (List<Comparable>) next.get("description");+ assertEquals(testDoc.getValues("description").size(), multiField.size());+ assertEquals(testDoc.getValues("description"), multiField);assertEquals(1, processor.getQueryCount());assertNull(processor.nextRow());-}-- public void testMultiThread() {++ public void testMultiThread() throws Exception {int numThreads = 5;int numDocs = 40;- String[][][] docs = generateDocs(numDocs);+ List<Doc> docs = generateUniqueDocs(numDocs);final MockSolrEntityProcessor entityProcessor = new MockSolrEntityProcessor(docs, 25);-- final Map<String,Map<String,Object>> rowList = new HashMap<String,Map<String,Object>>();+ ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS,+ new SynchronousQueue<Runnable>());++ final Map<String, Map<String, Object>> rowList = new LinkedHashMap<String, Map<String, Object>>();final CountDownLatch latch = new CountDownLatch(numThreads);+ final AtomicInteger errorCount = new AtomicInteger();for (int i = 0; i < numThreads; i++) {Runnable runnable = new Runnable() {public void run() {try {while (true) {- Map<String,Object> row;synchronized (entityProcessor) {- row = entityProcessor.nextRow();+ Map<String, Object> row = entityProcessor.nextRow();+ if (row == null) {+ break;+ }+ rowList.put(row.get(ID).toString(), row);}- if (row == null) {- break;- }- rowList.put(row.get(ID).toString(), row);}+ } catch (Throwable t) {+ errorCount.incrementAndGet();+ LOG.error("Error in thread", t);} finally {latch.countDown();}}};-- new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS,- new SynchronousQueue<Runnable>()).execute(runnable);- }-- try {- latch.await();- } catch (InterruptedException e) {- LOG.error(e.getMessage(), e);+ executor.execute(runnable);}-++ latch.await();+ assertEquals(0, errorCount.get());assertEquals(numDocs, rowList.size());-- for (String[][] expectedDoc : docs) {- Map<String,Object> row = rowList.get(expectedDoc[0][1]);- assertNotNull(row);- int i = 0;- for (Entry<String,Object> entry : row.entrySet()) {- assertEquals(expectedDoc[i][0], entry.getKey());- assertEquals(expectedDoc[i][1], entry.getValue());- i++;- }- rowList.remove(expectedDoc[0][1]);++ for (Doc expectedDoc : docs) {+ String id = (String) expectedDoc.getFirstValue("id");+ Map<String, Object> row = rowList.get(id);+ assertNotNull(id + " shouldn't yield null", row);+ assertEquals(2, row.size());+ assertEquals(expectedDoc.id, row.get("id"));+ assertEquals(expectedDoc.getValues("description"), row.get("description"));+ rowList.remove(id);}-+assertEquals(0, rowList.size());-+ executor.shutdown();}-- private static String[][][] generateDocs(int numDocs) {- String[][][] docs = new String[numDocs][2][2];++ private List<Doc> generateUniqueDocs(int numDocs) {+ List<FldType> types = new ArrayList<FldType>();+ types.add(new FldType(ID, ONE_ONE, new SVal('A', 'Z', 4, 40)));+ types.add(new FldType("description", new IRange(1, 3), new SVal('a', 'c', 1, 1)));++ Set<Comparable> previousIds = new HashSet<Comparable>();+ List<Doc> docs = new ArrayList<Doc>(numDocs);for (int i = 0; i < numDocs; i++) {- docs[i] = new String[][] { {"id", Integer.toString(i+1)},- {"description", "Description" + Integer.toString(i+1)}};+ Doc doc = createDoc(types);+ while (previousIds.contains(doc.id)) {+ doc = createDoc(types);+ }+ previousIds.add(doc.id);+ docs.add(doc);}return docs;}-- private static void assertExpectedDocs(String[][][] expectedDocs, SolrEntityProcessor processor) {- for (String[][] expectedDoc : expectedDocs) {++ private static void assertExpectedDocs(List<Doc> expectedDocs, SolrEntityProcessor processor) {+ for (Doc expectedDoc : expectedDocs) {Map<String, Object> next = processor.nextRow();assertNotNull(next);- assertEquals(expectedDoc[0][1], next.get(expectedDoc[0][0]));- assertEquals(expectedDoc[1][1], next.get(expectedDoc[1][0]));+ assertEquals(expectedDoc.id, next.get("id"));+ assertEquals(expectedDoc.getValues("description"), next.get("description"));}assertNull(processor.nextRow());}+}diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.javaindex aaa1fb8851e..1851375902b 100755--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java@@ -745,7 +745,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {public static final IRange ZERO_TWO = new IRange(0,2);public static final IRange ONE_ONE = new IRange(1,1);- public static class Doc implements Comparable{+ public static class Doc implements Comparable {public Comparable id;public List<Fld> fields;public int order; // the order this document was added to the index@@ -811,7 +811,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {}}- class FldType {+ protected class FldType {public String fname;public IRange numValues;public Vals vals;