From ec187caeae29ce38c27adfb2403a4da793c6af1e Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Thu, 1 Feb 2018 16:30:17 +0100 Subject: [PATCH] [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 --- .../client/RestHighLevelClientExtTests.java | 2 +- .../documentation/CRUDDocumentationIT.java | 109 ++++++++++-------- .../IndicesClientDocumentationIT.java | 29 +++-- 3 files changed, 85 insertions(+), 55 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java index b5fb98a3bdf..970ffb15f08 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java @@ -44,7 +44,7 @@ public class RestHighLevelClientExtTests extends ESTestCase { private RestHighLevelClient restHighLevelClient; @Before - public void initClient() throws IOException { + public void initClient() { RestClient restClient = mock(RestClient.class); restHighLevelClient = new RestHighLevelClientExt(restClient); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java index b32351656ca..453441ea97f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java @@ -59,7 +59,6 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; -import org.elasticsearch.threadpool.Scheduler; import java.io.IOException; import java.util.Collections; @@ -87,7 +86,7 @@ import static java.util.Collections.singletonMap; */ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { - public void testIndex() throws IOException { + public void testIndex() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -167,20 +166,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { } } // end::index-response - - // tag::index-execute-async - client.indexAsync(request, new ActionListener() { - @Override - public void onResponse(IndexResponse indexResponse) { - // <1> - } - - @Override - public void onFailure(Exception e) { - // <2> - } - }); - // end::index-execute-async } { IndexRequest request = new IndexRequest("posts", "doc", "1"); @@ -240,9 +225,28 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { } // end::index-optype } + { + IndexRequest request = new IndexRequest("posts", "doc", "async").source("field", "value"); + + // tag::index-execute-async + client.indexAsync(request, new ActionListener() { + @Override + public void onResponse(IndexResponse indexResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }); + // end::index-execute-async + + assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async")))); + } } - public void testUpdate() throws IOException { + public void testUpdate() throws Exception { RestHighLevelClient client = highLevelClient(); { IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source("field", 0); @@ -378,20 +382,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { } } // end::update-failure - - // tag::update-execute-async - client.updateAsync(request, new ActionListener() { - @Override - public void onResponse(UpdateResponse updateResponse) { - // <1> - } - - @Override - public void onFailure(Exception e) { - // <2> - } - }); - // end::update-execute-async } { //tag::update-docnotfound @@ -497,9 +487,28 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { request.waitForActiveShards(ActiveShardCount.ALL); // <2> // end::update-request-active-shards } + { + UpdateRequest request = new UpdateRequest("posts", "doc", "async").doc("reason", "async update").docAsUpsert(true); + + // tag::update-execute-async + client.updateAsync(request, new ActionListener() { + @Override + public void onResponse(UpdateResponse updateResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }); + // end::update-execute-async + + assertBusy(() -> assertTrue(client.exists(new GetRequest("posts", "doc", "async")))); + } } - public void testDelete() throws IOException { + public void testDelete() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -536,20 +545,6 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { } } // end::delete-response - - // tag::delete-execute-async - client.deleteAsync(request, new ActionListener() { - @Override - public void onResponse(DeleteResponse deleteResponse) { - // <1> - } - - @Override - public void onFailure(Exception e) { - // <2> - } - }); - // end::delete-execute-async } { @@ -601,6 +596,28 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase { } // end::delete-conflict } + { + IndexResponse indexResponse = client.index(new IndexRequest("posts", "doc", "async").source("field", "value")); + assertSame(indexResponse.status(), RestStatus.CREATED); + + DeleteRequest request = new DeleteRequest("posts", "doc", "async"); + + // tag::delete-execute-async + client.deleteAsync(request, new ActionListener() { + @Override + public void onResponse(DeleteResponse deleteResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }); + // end::delete-execute-async + + assertBusy(() -> assertFalse(client.exists(new GetRequest("posts", "doc", "async")))); + } } public void testBulk() throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 43661c96c5b..1ea66ffe70b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -22,12 +22,12 @@ package org.elasticsearch.client.documentation; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.alias.Alias; -import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; -import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; -import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; +import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; +import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; @@ -363,6 +363,12 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase } }); // end::open-index-execute-async + + assertBusy(() -> { + // TODO Use Indices Exist API instead once it exists + Response response = client.getLowLevelClient().performRequest("HEAD", "index"); + assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); + }); } { @@ -483,7 +489,7 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase } } - public void testUpdateAliases() throws IOException { + public void testUpdateAliases() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -506,9 +512,9 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase // tag::update-aliases-request2 AliasActions addIndexAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("alias1") - .filter("{\"term\":{\"year\":2016}}"); // <1> + .filter("{\"term\":{\"year\":2016}}"); // <1> AliasActions addIndicesAction = new AliasActions(AliasActions.Type.ADD).indices("index1", "index2").alias("alias2") - .routing("1"); // <2> + .routing("1"); // <2> AliasActions removeAction = new AliasActions(AliasActions.Type.REMOVE).index("index3").alias("alias3"); // <3> AliasActions removeIndexAction = new AliasActions(AliasActions.Type.REMOVE_INDEX).index("index4"); // <4> // end::update-aliases-request2 @@ -530,11 +536,16 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase boolean acknowledged = indicesAliasesResponse.isAcknowledged(); // <1> // end::update-aliases-response assertTrue(acknowledged); + } + { + IndicesAliasesRequest request = new IndicesAliasesRequest(); // <1> + AliasActions aliasAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("async"); // <2> + request.addAliasAction(aliasAction); // tag::update-aliases-execute-async client.indices().updateAliasesAsync(request, new ActionListener() { @Override - public void onResponse(IndicesAliasesResponse indciesAliasesResponse) { + public void onResponse(IndicesAliasesResponse indicesAliasesResponse) { // <1> } @@ -544,6 +555,8 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase } }); // end::update-aliases-execute-async + + assertBusy(() -> assertTrue(client.indices().existsAlias(new GetAliasesRequest("async")))); } - } + } }