Skip to content

Commit a9b782c

Browse files
Dry up HTTP Smoke Tests around Snapshots (#73962) (#74048)
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.
1 parent 556dbee commit a9b782c

File tree

14 files changed

+106
-220
lines changed

14 files changed

+106
-220
lines changed

qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.client.Cancellable;
1313
import org.elasticsearch.client.Request;
1414
import org.elasticsearch.client.Response;
15-
import org.elasticsearch.client.ResponseListener;
1615
import org.elasticsearch.core.Releasable;
1716
import org.elasticsearch.core.Releasables;
1817
import org.elasticsearch.common.settings.Setting;
@@ -41,6 +40,7 @@
4140
import java.util.function.Function;
4241

4342
import static java.util.Collections.singletonList;
43+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
4444
import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled;
4545
import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished;
4646
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
@@ -92,19 +92,9 @@ void runTest(Request request, String actionPrefix) throws Exception {
9292
releasables.add(searcherBlock::release);
9393
}
9494

95-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
95+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
9696
logger.info("--> sending request");
97-
final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() {
98-
@Override
99-
public void onSuccess(Response response) {
100-
future.onResponse(null);
101-
}
102-
103-
@Override
104-
public void onFailure(Exception exception) {
105-
future.onFailure(exception);
106-
}
107-
});
97+
final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future));
10898

10999
awaitTaskWithPrefix(actionPrefix);
110100

qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.client.Cancellable;
1616
import org.elasticsearch.client.Request;
1717
import org.elasticsearch.client.Response;
18-
import org.elasticsearch.client.ResponseListener;
1918
import org.elasticsearch.cluster.AbstractDiffable;
2019
import org.elasticsearch.cluster.ClusterState;
2120
import org.elasticsearch.cluster.ClusterStateUpdateTask;
@@ -33,6 +32,7 @@
3332
import java.util.concurrent.CancellationException;
3433
import java.util.function.UnaryOperator;
3534

35+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
3636
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
3737

3838
public class ClusterStateRestCancellationIT extends HttpSmokeTestCase {
@@ -75,19 +75,9 @@ public void testClusterStateRestCancellation() throws Exception {
7575
clusterStateRequest.addParameter("local", "true");
7676
}
7777

78-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
78+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
7979
logger.info("--> sending cluster state request");
80-
final Cancellable cancellable = getRestClient().performRequestAsync(clusterStateRequest, new ResponseListener() {
81-
@Override
82-
public void onSuccess(Response response) {
83-
future.onResponse(null);
84-
}
85-
86-
@Override
87-
public void onFailure(Exception exception) {
88-
future.onFailure(exception);
89-
}
90-
});
80+
final Cancellable cancellable = getRestClient().performRequestAsync(clusterStateRequest, wrapAsRestResponseListener(future));
9181

9282
awaitTaskWithPrefix(ClusterStateAction.NAME);
9383

qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.client.Cancellable;
1515
import org.elasticsearch.client.Request;
1616
import org.elasticsearch.client.Response;
17-
import org.elasticsearch.client.ResponseListener;
1817
import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings;
1918
import org.elasticsearch.core.Releasable;
2019
import org.elasticsearch.core.Releasables;
@@ -44,6 +43,7 @@
4443
import java.util.function.Function;
4544

4645
import static java.util.Collections.singletonList;
46+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
4747
import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled;
4848
import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished;
4949
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
@@ -100,19 +100,9 @@ public void testClusterStateRestCancellation() throws Exception {
100100

101101
final Request clusterStatsRequest = new Request(HttpGet.METHOD_NAME, "/_cluster/stats");
102102

103-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
103+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
104104
logger.info("--> sending cluster state request");
105-
final Cancellable cancellable = getRestClient().performRequestAsync(clusterStatsRequest, new ResponseListener() {
106-
@Override
107-
public void onSuccess(Response response) {
108-
future.onResponse(null);
109-
}
110-
111-
@Override
112-
public void onFailure(Exception exception) {
113-
future.onFailure(exception);
114-
}
115-
});
105+
final Cancellable cancellable = getRestClient().performRequestAsync(clusterStatsRequest, wrapAsRestResponseListener(future));
116106

117107
awaitTaskWithPrefix(ClusterStatsAction.NAME);
118108

qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesRecoveryRestCancellationIT.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.client.Cancellable;
1717
import org.elasticsearch.client.Request;
1818
import org.elasticsearch.client.Response;
19-
import org.elasticsearch.client.ResponseListener;
2019
import org.elasticsearch.core.Releasable;
2120
import org.elasticsearch.core.Releasables;
2221

@@ -25,6 +24,7 @@
2524
import java.util.concurrent.CancellationException;
2625
import java.util.concurrent.Semaphore;
2726

27+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
2828
import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled;
2929
import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished;
3030
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
@@ -68,19 +68,9 @@ private void runTest(Request request) throws Exception {
6868
releasables.add(operationBlock::release);
6969
}
7070

71-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
71+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
7272
logger.info("--> sending request");
73-
final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() {
74-
@Override
75-
public void onSuccess(Response response) {
76-
future.onResponse(null);
77-
}
78-
79-
@Override
80-
public void onFailure(Exception exception) {
81-
future.onFailure(exception);
82-
}
83-
});
73+
final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future));
8474

8575
awaitTaskWithPrefix(RecoveryAction.NAME);
8676

qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.client.Cancellable;
1616
import org.elasticsearch.client.Request;
1717
import org.elasticsearch.client.Response;
18-
import org.elasticsearch.client.ResponseListener;
1918
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
2019
import org.elasticsearch.cluster.ClusterState;
2120
import org.elasticsearch.cluster.ack.AckedRequest;
@@ -31,6 +30,7 @@
3130
import java.util.concurrent.CancellationException;
3231
import java.util.function.Function;
3332

33+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
3434
import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled;
3535
import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished;
3636
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
@@ -64,18 +64,8 @@ public void testGetMappingsCancellation() throws Exception {
6464
});
6565

6666
final Request request = new Request(HttpGet.METHOD_NAME, "/test/_mappings");
67-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
68-
final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() {
69-
@Override
70-
public void onSuccess(Response response) {
71-
future.onResponse(null);
72-
}
73-
74-
@Override
75-
public void onFailure(Exception exception) {
76-
future.onFailure(exception);
77-
}
78-
});
67+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
68+
final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future));
7969

8070
assertThat(future.isDone(), equalTo(false));
8171
awaitTaskWithPrefix(actionName);

qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import org.elasticsearch.action.search.MultiSearchRequest;
2020
import org.elasticsearch.action.search.SearchAction;
2121
import org.elasticsearch.action.search.SearchRequest;
22+
import org.elasticsearch.action.support.PlainActionFuture;
2223
import org.elasticsearch.action.support.WriteRequest;
2324
import org.elasticsearch.client.Cancellable;
2425
import org.elasticsearch.client.Request;
2526
import org.elasticsearch.client.Response;
26-
import org.elasticsearch.client.ResponseListener;
2727
import org.elasticsearch.common.Strings;
2828
import org.elasticsearch.common.util.CollectionUtils;
2929
import org.elasticsearch.common.xcontent.XContentType;
@@ -49,13 +49,12 @@
4949
import java.util.List;
5050
import java.util.Map;
5151
import java.util.concurrent.CancellationException;
52-
import java.util.concurrent.CountDownLatch;
5352
import java.util.concurrent.TimeUnit;
5453
import java.util.concurrent.atomic.AtomicBoolean;
5554
import java.util.concurrent.atomic.AtomicInteger;
56-
import java.util.concurrent.atomic.AtomicReference;
5755
import java.util.function.Function;
5856

57+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
5958
import static org.elasticsearch.index.query.QueryBuilders.scriptQuery;
6059
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
6160
import static org.hamcrest.Matchers.greaterThan;
@@ -93,28 +92,15 @@ void verifyCancellationDuringQueryPhase(String searchAction, Request searchReque
9392
List<ScriptedBlockPlugin> plugins = initBlockFactory();
9493
indexTestData();
9594

96-
CountDownLatch latch = new CountDownLatch(1);
97-
AtomicReference<Exception> error = new AtomicReference<>();
98-
Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {
99-
@Override
100-
public void onSuccess(Response response) {
101-
latch.countDown();
102-
}
103-
104-
@Override
105-
public void onFailure(Exception exception) {
106-
error.set(exception);
107-
latch.countDown();
108-
}
109-
});
95+
PlainActionFuture<Response> future = PlainActionFuture.newFuture();
96+
Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, wrapAsRestResponseListener(future));
11097

11198
awaitForBlock(plugins);
11299
cancellable.cancel();
113100
ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
114101

115102
disableBlocks(plugins);
116-
latch.await();
117-
assertThat(error.get(), instanceOf(CancellationException.class));
103+
expectThrows(CancellationException.class, future::actionGet);
118104
}
119105

120106
public void testAutomaticCancellationDuringFetchPhase() throws Exception {
@@ -142,28 +128,15 @@ void verifyCancellationDuringFetchPhase(String searchAction, Request searchReque
142128
List<ScriptedBlockPlugin> plugins = initBlockFactory();
143129
indexTestData();
144130

145-
CountDownLatch latch = new CountDownLatch(1);
146-
AtomicReference<Exception> error = new AtomicReference<>();
147-
Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {
148-
@Override
149-
public void onSuccess(Response response) {
150-
latch.countDown();
151-
}
152-
153-
@Override
154-
public void onFailure(Exception exception) {
155-
error.set(exception);
156-
latch.countDown();
157-
}
158-
});
131+
PlainActionFuture<Response> future = PlainActionFuture.newFuture();
132+
Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, wrapAsRestResponseListener(future));
159133

160134
awaitForBlock(plugins);
161135
cancellable.cancel();
162136
ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
163137

164138
disableBlocks(plugins);
165-
latch.await();
166-
assertThat(error.get(), instanceOf(CancellationException.class));
139+
expectThrows(CancellationException.class, future::actionGet);
167140
}
168141

169142
private static Map<String, String> readNodesInfo() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.http.snapshots;
10+
11+
import org.elasticsearch.common.util.CollectionUtils;
12+
import org.elasticsearch.http.HttpSmokeTestCase;
13+
import org.elasticsearch.plugins.Plugin;
14+
import org.elasticsearch.snapshots.mockstore.MockRepository;
15+
import org.elasticsearch.test.ESIntegTestCase;
16+
17+
import java.util.Collection;
18+
19+
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
20+
public abstract class AbstractSnapshotRestTestCase extends HttpSmokeTestCase {
21+
22+
@Override
23+
protected Collection<Class<? extends Plugin>> nodePlugins() {
24+
return CollectionUtils.appendToCopy(super.nodePlugins(), MockRepository.Plugin.class);
25+
}
26+
}

qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetSnapshotsCancellationIT.java renamed to qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsCancellationIT.java

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,43 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.http;
9+
package org.elasticsearch.http.snapshots;
1010

1111
import org.apache.http.client.methods.HttpGet;
1212
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsAction;
1313
import org.elasticsearch.action.support.PlainActionFuture;
1414
import org.elasticsearch.client.Cancellable;
1515
import org.elasticsearch.client.Request;
1616
import org.elasticsearch.client.Response;
17-
import org.elasticsearch.client.ResponseListener;
18-
import org.elasticsearch.common.settings.Settings;
19-
import org.elasticsearch.common.util.CollectionUtils;
20-
import org.elasticsearch.plugins.Plugin;
2117
import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase;
22-
import org.elasticsearch.snapshots.SnapshotState;
2318
import org.elasticsearch.snapshots.mockstore.MockRepository;
24-
import org.elasticsearch.test.ESIntegTestCase;
2519

26-
import java.util.Collection;
2720
import java.util.concurrent.CancellationException;
2821
import java.util.concurrent.TimeUnit;
2922

23+
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
3024
import static org.elasticsearch.test.TaskAssertions.assertAllCancellableTasksAreCancelled;
3125
import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished;
3226
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix;
33-
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
3427
import static org.hamcrest.core.IsEqual.equalTo;
3528

36-
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
37-
public class RestGetSnapshotsCancellationIT extends HttpSmokeTestCase {
38-
39-
@Override
40-
protected Collection<Class<? extends Plugin>> nodePlugins() {
41-
return CollectionUtils.appendToCopy(super.nodePlugins(), MockRepository.Plugin.class);
42-
}
29+
public class RestGetSnapshotsCancellationIT extends AbstractSnapshotRestTestCase {
4330

4431
public void testGetSnapshotsCancellation() throws Exception {
4532
internalCluster().startMasterOnlyNode();
4633
internalCluster().startDataOnlyNode();
4734
ensureStableCluster(2);
4835

4936
final String repoName = "test-repo";
50-
assertAcked(
51-
client().admin().cluster().preparePutRepository(repoName)
52-
.setType("mock").setSettings(Settings.builder().put("location", randomRepoPath())));
53-
54-
final int snapshotCount = randomIntBetween(1, 5);
55-
for (int i = 0; i < snapshotCount; i++) {
56-
assertEquals(
57-
SnapshotState.SUCCESS,
58-
client().admin().cluster().prepareCreateSnapshot(repoName, "snapshot-" + i).setWaitForCompletion(true)
59-
.get().getSnapshotInfo().state()
60-
);
61-
}
37+
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "mock");
38+
AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5));
6239

6340
final MockRepository repository = AbstractSnapshotIntegTestCase.getRepositoryOnMaster(repoName);
6441
repository.setBlockOnAnyFiles();
6542

6643
final Request request = new Request(HttpGet.METHOD_NAME, "/_snapshot/" + repoName + "/*");
67-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
68-
final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() {
69-
@Override
70-
public void onSuccess(Response response) {
71-
future.onResponse(null);
72-
}
73-
74-
@Override
75-
public void onFailure(Exception exception) {
76-
future.onFailure(exception);
77-
}
78-
});
44+
final PlainActionFuture<Response> future = new PlainActionFuture<>();
45+
final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future));
7946

8047
assertThat(future.isDone(), equalTo(false));
8148
awaitTaskWithPrefix(GetSnapshotsAction.NAME);

0 commit comments

Comments
 (0)