From 3f64cb5dbcb02086a5938a13d1faf3d018acbfea Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Tue, 29 Sep 2009 02:55:49 +0000 Subject: [PATCH] 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 --- .../embedded/MultiCoreExampleJettyTest.java | 2 +- .../solrj/embedded/JettySolrRunner.java | 44 ++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/test/org/apache/solr/client/solrj/embedded/MultiCoreExampleJettyTest.java b/src/test/org/apache/solr/client/solrj/embedded/MultiCoreExampleJettyTest.java index a08c61ee44b..8fe4b69196f 100644 --- a/src/test/org/apache/solr/client/solrj/embedded/MultiCoreExampleJettyTest.java +++ b/src/test/org/apache/solr/client/solrj/embedded/MultiCoreExampleJettyTest.java @@ -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); diff --git a/src/webapp/src/org/apache/solr/client/solrj/embedded/JettySolrRunner.java b/src/webapp/src/org/apache/solr/client/solrj/embedded/JettySolrRunner.java index ff7a058c46d..b5cf34db763 100644 --- a/src/webapp/src/org/apache/solr/client/solrj/embedded/JettySolrRunner.java +++ b/src/webapp/src/org/apache/solr/client/solrj/embedded/JettySolrRunner.java @@ -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(); } - + //-------------------------------------------------------------- //--------------------------------------------------------------