Apache Lucene open-source search software
Fix InfixSuggestersTest.testShutdownDuringBuild() failures
Steve Rowea08eadb4809b
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.javaindex 6390cc8f3c8..a17d60ec985 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), falsediff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 89e69389ffb..2d8db8c0f2c 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----------------------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.javaindex a8188bbfb4e..7971bf00115 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 {@Testpublic void testReloadDuringBuild() throws Exception {- ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("AnalyzingInfixSuggesterTest");+ ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("InfixSuggesterTest");try {// Build the suggester in the background with a long dictionaryFuture job = executor.submit(() ->@@ -114,20 +118,35 @@ public class InfixSuggestersTest extends SolrTestCaseJ4 {@Testpublic 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 iteratorSystem.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