Apache Lucene open-source search software
tests: test harness shouldn't hold core reference
Yonik Seeleycc90a37ed79e
diff --git a/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java b/solr/core/src/test/org/apache/solr/core/SolrCoreTest.javaindex 47aada81f07..43bc6ec0b05 100755--- a/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java+++ b/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java@@ -24,6 +24,7 @@ import org.apache.solr.handler.component.QueryComponent;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.schema.IndexSchema;import org.apache.solr.util.plugin.SolrCoreAware;import org.junit.Test;@@ -49,6 +50,8 @@ public class SolrCoreTest extends SolrTestCaseJ4 {public void testRemoveThenAddDefaultCore() throws Exception {final CoreContainer cores = h.getCoreContainer();SolrCore core = cores.getCore("");++ IndexSchema schema = h.getCore().getSchema();assertEquals(COLLECTION1, cores.getDefaultCoreName());cores.remove("");@@ -57,7 +60,7 @@ public class SolrCoreTest extends SolrTestCaseJ4 {SolrCore newCore = new SolrCore(COLLECTION1, dataDir + File.separator- + "datadir2", new SolrConfig("solr/collection1", "solrconfig.xml", null), h.getCore().getSchema(),+ + "datadir2", new SolrConfig("solr/collection1", "solrconfig.xml", null), schema,new CoreDescriptor(cores, COLLECTION1, "solr/collection1"));cores.register(newCore, false);diff --git a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.javaindex 1094cb38570..fa519b95fa8 100644--- a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java+++ b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java@@ -71,8 +71,8 @@ import java.util.Map;**/public class TestHarness {+ String coreName;protected CoreContainer container;- private SolrCore core;private final ThreadLocal<DocumentBuilder> builderTL = new ThreadLocal<DocumentBuilder>();private final ThreadLocal<XPath> xpathTL = new ThreadLocal<XPath>();public UpdateRequestHandler updater;@@ -114,11 +114,8 @@ public class TestHarness {container = init.initialize();if (coreName == null)coreName = CoreContainer.DEFAULT_DEFAULT_CORE_NAME;- // get the core & decrease its refcount:- // the container holds the core for the harness lifetime- core = container.getCore(coreName);- if (core != null)- core.close();++ this.coreName = coreName;updater = new UpdateRequestHandler();updater.init( null );@@ -206,10 +203,27 @@ public class TestHarness {return container;}+ /** Gets a core that does not have it's refcount incremented (i.e. there is no need to+ * close when done). This is not MT safe in conjunction with reloads!+ */public SolrCore getCore() {+ // get the core & decrease its refcount:+ // the container holds the core for the harness lifetime+ SolrCore core = container.getCore(coreName);+ if (core != null)+ core.close();return core;}-++ /** Gets the core with it's reference count incremented.+ * You must call core.close() when done!+ */+ public SolrCore getCoreInc() {+ return container.getCore(coreName);+ }+++/*** Processes an "update" (add, commit or optimize) and* returns the response as a String.@@ -218,6 +232,7 @@ public class TestHarness {* @return The XML response to the update*/public String update(String xml) {+ SolrCore core = getCoreInc();DirectSolrConnection connection = new DirectSolrConnection(core);SolrRequestHandler handler = core.getRequestHandler("/update");// prefer the handler mapped to /update, but use our generic backup handler@@ -231,6 +246,8 @@ public class TestHarness {throw (SolrException)e;} catch (Exception e) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);+ } finally {+ core.close();}}@@ -324,6 +341,7 @@ public class TestHarness {* @see LocalSolrQueryRequest*/public String query(String handler, SolrQueryRequest req) throws Exception {+ SolrCore core = getCoreInc();try {SolrQueryResponse rsp = new SolrQueryResponse();SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));@@ -341,18 +359,24 @@ public class TestHarness {} finally {req.close();SolrRequestInfo.clearRequestInfo();+ core.close();}}/** It is the users responsibility to close the request object when done with it.* This method does not set/clear SolrRequestInfo */public SolrQueryResponse queryAndResponse(String handler, SolrQueryRequest req) throws Exception {- SolrQueryResponse rsp = new SolrQueryResponse();- core.execute(core.getRequestHandler(handler),req,rsp);- if (rsp.getException() != null) {- throw rsp.getException();+ SolrCore core = getCoreInc();+ try {+ SolrQueryResponse rsp = new SolrQueryResponse();+ core.execute(core.getRequestHandler(handler),req,rsp);+ if (rsp.getException() != null) {+ throw rsp.getException();+ }+ return rsp;+ } finally {+ core.close();}- return rsp;}@@ -578,6 +602,10 @@ public class TestHarness {* ignored.</b>* </li>* </ul>+ *+ * TODO: this isn't really safe in the presense of core reloads!+ * Perhaps the best we could do is increment the core reference count+ * and decrement it in the request close() method?*/public LocalSolrQueryRequest makeRequest(String ... q) {if (q.length==1) {