Browse Source

Use ActionTestUtils for non-failing listeners (#100255)

Some small cleanups around assertions to make use of
`ActionTestUtils#assertNoFailureListener` where applicable. Also removes
an assertion that would take down the node in production.
David Turner 2 years ago
parent
commit
ed3d8e774a

+ 8 - 9
server/src/test/java/org/elasticsearch/action/admin/cluster/configuration/TransportAddVotingConfigExclusionsActionTests.java

@@ -8,8 +8,10 @@
 package org.elasticsearch.action.admin.cluster.configuration;
 
 import org.elasticsearch.ElasticsearchTimeoutException;
+import org.elasticsearch.action.ActionListenerResponseHandler;
 import org.elasticsearch.action.ActionResponse;
 import org.elasticsearch.action.support.ActionFilters;
+import org.elasticsearch.action.support.ActionTestUtils;
 import org.elasticsearch.cluster.ClusterName;
 import org.elasticsearch.cluster.ClusterState;
 import org.elasticsearch.cluster.ClusterStateObserver;
@@ -514,17 +516,14 @@ public class TransportAddVotingConfigExclusionsActionTests extends ESTestCase {
     }
 
     private TransportResponseHandler<ActionResponse.Empty> expectSuccess(Consumer<ActionResponse.Empty> onResponse) {
-        return responseHandler(onResponse, e -> { throw new AssertionError("unexpected", e); });
+        return new ActionListenerResponseHandler<>(
+            ActionTestUtils.assertNoFailureListener(onResponse::accept),
+            in -> ActionResponse.Empty.INSTANCE,
+            TransportResponseHandler.TRANSPORT_WORKER
+        );
     }
 
     private TransportResponseHandler<ActionResponse.Empty> expectError(Consumer<TransportException> onException) {
-        return responseHandler(r -> { assert false : r; }, onException);
-    }
-
-    private TransportResponseHandler<ActionResponse.Empty> responseHandler(
-        Consumer<ActionResponse.Empty> onResponse,
-        Consumer<TransportException> onException
-    ) {
         return new TransportResponseHandler<>() {
             @Override
             public ActionResponse.Empty read(StreamInput in) {
@@ -538,7 +537,7 @@ public class TransportAddVotingConfigExclusionsActionTests extends ESTestCase {
 
             @Override
             public void handleResponse(ActionResponse.Empty response) {
-                onResponse.accept(response);
+                assert false : response;
             }
 
             @Override

+ 1 - 1
test/framework/src/main/java/org/elasticsearch/action/support/ActionTestUtils.java

@@ -65,7 +65,7 @@ public class ActionTestUtils {
     }
 
     public static <T> ActionListener<T> assertNoFailureListener(CheckedConsumer<T, Exception> consumer) {
-        return ActionListener.wrap(consumer, e -> { throw new AssertionError(e); });
+        return ActionListener.wrap(consumer, e -> { throw new AssertionError("unexpected", e); });
     }
 
     public static ResponseListener wrapAsRestResponseListener(ActionListener<Response> listener) {

+ 4 - 1
x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java

@@ -596,7 +596,10 @@ public class AutoFollowCoordinator extends AbstractLifecycleComponent implements
             final GroupedActionListener<Tuple<Index, Exception>> groupedListener = new GroupedActionListener<>(
                 leaderIndicesToFollow.size(),
                 ActionListener.wrap(rs -> resultHandler.accept(new AutoFollowResult(autoFollowPattenName, new ArrayList<>(rs))), e -> {
-                    throw new AssertionError("must never happen", e);
+                    final var illegalStateException = new IllegalStateException("must never happen", e);
+                    LOGGER.error("must never happen", illegalStateException);
+                    assert false : illegalStateException;
+                    throw illegalStateException;
                 })
             );
 

+ 2 - 4
x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/DataTiersUsageRestCancellationIT.java

@@ -8,9 +8,9 @@
 package org.elasticsearch.xpack.core.rest.action;
 
 import org.apache.http.client.methods.HttpGet;
-import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsAction;
 import org.elasticsearch.action.support.ActionFilters;
+import org.elasticsearch.action.support.ActionTestUtils;
 import org.elasticsearch.action.support.PlainActionFuture;
 import org.elasticsearch.action.support.SubscribableListener;
 import org.elasticsearch.action.support.TransportAction;
@@ -80,9 +80,7 @@ public class DataTiersUsageRestCancellationIT extends ESIntegTestCase {
                 (handler, request, channel, task) -> {
                     tasksBlockedLatch.countDown();
                     nodeStatsRequestsReleaseListener.addListener(
-                        ActionListener.wrap(ignored -> handler.messageReceived(request, channel, task), e -> {
-                            throw new AssertionError("unexpected", e);
-                        })
+                        ActionTestUtils.assertNoFailureListener(ignored -> handler.messageReceived(request, channel, task))
                     );
                 }
             );