You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sa...@apache.org on 2018/07/31 02:50:23 UTC

[2/2] lucene-solr:master: Fix InfixSuggestersTest.testShutdownDuringBuild() failures

Fix InfixSuggestersTest.testShutdownDuringBuild() failures


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/a08eadb4
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/a08eadb4
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/a08eadb4

Branch: refs/heads/master
Commit: a08eadb4809b1d70d9cf6a098db9489f0325c260
Parents: 8d28bbc
Author: Steve Rowe <sa...@apache.org>
Authored: Mon Jul 30 22:49:30 2018 -0400
Committer: Steve Rowe <sa...@apache.org>
Committed: Mon Jul 30 22:49:49 2018 -0400

----------------------------------------------------------------------
 .../org/apache/lucene/util/LuceneTestCase.java  | 54 ++++++++++++++++++--
 solr/CHANGES.txt                                |  2 +
 .../handler/component/InfixSuggestersTest.java  | 33 +++++++++---
 3 files changed, 79 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a08eadb4/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
----------------------------------------------------------------------
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 6390cc8..a17d60e 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -52,6 +52,7 @@ import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -2731,17 +2732,64 @@ public abstract class LuceneTestCase extends Assert {
           return expectedWrappedType.cast(cause);
         } else {
           AssertionFailedError assertion = new AssertionFailedError
-              ("Unexpected wrapped exception type, expected " + expectedWrappedType.getSimpleName());
+              ("Unexpected wrapped exception type, expected " + expectedWrappedType.getSimpleName() 
+                  + " but got: " + cause);
           assertion.initCause(e);
           throw assertion;
         }
       }
       AssertionFailedError assertion = new AssertionFailedError
-          ("Unexpected outer exception type, expected " + expectedOuterType.getSimpleName());
+          ("Unexpected outer exception type, expected " + expectedOuterType.getSimpleName()
+           + " but got: " + e);
       assertion.initCause(e);
       throw assertion;
     }
-    throw new AssertionFailedError("Expected outer exception " + expectedOuterType.getSimpleName());
+    throw new AssertionFailedError("Expected outer exception " + expectedOuterType.getSimpleName()
+        + " but no exception was thrown.");
+  }
+
+  /**
+   * Checks that one of the specified wrapped and outer exception classes are thrown
+   * by the given runnable, and returns the outer exception.
+   * 
+   * This method accepts outer exceptions with no wrapped exception;
+   * an empty list of expected wrapped exception types indicates no wrapped exception.
+   */
+  public static <TO extends Throwable, TW extends Throwable> TO expectThrowsAnyOf
+  (LinkedHashMap<Class<? extends TO>,List<Class<? extends TW>>> expectedOuterToWrappedTypes, ThrowingRunnable runnable) {
+    try {
+      runnable.run();
+    } catch (Throwable e) {
+      for (Map.Entry<Class<? extends TO>, List<Class<? extends TW>>> entry : expectedOuterToWrappedTypes.entrySet()) {
+        Class<? extends TO> expectedOuterType = entry.getKey();
+        List<Class<? extends TW>> expectedWrappedTypes = entry.getValue();
+        Throwable cause = e.getCause();
+        if (expectedOuterType.isInstance(e)) {
+          if (expectedWrappedTypes.isEmpty()) {
+            return null; // no wrapped exception
+          } else {
+            for (Class<? extends TW> expectedWrappedType : expectedWrappedTypes) {
+              if (expectedWrappedType.isInstance(cause)) {
+                return expectedOuterType.cast(e);
+              }
+            }
+            List<String> wrappedTypes = expectedWrappedTypes.stream().map(Class::getSimpleName).collect(Collectors.toList());
+            AssertionFailedError assertion = new AssertionFailedError
+                ("Unexpected wrapped exception type, expected one of " + wrappedTypes + " but got: " + cause);
+            assertion.initCause(e);
+            throw assertion;
+          }
+        }
+      }
+      List<String> outerTypes = expectedOuterToWrappedTypes.keySet().stream().map(Class::getSimpleName).collect(Collectors.toList());
+      AssertionFailedError assertion = new AssertionFailedError
+          ("Unexpected outer exception type, expected one of " + outerTypes + " but got: " + e);
+      assertion.initCause(e);
+      throw assertion;
+    }
+    List<String> outerTypes = expectedOuterToWrappedTypes.keySet().stream().map(Class::getSimpleName).collect(Collectors.toList());
+    throw new AssertionFailedError("Expected any of the following outer exception types: " + outerTypes
+        + " but no exception was thrown.");
   }
 
   /** Returns true if the file exists (can be opened), false

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a08eadb4/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 89e6938..2d8db8c 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -183,6 +183,8 @@ Bug Fixes
 
 * SOLR-12477: An update would return a client error(400) if it hit a AlreadyClosedException.
   We now return the error as a server error(500) instead (Jeffery via Varun Thacker)
+  
+* SOLR-12606: Fix InfixSuggestersTest.testShutdownDuringBuild() failures. (Steve Rowe) 
 
 Optimizations
 ----------------------

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a08eadb4/solr/core/src/test/org/apache/solr/handler/component/InfixSuggestersTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/handler/component/InfixSuggestersTest.java b/solr/core/src/test/org/apache/solr/handler/component/InfixSuggestersTest.java
index a8188bb..7971bf0 100644
--- a/solr/core/src/test/org/apache/solr/handler/component/InfixSuggestersTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/component/InfixSuggestersTest.java
@@ -17,10 +17,14 @@
 
 package org.apache.solr.handler.component;
 
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 
 import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrException;
 import org.apache.solr.common.util.ExecutorUtil;
 import org.apache.solr.spelling.suggest.RandomTestDictionaryFactory;
 import org.apache.solr.spelling.suggest.SuggesterParams;
@@ -94,7 +98,7 @@ public class InfixSuggestersTest extends SolrTestCaseJ4 {
 
   @Test
   public void testReloadDuringBuild() throws Exception {
-    ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("AnalyzingInfixSuggesterTest");
+    ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("InfixSuggesterTest");
     try {
       // Build the suggester in the background with a long dictionary
       Future job = executor.submit(() ->
@@ -114,20 +118,35 @@ public class InfixSuggestersTest extends SolrTestCaseJ4 {
 
   @Test
   public void testShutdownDuringBuild() throws Exception {
-    ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("AnalyzingInfixSuggesterTest");
+    ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("InfixSuggesterTest");
     try {
+      LinkedHashMap<Class<? extends Throwable>, List<Class<? extends Throwable>>> expected = new LinkedHashMap<>();
+      expected.put(RuntimeException.class, Arrays.asList
+          (SolrCoreState.CoreIsClosedException.class, SolrException.class, IllegalStateException.class));
+      final Throwable[] outerException = new Throwable[1];
       // Build the suggester in the background with a long dictionary
-      Future job = executor.submit(() -> 
-          expectThrows(RuntimeException.class, SolrCoreState.CoreIsClosedException.class,
-              () -> assertQ(req("qt", rh_analyzing_long,
-                  SuggesterParams.SUGGEST_BUILD_ALL, "true"),
-                  "//str[@name='command'][.='buildAll']")));
+      Future job = executor.submit(() -> outerException[0] = expectThrowsAnyOf(expected,
+          () -> assertQ(req("qt", rh_analyzing_long, SuggesterParams.SUGGEST_BUILD_ALL, "true"),
+              "//str[@name='command'][.='buildAll']")));
       Thread.sleep(100); // TODO: is there a better way to ensure that the build has begun?
       h.close();
       // Stop the dictionary's input iterator
       System.clearProperty(RandomTestDictionaryFactory.RandomTestDictionary
           .getEnabledSysProp("longRandomAnalyzingInfixSuggester"));
       job.get();
+      Throwable wrappedException = outerException[0].getCause();
+      if (wrappedException instanceof SolrException) {
+        String expectedMessage = "SolrCoreState already closed.";
+        assertTrue("Expected wrapped SolrException message to contain '" + expectedMessage 
+            + "' but message is '" + wrappedException.getMessage() + "'", 
+            wrappedException.getMessage().contains(expectedMessage));
+      } else if (wrappedException instanceof IllegalStateException
+          && ! (wrappedException instanceof SolrCoreState.CoreIsClosedException)) { // CoreIsClosedException extends IllegalStateException
+        String expectedMessage = "Cannot commit on an closed writer. Add documents first";
+        assertTrue("Expected wrapped IllegalStateException message to contain '" + expectedMessage
+                + "' but message is '" + wrappedException.getMessage() + "'",
+            wrappedException.getMessage().contains(expectedMessage));
+      }
     } finally {
       ExecutorUtil.shutdownAndAwaitTermination(executor);
       initCore("solrconfig-infixsuggesters.xml","schema.xml"); // put the core back for other tests