Skip to content

Commit ef23eea

Browse files
committed
[Test] Fix IndicesClientDocumentationIT (#27899)
The last operation executed in IndicesClientDocumentationIT.testCreate() is an asynchronous index creation. Because nothing waits for its completion, on slow machines the index can sometimes be created after the testCreate() test is finished, and it can fail the following test. Closes #27754
1 parent b122fc4 commit ef23eea

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

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

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,50 +61,33 @@ public void testDeleteIndex() throws IOException {
6161
DeleteIndexRequest request = new DeleteIndexRequest("posts"); // <1>
6262
// end::delete-index-request
6363

64-
// tag::delete-index-execute
65-
DeleteIndexResponse deleteIndexResponse = client.indices().deleteIndex(request);
66-
// end::delete-index-execute
67-
assertTrue(deleteIndexResponse.isAcknowledged());
68-
69-
// tag::delete-index-response
70-
boolean acknowledged = deleteIndexResponse.isAcknowledged(); // <1>
71-
// end::delete-index-response
72-
73-
// tag::delete-index-execute-async
74-
client.indices().deleteIndexAsync(request, new ActionListener<DeleteIndexResponse>() {
75-
@Override
76-
public void onResponse(DeleteIndexResponse deleteIndexResponse) {
77-
// <1>
78-
}
79-
80-
@Override
81-
public void onFailure(Exception e) {
82-
// <2>
83-
}
84-
});
85-
// end::delete-index-execute-async
86-
}
87-
88-
{
89-
DeleteIndexRequest request = new DeleteIndexRequest("posts");
9064
// tag::delete-index-request-timeout
9165
request.timeout(TimeValue.timeValueMinutes(2)); // <1>
9266
request.timeout("2m"); // <2>
9367
// end::delete-index-request-timeout
9468
// tag::delete-index-request-masterTimeout
9569
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
96-
request.timeout("1m"); // <2>
70+
request.masterNodeTimeout("1m"); // <2>
9771
// end::delete-index-request-masterTimeout
9872
// tag::delete-index-request-indicesOptions
9973
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
10074
// end::delete-index-request-indicesOptions
75+
76+
// tag::delete-index-execute
77+
DeleteIndexResponse deleteIndexResponse = client.indices().deleteIndex(request);
78+
// end::delete-index-execute
79+
80+
// tag::delete-index-response
81+
boolean acknowledged = deleteIndexResponse.isAcknowledged(); // <1>
82+
// end::delete-index-response
83+
assertTrue(acknowledged);
10184
}
10285

10386
{
10487
// tag::delete-index-notfound
10588
try {
10689
DeleteIndexRequest request = new DeleteIndexRequest("does_not_exist");
107-
DeleteIndexResponse deleteIndexResponse = client.indices().deleteIndex(request);
90+
client.indices().deleteIndex(request);
10891
} catch (ElasticsearchException exception) {
10992
if (exception.status() == RestStatus.NOT_FOUND) {
11093
// <1>
@@ -113,4 +96,37 @@ public void onFailure(Exception e) {
11396
// end::delete-index-notfound
11497
}
11598
}
99+
100+
public void testDeleteIndexAsync() throws Exception {
101+
final RestHighLevelClient client = highLevelClient();
102+
103+
{
104+
Response createIndexResponse = client().performRequest("PUT", "/posts");
105+
assertEquals(200, createIndexResponse.getStatusLine().getStatusCode());
106+
}
107+
108+
{
109+
DeleteIndexRequest request = new DeleteIndexRequest("posts");
110+
111+
// tag::delete-index-execute-async
112+
client.indices().deleteIndexAsync(request, new ActionListener<DeleteIndexResponse>() {
113+
@Override
114+
public void onResponse(DeleteIndexResponse deleteIndexResponse) {
115+
// <1>
116+
}
117+
118+
@Override
119+
public void onFailure(Exception e) {
120+
// <2>
121+
}
122+
});
123+
// end::delete-index-execute-async
124+
125+
assertBusy(() -> {
126+
// TODO Use Indices Exist API instead once it exists
127+
Response response = client.getLowLevelClient().performRequest("HEAD", "posts");
128+
assertTrue(RestStatus.NOT_FOUND.getStatus() == response.getStatusLine().getStatusCode());
129+
});
130+
}
131+
}
116132
}

0 commit comments

Comments
 (0)