Skip to content

Commit 13cdc56

Browse files
committed
[Docs] Docs tests should wait for async execution to complete (#28481)
This commit splits the async execution documentation into 2 parts, one for the async method itself and one for the action listener. This allows to add more doc and to use CountDownLatches in doc tests to wait for asynchronous operations to be completed before moving to the next test. It also renames few files. Related to #28457
1 parent ba2ce54 commit 13cdc56

15 files changed

+386
-69
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.action.ActionListener;
2828
import org.elasticsearch.action.DocWriteRequest;
2929
import org.elasticsearch.action.DocWriteResponse;
30+
import org.elasticsearch.action.LatchedActionListener;
3031
import org.elasticsearch.action.bulk.BackoffPolicy;
3132
import org.elasticsearch.action.bulk.BulkItemResponse;
3233
import org.elasticsearch.action.bulk.BulkProcessor;
@@ -60,11 +61,11 @@
6061
import org.elasticsearch.script.ScriptType;
6162
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
6263

63-
import java.io.IOException;
6464
import java.util.Collections;
6565
import java.util.Date;
6666
import java.util.HashMap;
6767
import java.util.Map;
68+
import java.util.concurrent.CountDownLatch;
6869
import java.util.concurrent.TimeUnit;
6970

7071
import static java.util.Collections.emptyMap;
@@ -227,9 +228,8 @@ public void testIndex() throws Exception {
227228
}
228229
{
229230
IndexRequest request = new IndexRequest("posts", "doc", "async").source("field", "value");
230-
231-
// tag::index-execute-async
232-
client.indexAsync(request, new ActionListener<IndexResponse>() {
231+
// tag::index-execute-listener
232+
ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
233233
@Override
234234
public void onResponse(IndexResponse indexResponse) {
235235
// <1>
@@ -239,10 +239,18 @@ public void onResponse(IndexResponse indexResponse) {
239239
public void onFailure(Exception e) {
240240
// <2>
241241
}
242-
});
242+
};
243+
// end::index-execute-listener
244+
245+
// Replace the empty listener by a blocking listener in test
246+
final CountDownLatch latch = new CountDownLatch(1);
247+
listener = new LatchedActionListener<>(listener, latch);
248+
249+
// tag::index-execute-async
250+
client.indexAsync(request, listener); // <1>
243251
// end::index-execute-async
244252

245-
assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async"))));
253+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
246254
}
247255
}
248256

@@ -490,8 +498,8 @@ public void testUpdate() throws Exception {
490498
{
491499
UpdateRequest request = new UpdateRequest("posts", "doc", "async").doc("reason", "async update").docAsUpsert(true);
492500

493-
// tag::update-execute-async
494-
client.updateAsync(request, new ActionListener<UpdateResponse>() {
501+
// tag::update-execute-listener
502+
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
495503
@Override
496504
public void onResponse(UpdateResponse updateResponse) {
497505
// <1>
@@ -501,10 +509,18 @@ public void onResponse(UpdateResponse updateResponse) {
501509
public void onFailure(Exception e) {
502510
// <2>
503511
}
504-
});
512+
};
513+
// end::update-execute-listener
514+
515+
// Replace the empty listener by a blocking listener in test
516+
final CountDownLatch latch = new CountDownLatch(1);
517+
listener = new LatchedActionListener<>(listener, latch);
518+
519+
// tag::update-execute-async
520+
client.updateAsync(request, listener); // <1>
505521
// end::update-execute-async
506522

507-
assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async"))));
523+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
508524
}
509525
}
510526

@@ -602,8 +618,8 @@ public void testDelete() throws Exception {
602618

603619
DeleteRequest request = new DeleteRequest("posts", "doc", "async");
604620

605-
// tag::delete-execute-async
606-
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
621+
// tag::delete-execute-listener
622+
ActionListener<DeleteResponse> listener = new ActionListener<DeleteResponse>() {
607623
@Override
608624
public void onResponse(DeleteResponse deleteResponse) {
609625
// <1>
@@ -613,14 +629,22 @@ public void onResponse(DeleteResponse deleteResponse) {
613629
public void onFailure(Exception e) {
614630
// <2>
615631
}
616-
});
632+
};
633+
// end::delete-execute-listener
634+
635+
// Replace the empty listener by a blocking listener in test
636+
final CountDownLatch latch = new CountDownLatch(1);
637+
listener = new LatchedActionListener<>(listener, latch);
638+
639+
// tag::delete-execute-async
640+
client.deleteAsync(request, listener); // <1>
617641
// end::delete-execute-async
618642

619-
assertBusy(() -> assertFalse(client.exists(new GetRequest("posts", "doc", "async"))));
643+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
620644
}
621645
}
622646

623-
public void testBulk() throws IOException {
647+
public void testBulk() throws Exception {
624648
RestHighLevelClient client = highLevelClient();
625649
{
626650
// tag::bulk-request
@@ -696,8 +720,8 @@ public void testBulk() throws IOException {
696720
request.waitForActiveShards(ActiveShardCount.ALL); // <2>
697721
// end::bulk-request-active-shards
698722

699-
// tag::bulk-execute-async
700-
client.bulkAsync(request, new ActionListener<BulkResponse>() {
723+
// tag::bulk-execute-listener
724+
ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {
701725
@Override
702726
public void onResponse(BulkResponse bulkResponse) {
703727
// <1>
@@ -707,12 +731,22 @@ public void onResponse(BulkResponse bulkResponse) {
707731
public void onFailure(Exception e) {
708732
// <2>
709733
}
710-
});
734+
};
735+
// end::bulk-execute-listener
736+
737+
// Replace the empty listener by a blocking listener in test
738+
final CountDownLatch latch = new CountDownLatch(1);
739+
listener = new LatchedActionListener<>(listener, latch);
740+
741+
// tag::bulk-execute-async
742+
client.bulkAsync(request, listener); // <1>
711743
// end::bulk-execute-async
744+
745+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
712746
}
713747
}
714748

715-
public void testGet() throws IOException {
749+
public void testGet() throws Exception {
716750
RestHighLevelClient client = highLevelClient();
717751
{
718752
String mappings = "{\n" +
@@ -839,8 +873,9 @@ public void testGet() throws IOException {
839873
}
840874
{
841875
GetRequest request = new GetRequest("posts", "doc", "1");
842-
//tag::get-execute-async
843-
client.getAsync(request, new ActionListener<GetResponse>() {
876+
877+
//tag::get-execute-listener
878+
ActionListener<GetResponse> listener = new ActionListener<GetResponse>() {
844879
@Override
845880
public void onResponse(GetResponse getResponse) {
846881
// <1>
@@ -850,8 +885,18 @@ public void onResponse(GetResponse getResponse) {
850885
public void onFailure(Exception e) {
851886
// <2>
852887
}
853-
});
888+
};
889+
//end::get-execute-listener
890+
891+
// Replace the empty listener by a blocking listener in test
892+
final CountDownLatch latch = new CountDownLatch(1);
893+
listener = new LatchedActionListener<>(listener, latch);
894+
895+
//tag::get-execute-async
896+
client.getAsync(request, listener); // <1>
854897
//end::get-execute-async
898+
899+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
855900
}
856901
{
857902
//tag::get-indexnotfound
@@ -879,7 +924,7 @@ public void onFailure(Exception e) {
879924
}
880925
}
881926

882-
public void testBulkProcessor() throws InterruptedException, IOException {
927+
public void testBulkProcessor() throws InterruptedException {
883928
RestHighLevelClient client = highLevelClient();
884929
{
885930
// tag::bulk-processor-init

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.action.ActionListener;
24+
import org.elasticsearch.action.LatchedActionListener;
2425
import org.elasticsearch.action.admin.indices.alias.Alias;
2526
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
2627
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
@@ -33,14 +34,16 @@
3334
import org.elasticsearch.action.support.ActiveShardCount;
3435
import org.elasticsearch.action.support.IndicesOptions;
3536
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
36-
import org.elasticsearch.client.Response;
3737
import org.elasticsearch.client.RestHighLevelClient;
3838
import org.elasticsearch.common.settings.Settings;
3939
import org.elasticsearch.common.unit.TimeValue;
4040
import org.elasticsearch.common.xcontent.XContentType;
4141
import org.elasticsearch.rest.RestStatus;
4242

4343
import java.io.IOException;
44+
import java.util.Map;
45+
import java.util.concurrent.CountDownLatch;
46+
import java.util.concurrent.TimeUnit;
4447

4548
/**
4649
* This class is used to generate the Java Indices API documentation.
@@ -118,8 +121,8 @@ public void testDeleteIndexAsync() throws Exception {
118121
{
119122
DeleteIndexRequest request = new DeleteIndexRequest("posts");
120123

121-
// tag::delete-index-execute-async
122-
client.indices().deleteAsync(request, new ActionListener<DeleteIndexResponse>() {
124+
// tag::delete-index-execute-listener
125+
ActionListener<DeleteIndexResponse> listener = new ActionListener<DeleteIndexResponse>() {
123126
@Override
124127
public void onResponse(DeleteIndexResponse deleteIndexResponse) {
125128
// <1>
@@ -129,14 +132,18 @@ public void onResponse(DeleteIndexResponse deleteIndexResponse) {
129132
public void onFailure(Exception e) {
130133
// <2>
131134
}
132-
});
135+
};
136+
// end::delete-index-execute-listener
137+
138+
// Replace the empty listener by a blocking listener in test
139+
final CountDownLatch latch = new CountDownLatch(1);
140+
listener = new LatchedActionListener<>(listener, latch);
141+
142+
// tag::delete-index-execute-async
143+
client.indices().deleteAsync(request, listener); // <1>
133144
// end::delete-index-execute-async
134145

135-
assertBusy(() -> {
136-
// TODO Use Indices Exist API instead once it exists
137-
Response response = client.getLowLevelClient().performRequest("HEAD", "posts");
138-
assertTrue(RestStatus.NOT_FOUND.getStatus() == response.getStatusLine().getStatusCode());
139-
});
146+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
140147
}
141148
}
142149

@@ -206,8 +213,9 @@ public void testCreateIndexAsync() throws Exception {
206213

207214
{
208215
CreateIndexRequest request = new CreateIndexRequest("twitter");
209-
// tag::create-index-execute-async
210-
client.indices().createAsync(request, new ActionListener<CreateIndexResponse>() {
216+
217+
// tag::create-index-execute-listener
218+
ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
211219
@Override
212220
public void onResponse(CreateIndexResponse createIndexResponse) {
213221
// <1>
@@ -217,14 +225,18 @@ public void onResponse(CreateIndexResponse createIndexResponse) {
217225
public void onFailure(Exception e) {
218226
// <2>
219227
}
220-
});
228+
};
229+
// end::create-index-execute-listener
230+
231+
// Replace the empty listener by a blocking listener in test
232+
final CountDownLatch latch = new CountDownLatch(1);
233+
listener = new LatchedActionListener<>(listener, latch);
234+
235+
// tag::create-index-execute-async
236+
client.indices().createAsync(request, listener); // <1>
221237
// end::create-index-execute-async
222238

223-
assertBusy(() -> {
224-
// TODO Use Indices Exist API instead once it exists
225-
Response response = client.getLowLevelClient().performRequest("HEAD", "twitter");
226-
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
227-
});
239+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
228240
}
229241
}
230242

@@ -269,8 +281,8 @@ public void testOpenIndex() throws Exception {
269281
assertTrue(acknowledged);
270282
assertTrue(shardsAcked);
271283

272-
// tag::open-index-execute-async
273-
client.indices().openAsync(request, new ActionListener<OpenIndexResponse>() {
284+
// tag::open-index-execute-listener
285+
ActionListener<OpenIndexResponse> listener = new ActionListener<OpenIndexResponse>() {
274286
@Override
275287
public void onResponse(OpenIndexResponse openIndexResponse) {
276288
// <1>
@@ -280,14 +292,18 @@ public void onResponse(OpenIndexResponse openIndexResponse) {
280292
public void onFailure(Exception e) {
281293
// <2>
282294
}
283-
});
295+
};
296+
// end::open-index-execute-listener
297+
298+
// Replace the empty listener by a blocking listener in test
299+
final CountDownLatch latch = new CountDownLatch(1);
300+
listener = new LatchedActionListener<>(listener, latch);
301+
302+
// tag::open-index-execute-async
303+
client.indices().openAsync(request, listener); // <1>
284304
// end::open-index-execute-async
285305

286-
assertBusy(() -> {
287-
// TODO Use Indices Exist API instead once it exists
288-
Response response = client.getLowLevelClient().performRequest("HEAD", "index");
289-
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
290-
});
306+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
291307
}
292308

293309
{
@@ -304,7 +320,7 @@ public void onFailure(Exception e) {
304320
}
305321
}
306322

307-
public void testCloseIndex() throws IOException {
323+
public void testCloseIndex() throws Exception {
308324
RestHighLevelClient client = highLevelClient();
309325

310326
{
@@ -339,8 +355,8 @@ public void testCloseIndex() throws IOException {
339355
// end::close-index-response
340356
assertTrue(acknowledged);
341357

342-
// tag::close-index-execute-async
343-
client.indices().closeAsync(request, new ActionListener<CloseIndexResponse>() {
358+
// tag::close-index-execute-listener
359+
ActionListener<CloseIndexResponse> listener = new ActionListener<CloseIndexResponse>() {
344360
@Override
345361
public void onResponse(CloseIndexResponse closeIndexResponse) {
346362
// <1>
@@ -350,8 +366,18 @@ public void onResponse(CloseIndexResponse closeIndexResponse) {
350366
public void onFailure(Exception e) {
351367
// <2>
352368
}
353-
});
369+
};
370+
// end::close-index-execute-listener
371+
372+
// Replace the empty listener by a blocking listener in test
373+
final CountDownLatch latch = new CountDownLatch(1);
374+
listener = new LatchedActionListener<>(listener, latch);
375+
376+
// tag::close-index-execute-async
377+
client.indices().closeAsync(request, listener); // <1>
354378
// end::close-index-execute-async
379+
380+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
355381
}
356382

357383
{

0 commit comments

Comments
 (0)