From a4ec6c4177d3e1a908c4d65549b73665574af6d8 Mon Sep 17 00:00:00 2001 From: Mark Robert Miller Date: Fri, 19 Aug 2011 14:38:50 +0000 Subject: [PATCH] try to harden this test more git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1159649 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/solr/update/AutoCommitTest.java | 93 ++++++++++++++----- 1 file changed, 71 insertions(+), 22 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/update/AutoCommitTest.java b/solr/core/src/test/org/apache/solr/update/AutoCommitTest.java index 2df8d6b18b7..032de4d4e06 100644 --- a/solr/core/src/test/org/apache/solr/update/AutoCommitTest.java +++ b/solr/core/src/test/org/apache/solr/update/AutoCommitTest.java @@ -31,10 +31,24 @@ import org.apache.solr.handler.XmlUpdateRequestHandler; import org.apache.solr.request.SolrQueryRequestBase; import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.search.SolrIndexSearcher; +import org.apache.solr.update.NewSearcherListener.TriggerOn; import org.apache.solr.util.AbstractSolrTestCase; class NewSearcherListener implements SolrEventListener { + + enum TriggerOn {Both, Soft, Hard} + private volatile boolean triggered = false; + private volatile TriggerOn lastType; + private volatile TriggerOn triggerOnType; + + public NewSearcherListener() { + this(TriggerOn.Both); + } + + public NewSearcherListener(TriggerOn type) { + this.triggerOnType = type; + } @Override public void init(NamedList args) {} @@ -42,14 +56,24 @@ class NewSearcherListener implements SolrEventListener { @Override public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) { - triggered = true; + if (triggerOnType == TriggerOn.Soft && lastType == TriggerOn.Soft) { + triggered = true; + } else if (triggerOnType == TriggerOn.Hard && lastType == TriggerOn.Hard) { + triggered = true; + } else if (triggerOnType == TriggerOn.Both) { + triggered = true; + } } @Override - public void postCommit() {} + public void postCommit() { + lastType = TriggerOn.Hard; + } @Override - public void postSoftCommit() {} + public void postSoftCommit() { + lastType = TriggerOn.Soft; + } public void reset() { triggered = false; @@ -217,20 +241,26 @@ public class AutoCommitTest extends AbstractSolrTestCase { public void testSoftCommitMaxDocs() throws Exception { SolrCore core = h.getCore(); - NewSearcherListener trigger = new NewSearcherListener(); + NewSearcherListener trigger = new NewSearcherListener(TriggerOn.Hard); + core.registerNewSearcherListener(trigger); + DirectUpdateHandler2 updateHandler = (DirectUpdateHandler2)core.getUpdateHandler(); + updateHandler.registerCommitCallback(trigger); + CommitTracker tracker = updateHandler.commitTracker; tracker.setTimeUpperBound(-1); tracker.setDocsUpperBound(8); - NewSearcherListener softTrigger = new NewSearcherListener(); - + NewSearcherListener softTrigger = new NewSearcherListener(TriggerOn.Soft); + updateHandler.registerSoftCommitCallback(softTrigger); + core.registerNewSearcherListener(softTrigger); + CommitTracker softTracker = updateHandler.softCommitTracker; softTracker.setTimeUpperBound(-1); softTracker.setDocsUpperBound(4); - core.registerNewSearcherListener(softTrigger); + XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler(); handler.init( null ); @@ -254,8 +284,7 @@ public class AutoCommitTest extends AbstractSolrTestCase { handler.handleRequest( req, rsp ); assertTrue(softTrigger.waitForNewSearcher(10000)); - - core.registerNewSearcherListener(trigger); + softTrigger.reset(); assertQ("should find 5", req("*:*") ,"//result[@numFound=5]" ); assertEquals( 1, softTracker.getCommitCount()); @@ -279,6 +308,8 @@ public class AutoCommitTest extends AbstractSolrTestCase { } req.close(); + assertTrue(softTrigger.waitForNewSearcher(10000)); + softTrigger.reset(); assertTrue(trigger.waitForNewSearcher(10000)); assertQ("should find 10", req("*:*") ,"//result[@numFound=10]" ); @@ -365,17 +396,25 @@ public class AutoCommitTest extends AbstractSolrTestCase { public void testSoftAndHardCommitMaxTime() throws Exception { SolrCore core = h.getCore(); - NewSearcherListener trigger = new NewSearcherListener(); - core.registerNewSearcherListener(trigger); + NewSearcherListener softTrigger = new NewSearcherListener(TriggerOn.Soft); + NewSearcherListener hardTrigger = new NewSearcherListener(TriggerOn.Hard); + core.registerNewSearcherListener(softTrigger); + core.registerNewSearcherListener(hardTrigger); DirectUpdateHandler2 updater = (DirectUpdateHandler2) core.getUpdateHandler(); + + updater.registerCommitCallback(softTrigger); + updater.registerSoftCommitCallback(softTrigger); + updater.registerCommitCallback(hardTrigger); + updater.registerSoftCommitCallback(hardTrigger); + CommitTracker hardTracker = updater.commitTracker; CommitTracker softTracker = updater.softCommitTracker; // too low of a number can cause a slow host to commit before the test code checks that it // isn't there... causing a failure at "shouldn't find any" - softTracker.setTimeUpperBound(100); + softTracker.setTimeUpperBound(300); softTracker.setDocsUpperBound(-1); - hardTracker.setTimeUpperBound(500); + hardTracker.setTimeUpperBound(1200); hardTracker.setDocsUpperBound(-1); // updater.commitCallbacks.add(trigger); @@ -389,15 +428,15 @@ public class AutoCommitTest extends AbstractSolrTestCase { SolrQueryRequestBase req = new SolrQueryRequestBase( core, params ) {}; req.setContentStreams( toContentStreams( adoc("id", "529", "field_t", "what's inside?", "subject", "info"), null ) ); - trigger.reset(); + handler.handleRequest( req, rsp ); // Check if it is in the index assertQ("shouldn't find any", req("id:529") ,"//result[@numFound=0]" ); // Wait longer than the autocommit time - assertTrue(trigger.waitForNewSearcher(30000)); - trigger.reset(); + assertTrue(softTrigger.waitForNewSearcher(30000)); + softTrigger.reset(); req.setContentStreams( toContentStreams( adoc("id", "530", "field_t", "what's inside?", "subject", "info"), null ) ); handler.handleRequest( req, rsp ); @@ -410,9 +449,12 @@ public class AutoCommitTest extends AbstractSolrTestCase { // Delete the document assertU( delI("529") ); assertQ("deleted, but should still be there", req("id:529") ,"//result[@numFound=1]" ); - // Wait longer than the autocommit time - assertTrue(trigger.waitForNewSearcher(15000)); - trigger.reset(); + + // Wait longer than the autocommit time - wait twice to ensure latest is picked up + assertTrue(softTrigger.waitForNewSearcher(15000)); + softTrigger.reset(); + + req.setContentStreams( toContentStreams( adoc("id", "550", "field_t", "what's inside?", "subject", "info"), null ) ); handler.handleRequest( req, rsp ); @@ -430,8 +472,8 @@ public class AutoCommitTest extends AbstractSolrTestCase { assertQ("should not be there yet", req("id:500") ,"//result[@numFound=0]" ); // Wait longer than the autocommit time - assertTrue(trigger.waitForNewSearcher(15000)); - trigger.reset(); + assertTrue(softTrigger.waitForNewSearcher(15000)); + softTrigger.reset(); req.setContentStreams( toContentStreams( adoc("id", "531", "field_t", "what's inside?", "subject", "info"), null ) ); @@ -441,12 +483,19 @@ public class AutoCommitTest extends AbstractSolrTestCase { int softCommitCnt = softTracker.getCommitCount(); assertTrue("commit cnt:" + softCommitCnt, softCommitCnt == 2 || softCommitCnt == 3); + // depending on timing, you might see 1 or 2 hard commits + assertTrue(hardTrigger.waitForNewSearcher(15000)); + hardTrigger.reset(); + int hardCommitCnt = hardTracker.getCommitCount(); assertTrue("commit cnt:" + hardCommitCnt, hardCommitCnt == 1 || hardCommitCnt == 2); + assertTrue(softTrigger.waitForNewSearcher(15000)); + softTrigger.reset(); + assertQ("now it should", req("id:500") ,"//result[@numFound=1]" ); - assertQ("but not this", req("id:531") ,"//result[@numFound=0]" ); + assertQ("but not this", req("id:531") ,"//result[@numFound=1]" ); } }