Author: klaas
Date: Tue Jan 29 21:55:58 2008
New Revision: 616641
URL: http://svn.apache.org/viewvc?rev=616641&view=rev
Log:
AutoCommitTest: faster and less likely to fail due to slow hardware
Modified:
lucene/solr/trunk/src/java/org/apache/solr/update/DirectUpdateHandler2.java
lucene/solr/trunk/src/test/org/apache/solr/update/AutoCommitTest.java
Modified:
lucene/solr/trunk/src/java/org/apache/solr/update/DirectUpdateHandler2.java
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/update/DirectUpdateHandler2.java?rev=616641&r1=616640&r2=616641&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/update/DirectUpdateHandler2.java
(original)
+++ lucene/solr/trunk/src/java/org/apache/solr/update/DirectUpdateHandler2.java
Tue Jan 29 21:55:58 2008
@@ -725,6 +725,9 @@
}
}
+ // to facilitate testing: blocks if called during commit
+ public synchronized int getCommitCount() { return autoCommitCount; }
+
public String toString() {
if(timeUpperBound > 0 || docsUpperBound > 0) {
return
Modified: lucene/solr/trunk/src/test/org/apache/solr/update/AutoCommitTest.java
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/update/AutoCommitTest.java?rev=616641&r1=616640&r2=616641&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/update/AutoCommitTest.java
(original)
+++ lucene/solr/trunk/src/test/org/apache/solr/update/AutoCommitTest.java Tue
Jan 29 21:55:58 2008
@@ -24,16 +24,39 @@
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 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 @@
}
// It should not be there right away
assertQ("shouldn't find any", req("id:A1") ,"//[EMAIL PROTECTED]" );
- 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 it
assertQ("should find one", req("id:A14") ,"//[EMAIL PROTECTED]" );
- assertEquals( 1, tracker.autoCommitCount );
+ assertEquals( 1, tracker.getCommitCount());
// But not the one added afterward
assertQ("should find one", req("id:A15") ,"//[EMAIL PROTECTED]" );
- assertEquals( 1, tracker.autoCommitCount );
+ assertEquals( 1, tracker.getCommitCount());
// Now add some more
for( int i=0; i<14; i++ ) {
@@ -102,12 +128,12 @@
}
// It should not be there right away
assertQ("shouldn't find any", req("id:B1") ,"//[EMAIL PROTECTED]" );
- 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 completed
req.setContentStreams( toContentStreams(
@@ -115,17 +141,19 @@
handler.handleRequest( req, rsp );
assertQ("should find one", req("id:B14") ,"//[EMAIL PROTECTED]" );
- assertEquals( 2, tracker.autoCommitCount );
+ assertEquals( 2, tracker.getCommitCount() );
assertQ("should find none", req("id:B15") ,"//[EMAIL PROTECTED]" );
- 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 @@
assertQ("shouldn't find any", req("id:529") ,"//[EMAIL PROTECTED]" );
// 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 @@
assertU( delI("529") );
assertQ("deleted, but should still be there", req("id:529") ,"//[EMAIL
PROTECTED]" );
// 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") ,"//[EMAIL
PROTECTED]" );
// now make the call 10 times really fast and make sure it
@@ -168,17 +200,16 @@
handler.handleRequest( req, rsp );
}
assertQ("should not be there yet", req("id:500") ,"//[EMAIL PROTECTED]" );
- 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") ,"//[EMAIL PROTECTED]" );
assertQ("but not this", req("id:531") ,"//[EMAIL PROTECTED]" );
- assertEquals( 3, tracker.autoCommitCount );
}
public void testMaxPending() throws Exception {