Skip to content

Commit ec187ca

Browse files
authored
[Test] Fix CRUDDocumentationIT (#28457)
Similarly to other documentation tests in the high level client, the asynchronous operation like update, index or delete can make the test fail if it sneak in between the middle of another operation. This commit moves the async doc tests to be the last ones executed and adds assert busy loops to ensure that the asynchronous operations are correctly terminated. closes #28446
1 parent 6180680 commit ec187ca

File tree

3 files changed

+85
-55
lines changed

3 files changed

+85
-55
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class RestHighLevelClientExtTests extends ESTestCase {
4444
private RestHighLevelClient restHighLevelClient;
4545

4646
@Before
47-
public void initClient() throws IOException {
47+
public void initClient() {
4848
RestClient restClient = mock(RestClient.class);
4949
restHighLevelClient = new RestHighLevelClientExt(restClient);
5050
}

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

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.elasticsearch.script.Script;
6060
import org.elasticsearch.script.ScriptType;
6161
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
62-
import org.elasticsearch.threadpool.Scheduler;
6362

6463
import java.io.IOException;
6564
import java.util.Collections;
@@ -87,7 +86,7 @@
8786
*/
8887
public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
8988

90-
public void testIndex() throws IOException {
89+
public void testIndex() throws Exception {
9190
RestHighLevelClient client = highLevelClient();
9291

9392
{
@@ -167,20 +166,6 @@ public void testIndex() throws IOException {
167166
}
168167
}
169168
// end::index-response
170-
171-
// tag::index-execute-async
172-
client.indexAsync(request, new ActionListener<IndexResponse>() {
173-
@Override
174-
public void onResponse(IndexResponse indexResponse) {
175-
// <1>
176-
}
177-
178-
@Override
179-
public void onFailure(Exception e) {
180-
// <2>
181-
}
182-
});
183-
// end::index-execute-async
184169
}
185170
{
186171
IndexRequest request = new IndexRequest("posts", "doc", "1");
@@ -240,9 +225,28 @@ public void onFailure(Exception e) {
240225
}
241226
// end::index-optype
242227
}
228+
{
229+
IndexRequest request = new IndexRequest("posts", "doc", "async").source("field", "value");
230+
231+
// tag::index-execute-async
232+
client.indexAsync(request, new ActionListener<IndexResponse>() {
233+
@Override
234+
public void onResponse(IndexResponse indexResponse) {
235+
// <1>
236+
}
237+
238+
@Override
239+
public void onFailure(Exception e) {
240+
// <2>
241+
}
242+
});
243+
// end::index-execute-async
244+
245+
assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async"))));
246+
}
243247
}
244248

245-
public void testUpdate() throws IOException {
249+
public void testUpdate() throws Exception {
246250
RestHighLevelClient client = highLevelClient();
247251
{
248252
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source("field", 0);
@@ -378,20 +382,6 @@ public void testUpdate() throws IOException {
378382
}
379383
}
380384
// end::update-failure
381-
382-
// tag::update-execute-async
383-
client.updateAsync(request, new ActionListener<UpdateResponse>() {
384-
@Override
385-
public void onResponse(UpdateResponse updateResponse) {
386-
// <1>
387-
}
388-
389-
@Override
390-
public void onFailure(Exception e) {
391-
// <2>
392-
}
393-
});
394-
// end::update-execute-async
395385
}
396386
{
397387
//tag::update-docnotfound
@@ -497,9 +487,28 @@ public void onFailure(Exception e) {
497487
request.waitForActiveShards(ActiveShardCount.ALL); // <2>
498488
// end::update-request-active-shards
499489
}
490+
{
491+
UpdateRequest request = new UpdateRequest("posts", "doc", "async").doc("reason", "async update").docAsUpsert(true);
492+
493+
// tag::update-execute-async
494+
client.updateAsync(request, new ActionListener<UpdateResponse>() {
495+
@Override
496+
public void onResponse(UpdateResponse updateResponse) {
497+
// <1>
498+
}
499+
500+
@Override
501+
public void onFailure(Exception e) {
502+
// <2>
503+
}
504+
});
505+
// end::update-execute-async
506+
507+
assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async"))));
508+
}
500509
}
501510

502-
public void testDelete() throws IOException {
511+
public void testDelete() throws Exception {
503512
RestHighLevelClient client = highLevelClient();
504513

505514
{
@@ -536,20 +545,6 @@ public void testDelete() throws IOException {
536545
}
537546
}
538547
// end::delete-response
539-
540-
// tag::delete-execute-async
541-
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
542-
@Override
543-
public void onResponse(DeleteResponse deleteResponse) {
544-
// <1>
545-
}
546-
547-
@Override
548-
public void onFailure(Exception e) {
549-
// <2>
550-
}
551-
});
552-
// end::delete-execute-async
553548
}
554549

555550
{
@@ -601,6 +596,28 @@ public void onFailure(Exception e) {
601596
}
602597
// end::delete-conflict
603598
}
599+
{
600+
IndexResponse indexResponse = client.index(new IndexRequest("posts", "doc", "async").source("field", "value"));
601+
assertSame(indexResponse.status(), RestStatus.CREATED);
602+
603+
DeleteRequest request = new DeleteRequest("posts", "doc", "async");
604+
605+
// tag::delete-execute-async
606+
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
607+
@Override
608+
public void onResponse(DeleteResponse deleteResponse) {
609+
// <1>
610+
}
611+
612+
@Override
613+
public void onFailure(Exception e) {
614+
// <2>
615+
}
616+
});
617+
// end::delete-execute-async
618+
619+
assertBusy(() -> assertFalse(client.exists(new GetRequest("posts", "doc", "async"))));
620+
}
604621
}
605622

606623
public void testBulk() throws IOException {

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.action.ActionListener;
2424
import org.elasticsearch.action.admin.indices.alias.Alias;
25-
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
26-
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
27-
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
2825
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
2926
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
3027
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
28+
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
29+
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
30+
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
3131
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3232
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
3333
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -363,6 +363,12 @@ public void onFailure(Exception e) {
363363
}
364364
});
365365
// end::open-index-execute-async
366+
367+
assertBusy(() -> {
368+
// TODO Use Indices Exist API instead once it exists
369+
Response response = client.getLowLevelClient().performRequest("HEAD", "index");
370+
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
371+
});
366372
}
367373

368374
{
@@ -483,7 +489,7 @@ public void onFailure(Exception e) {
483489
}
484490
}
485491

486-
public void testUpdateAliases() throws IOException {
492+
public void testUpdateAliases() throws Exception {
487493
RestHighLevelClient client = highLevelClient();
488494

489495
{
@@ -506,9 +512,9 @@ public void testUpdateAliases() throws IOException {
506512

507513
// tag::update-aliases-request2
508514
AliasActions addIndexAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("alias1")
509-
.filter("{\"term\":{\"year\":2016}}"); // <1>
515+
.filter("{\"term\":{\"year\":2016}}"); // <1>
510516
AliasActions addIndicesAction = new AliasActions(AliasActions.Type.ADD).indices("index1", "index2").alias("alias2")
511-
.routing("1"); // <2>
517+
.routing("1"); // <2>
512518
AliasActions removeAction = new AliasActions(AliasActions.Type.REMOVE).index("index3").alias("alias3"); // <3>
513519
AliasActions removeIndexAction = new AliasActions(AliasActions.Type.REMOVE_INDEX).index("index4"); // <4>
514520
// end::update-aliases-request2
@@ -530,11 +536,16 @@ public void testUpdateAliases() throws IOException {
530536
boolean acknowledged = indicesAliasesResponse.isAcknowledged(); // <1>
531537
// end::update-aliases-response
532538
assertTrue(acknowledged);
539+
}
540+
{
541+
IndicesAliasesRequest request = new IndicesAliasesRequest(); // <1>
542+
AliasActions aliasAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("async"); // <2>
543+
request.addAliasAction(aliasAction);
533544

534545
// tag::update-aliases-execute-async
535546
client.indices().updateAliasesAsync(request, new ActionListener<IndicesAliasesResponse>() {
536547
@Override
537-
public void onResponse(IndicesAliasesResponse indciesAliasesResponse) {
548+
public void onResponse(IndicesAliasesResponse indicesAliasesResponse) {
538549
// <1>
539550
}
540551

@@ -544,6 +555,8 @@ public void onFailure(Exception e) {
544555
}
545556
});
546557
// end::update-aliases-execute-async
558+
559+
assertBusy(() -> assertTrue(client.indices().existsAlias(new GetAliasesRequest("async"))));
547560
}
548-
}
561+
}
549562
}

0 commit comments

Comments
 (0)