You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2012/12/04 00:54:21 UTC

svn commit: r1416733 - /lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java

Author: mikemccand
Date: Mon Dec  3 23:54:20 2012
New Revision: 1416733

URL: http://svn.apache.org/viewvc?rev=1416733&view=rev
Log:
add test

Added:
    lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java   (with props)

Added: lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java?rev=1416733&view=auto
==============================================================================
--- lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java (added)
+++ lucene/dev/branches/lucene4547/lucene/core/src/test/org/apache/lucene/search/TestSameScoresWithThreads.java Mon Dec  3 23:54:20 2012
@@ -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<BytesRef,TopDocs> answers = new HashMap<BytesRef,TopDocs>();
+    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<numThreads;threadID++) {
+        Thread thread = new Thread() {
+            @Override
+            public void run() {
+              try {
+                startingGun.await();
+                for(int i=0;i<20;i++) {
+                  List<Map.Entry<BytesRef,TopDocs>> shuffled = new ArrayList<Map.Entry<BytesRef,TopDocs>>(answers.entrySet());
+                  Collections.shuffle(shuffled);
+                  for(Map.Entry<BytesRef,TopDocs> 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<expected.scoreDocs.length;hit++) {
+                      assertEquals(expected.scoreDocs[hit].doc, actual.scoreDocs[hit].doc);
+                      // Floats really should be identical:
+                      assertTrue(expected.scoreDocs[hit].score == actual.scoreDocs[hit].score);
+                    }
+                  }
+                }
+              } catch (Exception e) {
+                throw new RuntimeException(e);
+              }
+            }
+          };
+        threads[threadID] = thread;
+        thread.start();
+      }
+      startingGun.countDown();
+      for(Thread thread : threads) {
+        thread.join();
+      }
+      r.close();
+    }
+
+    dir.close();
+  }
+}