From f8d549d403d90867defa0b5976fe209788af2507 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 9 Jun 2021 17:34:31 +0200 Subject: [PATCH] Dry up HTTP Smoke Tests around Snapshots Drying up a few spots of code duplication with these tests. Partly to reduce the size of PR #73952 that makes use of the smoke test infrastructure. --- ...ockedSearcherRestCancellationTestCase.java | 16 ++---- .../http/ClusterStateRestCancellationIT.java | 16 ++---- .../http/ClusterStatsRestCancellationIT.java | 16 ++---- .../IndicesRecoveryRestCancellationIT.java | 16 ++---- .../http/RestGetMappingsCancellationIT.java | 16 ++---- .../http/SearchRestCancellationIT.java | 43 +++------------- .../AbstractSnapshotRestTestCase.java | 26 ++++++++++ .../RestGetSnapshotsCancellationIT.java | 47 +++--------------- .../RestSnapshotsStatusCancellationIT.java | 49 +++---------------- .../action/support/ActionTestUtils.java | 16 ++++++ .../AbstractSnapshotIntegTestCase.java | 19 +++++-- .../elasticsearch/test/ESIntegTestCase.java | 6 +-- .../action/XPackUsageRestCancellationIT.java | 16 ++---- .../eql/action/RestEqlCancellationIT.java | 24 ++------- 14 files changed, 106 insertions(+), 220 deletions(-) create mode 100644 qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java rename qa/smoke-test-http/src/test/java/org/elasticsearch/http/{ => snapshots}/RestGetSnapshotsCancellationIT.java (55%) rename qa/smoke-test-http/src/test/java/org/elasticsearch/http/{ => snapshots}/RestSnapshotsStatusCancellationIT.java (56%) diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java index 7e9a8657921b3..37d9abecb82e6 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java @@ -12,7 +12,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; import org.elasticsearch.common.settings.Setting; @@ -41,6 +40,7 @@ import java.util.function.Function; import static java.util.Collections.singletonList; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; @@ -92,19 +92,9 @@ void runTest(Request request, String actionPrefix) throws Exception { releasables.add(searcherBlock::release); } - final PlainActionFuture future = new PlainActionFuture<>(); + final PlainActionFuture future = new PlainActionFuture<>(); logger.info("--> sending request"); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); awaitTaskWithPrefix(actionPrefix); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java index b7d43de531de5..40fe313c36c42 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java @@ -15,7 +15,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; @@ -33,6 +32,7 @@ import java.util.concurrent.CancellationException; import java.util.function.UnaryOperator; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; public class ClusterStateRestCancellationIT extends HttpSmokeTestCase { @@ -75,19 +75,9 @@ public void testClusterStateRestCancellation() throws Exception { clusterStateRequest.addParameter("local", "true"); } - final PlainActionFuture future = new PlainActionFuture<>(); + final PlainActionFuture future = new PlainActionFuture<>(); logger.info("--> sending cluster state request"); - final Cancellable cancellable = getRestClient().performRequestAsync(clusterStateRequest, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final Cancellable cancellable = getRestClient().performRequestAsync(clusterStateRequest, wrapAsRestResponseListener(future)); awaitTaskWithPrefix(ClusterStateAction.NAME); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java index 22a0851378690..e61b610552baf 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java @@ -14,7 +14,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; @@ -44,6 +43,7 @@ import java.util.function.Function; import static java.util.Collections.singletonList; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; @@ -100,19 +100,9 @@ public void testClusterStateRestCancellation() throws Exception { final Request clusterStatsRequest = new Request(HttpGet.METHOD_NAME, "/_cluster/stats"); - final PlainActionFuture future = new PlainActionFuture<>(); + final PlainActionFuture future = new PlainActionFuture<>(); logger.info("--> sending cluster state request"); - final Cancellable cancellable = getRestClient().performRequestAsync(clusterStatsRequest, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final Cancellable cancellable = getRestClient().performRequestAsync(clusterStatsRequest, wrapAsRestResponseListener(future)); awaitTaskWithPrefix(ClusterStatsAction.NAME); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesRecoveryRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesRecoveryRestCancellationIT.java index af4737d24565e..55870bed5e851 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesRecoveryRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesRecoveryRestCancellationIT.java @@ -16,7 +16,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; @@ -25,6 +24,7 @@ import java.util.concurrent.CancellationException; import java.util.concurrent.Semaphore; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; @@ -68,19 +68,9 @@ private void runTest(Request request) throws Exception { releasables.add(operationBlock::release); } - final PlainActionFuture future = new PlainActionFuture<>(); + final PlainActionFuture future = new PlainActionFuture<>(); logger.info("--> sending request"); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); awaitTaskWithPrefix(RecoveryAction.NAME); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java index 859c8df759b2b..e8ba3ab4e9191 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java @@ -15,7 +15,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.cluster.AckedClusterStateUpdateTask; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ack.AckedRequest; @@ -31,6 +30,7 @@ import java.util.concurrent.CancellationException; import java.util.function.Function; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; @@ -64,18 +64,8 @@ public void testGetMappingsCancellation() throws Exception { }); final Request request = new Request(HttpGet.METHOD_NAME, "/test/_mappings"); - final PlainActionFuture future = new PlainActionFuture<>(); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final PlainActionFuture future = new PlainActionFuture<>(); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); assertThat(future.isDone(), equalTo(false)); awaitTaskWithPrefix(actionName); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java index b7a5224d6258e..642754b6e395e 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java @@ -19,11 +19,11 @@ import org.elasticsearch.action.search.MultiSearchRequest; import org.elasticsearch.action.search.SearchAction; import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.XContentType; @@ -49,13 +49,12 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CancellationException; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.index.query.QueryBuilders.scriptQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.hamcrest.Matchers.greaterThan; @@ -93,28 +92,15 @@ void verifyCancellationDuringQueryPhase(String searchAction, Request searchReque List plugins = initBlockFactory(); indexTestData(); - CountDownLatch latch = new CountDownLatch(1); - AtomicReference error = new AtomicReference<>(); - Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() { - @Override - public void onSuccess(Response response) { - latch.countDown(); - } - - @Override - public void onFailure(Exception exception) { - error.set(exception); - latch.countDown(); - } - }); + PlainActionFuture future = PlainActionFuture.newFuture(); + Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, wrapAsRestResponseListener(future)); awaitForBlock(plugins); cancellable.cancel(); ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get); disableBlocks(plugins); - latch.await(); - assertThat(error.get(), instanceOf(CancellationException.class)); + expectThrows(CancellationException.class, future::actionGet); } public void testAutomaticCancellationDuringFetchPhase() throws Exception { @@ -142,28 +128,15 @@ void verifyCancellationDuringFetchPhase(String searchAction, Request searchReque List plugins = initBlockFactory(); indexTestData(); - CountDownLatch latch = new CountDownLatch(1); - AtomicReference error = new AtomicReference<>(); - Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() { - @Override - public void onSuccess(Response response) { - latch.countDown(); - } - - @Override - public void onFailure(Exception exception) { - error.set(exception); - latch.countDown(); - } - }); + PlainActionFuture future = PlainActionFuture.newFuture(); + Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, wrapAsRestResponseListener(future)); awaitForBlock(plugins); cancellable.cancel(); ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get); disableBlocks(plugins); - latch.await(); - assertThat(error.get(), instanceOf(CancellationException.class)); + expectThrows(CancellationException.class, future::actionGet); } private static Map readNodesInfo() { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java new file mode 100644 index 0000000000000..ef993ca55f3bb --- /dev/null +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.http.snapshots; + +import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.http.HttpSmokeTestCase; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.snapshots.mockstore.MockRepository; +import org.elasticsearch.test.ESIntegTestCase; + +import java.util.Collection; + +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) +public abstract class AbstractSnapshotRestTestCase extends HttpSmokeTestCase { + + @Override + protected Collection> nodePlugins() { + return CollectionUtils.appendToCopy(super.nodePlugins(), MockRepository.Plugin.class); + } +} diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetSnapshotsCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsCancellationIT.java similarity index 55% rename from qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetSnapshotsCancellationIT.java rename to qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsCancellationIT.java index bef2882bce5eb..cbd27b8f8037c 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetSnapshotsCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsCancellationIT.java @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -package org.elasticsearch.http; +package org.elasticsearch.http.snapshots; import org.apache.http.client.methods.HttpGet; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsAction; @@ -14,32 +14,19 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.plugins.Plugin; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; -import org.elasticsearch.snapshots.SnapshotState; import org.elasticsearch.snapshots.mockstore.MockRepository; -import org.elasticsearch.test.ESIntegTestCase; -import java.util.Collection; import java.util.concurrent.CancellationException; import java.util.concurrent.TimeUnit; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.core.IsEqual.equalTo; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) -public class RestGetSnapshotsCancellationIT extends HttpSmokeTestCase { - - @Override - protected Collection> nodePlugins() { - return CollectionUtils.appendToCopy(super.nodePlugins(), MockRepository.Plugin.class); - } +public class RestGetSnapshotsCancellationIT extends AbstractSnapshotRestTestCase { public void testGetSnapshotsCancellation() throws Exception { internalCluster().startMasterOnlyNode(); @@ -47,35 +34,15 @@ public void testGetSnapshotsCancellation() throws Exception { ensureStableCluster(2); final String repoName = "test-repo"; - assertAcked( - client().admin().cluster().preparePutRepository(repoName) - .setType("mock").setSettings(Settings.builder().put("location", randomRepoPath()))); - - final int snapshotCount = randomIntBetween(1, 5); - for (int i = 0; i < snapshotCount; i++) { - assertEquals( - SnapshotState.SUCCESS, - client().admin().cluster().prepareCreateSnapshot(repoName, "snapshot-" + i).setWaitForCompletion(true) - .get().getSnapshotInfo().state() - ); - } + AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "mock"); + AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5)); final MockRepository repository = AbstractSnapshotIntegTestCase.getRepositoryOnMaster(repoName); repository.setBlockOnAnyFiles(); final Request request = new Request(HttpGet.METHOD_NAME, "/_snapshot/" + repoName + "/*"); - final PlainActionFuture future = new PlainActionFuture<>(); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final PlainActionFuture future = new PlainActionFuture<>(); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); assertThat(future.isDone(), equalTo(false)); awaitTaskWithPrefix(GetSnapshotsAction.NAME); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestSnapshotsStatusCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java similarity index 56% rename from qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestSnapshotsStatusCancellationIT.java rename to qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java index 08429bdbff3de..75d3ca545f3d0 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestSnapshotsStatusCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -package org.elasticsearch.http; +package org.elasticsearch.http.snapshots; import org.apache.http.client.methods.HttpGet; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusAction; @@ -14,32 +14,19 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.plugins.Plugin; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; -import org.elasticsearch.snapshots.SnapshotState; import org.elasticsearch.snapshots.mockstore.MockRepository; -import org.elasticsearch.test.ESIntegTestCase; -import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.CancellationException; import java.util.concurrent.TimeUnit; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) -public class RestSnapshotsStatusCancellationIT extends HttpSmokeTestCase { - - @Override - protected Collection> nodePlugins() { - return CollectionUtils.appendToCopy(super.nodePlugins(), MockRepository.Plugin.class); - } +public class RestSnapshotsStatusCancellationIT extends AbstractSnapshotRestTestCase { public void testSnapshotStatusCancellation() throws Exception { internalCluster().startMasterOnlyNode(); @@ -48,21 +35,9 @@ public void testSnapshotStatusCancellation() throws Exception { createIndex("test-idx"); final String repoName = "test-repo"; - assertAcked( - client().admin().cluster().preparePutRepository(repoName) - .setType("mock").setSettings(Settings.builder().put("location", randomRepoPath()))); - + AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "mock"); final int snapshotCount = randomIntBetween(1, 5); - final Collection snapshotNames = new ArrayList<>(); - for (int i = 0; i < snapshotCount; i++) { - final String snapshotName = "snapshot-" + i; - snapshotNames.add(snapshotName); - assertEquals( - SnapshotState.SUCCESS, - client().admin().cluster().prepareCreateSnapshot(repoName, "snapshot-" + i).setWaitForCompletion(true) - .get().getSnapshotInfo().state() - ); - } + final Collection snapshotNames = AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, snapshotCount); final MockRepository repository = AbstractSnapshotIntegTestCase.getRepositoryOnMaster(repoName); repository.setBlockOnAnyFiles(); @@ -73,18 +48,8 @@ public void testSnapshotStatusCancellation() throws Exception { + String.join(",", randomSubsetOf(randomIntBetween(1, snapshotCount), snapshotNames)) + "/_status" ); - final PlainActionFuture future = new PlainActionFuture<>(); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final PlainActionFuture future = new PlainActionFuture<>(); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); assertFalse(future.isDone()); awaitTaskWithPrefix(SnapshotsStatusAction.NAME); diff --git a/test/framework/src/main/java/org/elasticsearch/action/support/ActionTestUtils.java b/test/framework/src/main/java/org/elasticsearch/action/support/ActionTestUtils.java index 50391a9385671..bd55a0b35035e 100644 --- a/test/framework/src/main/java/org/elasticsearch/action/support/ActionTestUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/action/support/ActionTestUtils.java @@ -11,6 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseListener; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskManager; @@ -55,4 +57,18 @@ public static ActionListener assertNoFailureListener(CheckedConsumer listener) { + return new ResponseListener() { + @Override + public void onSuccess(Response response) { + listener.onResponse(response); + } + + @Override + public void onFailure(Exception exception) { + listener.onFailure(exception); + } + }; + } } diff --git a/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java index 97efdae690d2e..2c48b897dd06f 100644 --- a/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java @@ -7,6 +7,7 @@ */ package org.elasticsearch.snapshots; +import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionListener; @@ -282,6 +283,10 @@ public void unblockNode(final String repository, final String node) { } protected void createRepository(String repoName, String type, Settings.Builder settings, boolean verify) { + createRepository(logger, repoName, type, settings, verify); + } + + public static void createRepository(Logger logger, String repoName, String type, Settings.Builder settings, boolean verify) { logger.info("--> creating or updating repository [{}] [{}]", repoName, type); assertAcked(clusterAdmin().preparePutRepository(repoName) .setVerify(verify) @@ -298,14 +303,18 @@ protected void createRepository(String repoName, String type, Path location) { } protected void createRepository(String repoName, String type) { - createRepository(repoName, type, randomRepositorySettings()); + createRepository(logger, repoName, type); } protected void createRepositoryNoVerify(String repoName, String type) { createRepository(repoName, type, randomRepositorySettings(), false); } - protected Settings.Builder randomRepositorySettings() { + public static void createRepository(Logger logger, String repoName, String type) { + createRepository(logger, repoName, type, randomRepositorySettings(), true); + } + + public static Settings.Builder randomRepositorySettings() { final Settings.Builder settings = Settings.builder(); settings.put("location", randomRepoPath()).put("compress", randomBoolean()); if (rarely()) { @@ -558,7 +567,7 @@ protected ActionFuture startDeleteSnapshot(String repoName return clusterAdmin().prepareDeleteSnapshot(repoName, snapshotName).execute(); } - protected void updateClusterState(final Function updater) throws Exception { + protected static void updateClusterState(final Function updater) throws Exception { final PlainActionFuture future = PlainActionFuture.newFuture(); final ClusterService clusterService = internalCluster().getCurrentMasterNodeInstance(ClusterService.class); clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() { @@ -601,6 +610,10 @@ protected void awaitMasterFinishRepoOperations() throws Exception { } protected List createNSnapshots(String repoName, int count) throws Exception { + return createNSnapshots(logger, repoName, count); + } + + public static List createNSnapshots(Logger logger, String repoName, int count) throws Exception { final PlainActionFuture> allSnapshotsDone = PlainActionFuture.newFuture(); final ActionListener snapshotsListener = new GroupedActionListener<>(allSnapshotsDone, count); final List snapshotNames = new ArrayList<>(count); diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java index ebf355fad9b5a..0c77e50daf27a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -1337,14 +1337,14 @@ protected final void disableAllocation(String... indices) { /** * Returns a random admin client. This client can be pointing to any of the nodes in the cluster. */ - protected AdminClient admin() { + protected static AdminClient admin() { return client().admin(); } /** * Returns a random cluster admin client. This client can be pointing to any of the nodes in the cluster. */ - protected ClusterAdminClient clusterAdmin() { + protected static ClusterAdminClient clusterAdmin() { return admin().cluster(); } @@ -1966,7 +1966,7 @@ public TransportRequestHandler interceptHandler( /** * Returns path to a random directory that can be used to create a temporary file system repo */ - public Path randomRepoPath() { + public static Path randomRepoPath() { if (currentCluster instanceof InternalTestCluster) { return randomRepoPath(((InternalTestCluster) currentCluster).getDefaultSettings()); } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java index aa52fadb0fa17..a24bd4384b742 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java @@ -18,7 +18,6 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -51,6 +50,7 @@ import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled; import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; @@ -82,18 +82,8 @@ public void testCancellation() throws Exception { final String actionName = XPackUsageAction.NAME; final Request request = new Request(HttpGet.METHOD_NAME, "/_xpack/usage"); - final PlainActionFuture future = new PlainActionFuture<>(); - final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - future.onResponse(null); - } - - @Override - public void onFailure(Exception exception) { - future.onFailure(exception); - } - }); + final PlainActionFuture future = new PlainActionFuture<>(); + final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); assertThat(future.isDone(), equalTo(false)); awaitTaskWithPrefix(actionName); diff --git a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java index 6d192810ce1cd..c3cf11ebc97f5 100644 --- a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java +++ b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.eql.action; import org.elasticsearch.action.index.IndexRequestBuilder; +import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseListener; import org.elasticsearch.common.Strings; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; @@ -28,9 +28,8 @@ import java.util.Collection; import java.util.List; import java.util.concurrent.CancellationException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicReference; +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; @@ -108,20 +107,8 @@ public void testRestCancellation() throws Exception { request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader(Task.X_OPAQUE_ID, id)); logger.trace("Preparing search"); - CountDownLatch latch = new CountDownLatch(1); - AtomicReference error = new AtomicReference<>(); - Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { - @Override - public void onSuccess(Response response) { - latch.countDown(); - } - - @Override - public void onFailure(Exception exception) { - error.set(exception); - latch.countDown(); - } - }); + final PlainActionFuture future = PlainActionFuture.newFuture(); + Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future)); logger.trace("Waiting for block to be established"); awaitForBlockedFieldCaps(plugins); @@ -157,8 +144,7 @@ public void onFailure(Exception exception) { assertThat(getNumberOfContexts(plugins), equalTo(0)); disableSearchBlocks(plugins); - latch.await(); - assertThat(error.get(), instanceOf(CancellationException.class)); + expectThrows(CancellationException.class, future::actionGet); } @Override