Skip to content

Commit ba2ce54

Browse files
committed
[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 0a00eb4 commit ba2ce54

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void onFailure(Exception e) {
228228
}
229229
}
230230

231-
public void testOpenIndex() throws IOException {
231+
public void testOpenIndex() throws Exception {
232232
RestHighLevelClient client = highLevelClient();
233233

234234
{
@@ -254,7 +254,6 @@ public void testOpenIndex() throws IOException {
254254
request.waitForActiveShards(ActiveShardCount.ONE); // <2>
255255
// end::open-index-request-waitForActiveShards
256256

257-
258257
// tag::open-index-request-indicesOptions
259258
request.indicesOptions(IndicesOptions.strictExpandOpen()); // <1>
260259
// end::open-index-request-indicesOptions
@@ -283,6 +282,12 @@ public void onFailure(Exception e) {
283282
}
284283
});
285284
// end::open-index-execute-async
285+
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+
});
286291
}
287292

288293
{

0 commit comments

Comments
 (0)