tests: try and prevent some spurious test failures that may be happening because jetty/solr isn't ready

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@819804 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2009-09-29 02:55:49 +00:00
parent fe2a788525
commit 3f64cb5dbc
2 changed files with 43 additions and 3 deletions

View File

@ -41,7 +41,7 @@ public class MultiCoreExampleJettyTest extends MultiCoreExampleTestBase {
super.setUp();
jetty = new JettySolrRunner( context, 0 );
jetty.start();
jetty.start(false);
port = jetty.getLocalPort();
h.getCoreContainer().setPersistent(false);

View File

@ -18,6 +18,8 @@
package org.apache.solr.client.solrj.embedded;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@ -41,6 +43,7 @@ public class JettySolrRunner
{
Server server;
FilterHolder dispatchFilter;
String context;
public JettySolrRunner( String context, int port )
{
@ -70,6 +73,7 @@ public class JettySolrRunner
private void init( String context, int port )
{
this.context = context;
server = new Server( port );
server.setStopAtShutdown( true );
@ -85,12 +89,19 @@ public class JettySolrRunner
//------------------------------------------------------------------------------------------------
public void start() throws Exception
{
start(true);
}
public void start(boolean waitForSolr) throws Exception
{
if(!server.isRunning() ) {
server.start();
}
if (waitForSolr) waitForSolr(context);
}
public void stop() throws Exception
{
if( server.isRunning() ) {
@ -99,6 +110,35 @@ public class JettySolrRunner
}
}
/** Waits until a ping query to the solr server succeeds,
* retrying every 200 milliseconds for a total of 20 seconds.
*/
public void waitForSolr(String context) throws Exception
{
int port = getLocalPort();
// A raw term query type doesn't check the schema
URL url = new URL("http://localhost:"+port+context+"/select?q={!raw+f=junit_test_query}ping");
Exception ex = null;
// Wait for a total of 20 seconds: 100 tries, 200 milliseconds each
for (int i=0; i<100; i++) {
try {
InputStream stream = url.openStream();
stream.close();
} catch (IOException e) {
e.printStackTrace();
ex = e;
Thread.sleep(200);
continue;
}
return;
}
throw new RuntimeException("Jetty/Solr unresponsive",ex);
}
/**
* Returns the Local Port of the first Connector found for the jetty Server.
* @exception RuntimeException if there is no Connector
@ -110,7 +150,7 @@ public class JettySolrRunner
}
return conns[0].getLocalPort();
}
//--------------------------------------------------------------
//--------------------------------------------------------------