From 1ef8f5e966bc008487bf6b17249b298f547f7299 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Mon, 3 Dec 2012 23:54:20 +0000 Subject: [PATCH] add test git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1416733 13f79535-47bb-0310-9956-ffa450edef68 --- .../search/TestSameScoresWithThreads.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java b/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java new file mode 100644 index 00000000000..900c1258934 --- /dev/null +++ b/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java @@ -0,0 +1,121 @@ +package org.apache.lucene.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.MultiFields; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LineFileDocs; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util._TestUtil; + +// nocommit make TestCodecHoldsOpenFiles, ie remove files +// after opening reader, then run check index (need to +// change CI to pass in atomic reader ... robert has patch +// LUCENE-4294) + +public class TestSameScoresWithThreads extends LuceneTestCase { + + public void test() throws Exception { + final Directory dir = newDirectory(); + final RandomIndexWriter w = new RandomIndexWriter(random(), dir); + LineFileDocs docs = new LineFileDocs(random()); + int bytesToIndex = atLeast(100000); + int bytesIndexed = 0; + while(bytesIndexed < bytesToIndex) { + Document doc = docs.nextDoc(); + bytesIndexed += RamUsageEstimator.sizeOf(doc); + w.addDocument(doc); + } + IndexReader r = w.getReader(); + w.close(); + + final IndexSearcher s = new IndexSearcher(r); + Terms terms = MultiFields.getFields(r).terms("body"); + long termCount = terms.size(); + assertTrue(termCount > 0); + + // Target ~10 terms to search: + double chance = 10.0 / termCount; + TermsEnum termsEnum = terms.iterator(null); + final Map answers = new HashMap(); + while(termsEnum.next() != null) { + if (random().nextDouble() <= chance) { + BytesRef term = BytesRef.deepCopyOf(termsEnum.term()); + answers.put(term, + s.search(new TermQuery(new Term("body", term)), 100)); + } + } + + if (!answers.isEmpty()) { + final CountDownLatch startingGun = new CountDownLatch(1); + int numThreads = _TestUtil.nextInt(random(), 2, 5); + Thread[] threads = new Thread[numThreads]; + for(int threadID=0;threadID> shuffled = new ArrayList>(answers.entrySet()); + Collections.shuffle(shuffled); + for(Map.Entry ent : shuffled) { + TopDocs actual = s.search(new TermQuery(new Term("body", ent.getKey())), 10); + TopDocs expected = ent.getValue(); + assertEquals(expected.totalHits, actual.totalHits); + assertEquals(expected.scoreDocs.length, actual.scoreDocs.length); + for(int hit=0;hit