Apache Solr open-source search software
AutoCommitTest: faster and less likely to fail due to slow hardware
Mike Klaas40a9f64a0ec8
diff --git a/src/java/org/apache/solr/update/DirectUpdateHandler2.java b/src/java/org/apache/solr/update/DirectUpdateHandler2.javaindex c82d9b37671..24d6fef14ca 100644--- a/src/java/org/apache/solr/update/DirectUpdateHandler2.java+++ b/src/java/org/apache/solr/update/DirectUpdateHandler2.java@@ -725,6 +725,9 @@ public class DirectUpdateHandler2 extends UpdateHandler {}}+ // to facilitate testing: blocks if called during commit+ public synchronized int getCommitCount() { return autoCommitCount; }+public String toString() {if(timeUpperBound > 0 || docsUpperBound > 0) {returndiff --git a/src/test/org/apache/solr/update/AutoCommitTest.java b/src/test/org/apache/solr/update/AutoCommitTest.javaindex 2ab6c4241b8..8b9a10f8a85 100644--- a/src/test/org/apache/solr/update/AutoCommitTest.java+++ b/src/test/org/apache/solr/update/AutoCommitTest.java@@ -24,16 +24,39 @@ import java.util.HashMap;import org.apache.solr.common.params.MapSolrParams;import org.apache.solr.common.util.ContentStream;import org.apache.solr.common.util.ContentStreamBase;-import org.apache.solr.core.SolrCore;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.core.*;+import org.apache.solr.search.*;import org.apache.solr.handler.XmlUpdateRequestHandler;import org.apache.solr.request.SolrQueryRequestBase;import org.apache.solr.request.SolrQueryResponse;import org.apache.solr.util.AbstractSolrTestCase;-/**- *++/** Catch commit notifications*+ * It is tricky to be correctly notified when commits occur: Solr's post-commit+ * hook is called after commit has completed but before the search is opened. The+ * best that can be done is wait for a post commit hook, then add a document (which+ * will block while the searcher is opened)*/+class CommitListener implements SolrEventListener {+ public boolean triggered = false;+ public void init(NamedList args) {}+ public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) {}+ public void postCommit() {+ triggered = true;+ }+ public boolean waitForCommit(int timeout) {+ triggered = false;+ for (int towait=timeout; towait > 0; towait -= 250) {+ if (triggered) break;+ try { Thread.sleep( 250 ); } catch (InterruptedException e) {}+ }+ return triggered;+ }+}+public class AutoCommitTest extends AbstractSolrTestCase {public String getSchemaFile() { return "schema.xml"; }@@ -54,11 +77,14 @@ public class AutoCommitTest extends AbstractSolrTestCase {}public void testMaxDocs() throws Exception {++ CommitListener trigger = new CommitListener();SolrCore core = h.getCore();DirectUpdateHandler2 updater = (DirectUpdateHandler2)core.getUpdateHandler();DirectUpdateHandler2.CommitTracker tracker = updater.tracker;tracker.timeUpperBound = 100000;tracker.docsUpperBound = 14;+ updater.commitCallbacks.add(trigger);XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler();handler.init( null );@@ -75,24 +101,24 @@ public class AutoCommitTest extends AbstractSolrTestCase {}// It should not be there right awayassertQ("shouldn't find any", req("id:A1") ,"//result[@numFound=0]" );- assertEquals( 0, tracker.autoCommitCount );+ assertEquals( 0, tracker.getCommitCount());req.setContentStreams( toContentStreams(adoc("id", "A14", "subject", "info" ), null ) );handler.handleRequest( req, rsp );// Wait longer than the autocommit time- Thread.sleep( 1000 );- // blocks until commit is complete+ assertTrue(trigger.waitForCommit(10000));+req.setContentStreams( toContentStreams(adoc("id", "A15", "subject", "info" ), null ) );handler.handleRequest( req, rsp );// Now make sure we can find itassertQ("should find one", req("id:A14") ,"//result[@numFound=1]" );- assertEquals( 1, tracker.autoCommitCount );+ assertEquals( 1, tracker.getCommitCount());// But not the one added afterwardassertQ("should find one", req("id:A15") ,"//result[@numFound=0]" );- assertEquals( 1, tracker.autoCommitCount );+ assertEquals( 1, tracker.getCommitCount());// Now add some morefor( int i=0; i<14; i++ ) {@@ -102,12 +128,12 @@ public class AutoCommitTest extends AbstractSolrTestCase {}// It should not be there right awayassertQ("shouldn't find any", req("id:B1") ,"//result[@numFound=0]" );- assertEquals( 1, tracker.autoCommitCount );+ assertEquals( 1, tracker.getCommitCount() );req.setContentStreams( toContentStreams(adoc("id", "B14", "subject", "info" ), null ) );handler.handleRequest( req, rsp );- Thread.sleep( 1000 );+ assertTrue(trigger.waitForCommit(10000));// add request will block if commit has already started or completedreq.setContentStreams( toContentStreams(@@ -115,17 +141,19 @@ public class AutoCommitTest extends AbstractSolrTestCase {handler.handleRequest( req, rsp );assertQ("should find one", req("id:B14") ,"//result[@numFound=1]" );- assertEquals( 2, tracker.autoCommitCount );+ assertEquals( 2, tracker.getCommitCount() );assertQ("should find none", req("id:B15") ,"//result[@numFound=0]" );- assertEquals( 2, tracker.autoCommitCount );+ assertEquals( 2, tracker.getCommitCount());}public void testMaxTime() throws Exception {+ CommitListener trigger = new CommitListener();SolrCore core = h.getCore();DirectUpdateHandler2 updater = (DirectUpdateHandler2) core.getUpdateHandler();DirectUpdateHandler2.CommitTracker tracker = updater.tracker;tracker.timeUpperBound = 500;tracker.docsUpperBound = -1;+ updater.commitCallbacks.add(trigger);XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler();handler.init( null );@@ -143,7 +171,7 @@ public class AutoCommitTest extends AbstractSolrTestCase {assertQ("shouldn't find any", req("id:529") ,"//result[@numFound=0]" );// Wait longer than the autocommit time- Thread.sleep( 1000 );+ assertTrue(trigger.waitForCommit(10000));req.setContentStreams( toContentStreams(adoc("id", "530", "field_t", "what's inside?", "subject", "info"), null ) );handler.handleRequest( req, rsp );@@ -157,7 +185,11 @@ public class AutoCommitTest extends AbstractSolrTestCase {assertU( delI("529") );assertQ("deleted, but should still be there", req("id:529") ,"//result[@numFound=1]" );// Wait longer than the autocommit time- Thread.sleep( 1000 );+ assertTrue(trigger.waitForCommit(10000));+ req.setContentStreams( toContentStreams(+ adoc("id", "550", "field_t", "what's inside?", "subject", "info"), null ) );+ handler.handleRequest( req, rsp );+ assertEquals( 2, tracker.getCommitCount() );assertQ("deleted and time has passed", req("id:529") ,"//result[@numFound=0]" );// now make the call 10 times really fast and make sure it@@ -168,17 +200,16 @@ public class AutoCommitTest extends AbstractSolrTestCase {handler.handleRequest( req, rsp );}assertQ("should not be there yet", req("id:500") ,"//result[@numFound=0]" );- assertEquals( 2, tracker.autoCommitCount );// Wait longer than the autocommit time- Thread.sleep( 1000 );+ assertTrue(trigger.waitForCommit(10000));req.setContentStreams( toContentStreams(adoc("id", "531", "field_t", "what's inside?", "subject", "info"), null ) );handler.handleRequest( req, rsp );+ assertEquals( 3, tracker.getCommitCount() );assertQ("now it should", req("id:500") ,"//result[@numFound=1]" );assertQ("but not this", req("id:531") ,"//result[@numFound=0]" );- assertEquals( 3, tracker.autoCommitCount );}public void testMaxPending() throws Exception {