You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by yo...@apache.org on 2012/07/20 17:10:59 UTC

svn commit: r1363825 - in /lucene/dev/branches/branch_4x: ./ dev-tools/ lucene/ solr/ solr/core/src/test/org/apache/solr/core/SolrCoreTest.java solr/test-framework/src/java/org/apache/solr/util/TestHarness.java

Author: yonik
Date: Fri Jul 20 15:10:59 2012
New Revision: 1363825

URL: http://svn.apache.org/viewvc?rev=1363825&view=rev
Log:
tests: test harness shouldn't hold core reference

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/dev-tools/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/solr/   (props changed)
    lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java
    lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java

Modified: lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java?rev=1363825&r1=1363824&r2=1363825&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/core/SolrCoreTest.java Fri Jul 20 15:10:59 2012
@@ -24,6 +24,7 @@ import org.apache.solr.handler.component
 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 SolrTe
   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 SolrTe
     
     
     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);

Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java?rev=1363825&r1=1363824&r2=1363825&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java Fri Jul 20 15:10:59 2012
@@ -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) {