[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
This commit is contained in:
Tanguy Leroux 2017-12-20 09:31:10 +01:00 committed by GitHub
parent 0779af6dd2
commit 0f80e7c5f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 14 deletions

View File

@ -29,6 +29,7 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
@ -86,6 +87,32 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
boolean acknowledged = deleteIndexResponse.isAcknowledged(); // <1>
// end::delete-index-response
assertTrue(acknowledged);
}
{
// tag::delete-index-notfound
try {
DeleteIndexRequest request = new DeleteIndexRequest("does_not_exist");
client.indices().deleteIndex(request);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) {
// <1>
}
}
// end::delete-index-notfound
}
}
public void testDeleteIndexAsync() throws Exception {
final RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().createIndex(new CreateIndexRequest("posts"));
assertTrue(createIndexResponse.isAcknowledged());
}
{
DeleteIndexRequest request = new DeleteIndexRequest("posts");
// tag::delete-index-execute-async
client.indices().deleteIndexAsync(request, new ActionListener<DeleteIndexResponse>() {
@ -100,19 +127,12 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
}
});
// end::delete-index-execute-async
}
{
// tag::delete-index-notfound
try {
DeleteIndexRequest request = new DeleteIndexRequest("does_not_exist");
client.indices().deleteIndex(request);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) {
// <1>
}
}
// end::delete-index-notfound
assertBusy(() -> {
// TODO Use Indices Exist API instead once it exists
Response response = client.getLowLevelClient().performRequest("HEAD", "posts");
assertTrue(RestStatus.NOT_FOUND.getStatus() == response.getStatusLine().getStatusCode());
});
}
}
@ -174,6 +194,14 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
// end::create-index-response
assertTrue(acknowledged);
assertTrue(shardsAcked);
}
}
public void testCreateIndexAsync() throws Exception {
final RestHighLevelClient client = highLevelClient();
{
CreateIndexRequest request = new CreateIndexRequest("twitter");
// tag::create-index-execute-async
client.indices().createIndexAsync(request, new ActionListener<CreateIndexResponse>() {
@ -188,6 +216,13 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
}
});
// end::create-index-execute-async
assertBusy(() -> {
// TODO Use Indices Exist API instead once it exists
Response response = client.getLowLevelClient().performRequest("HEAD", "twitter");
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
});
}
}
}

View File

@ -102,7 +102,6 @@ public class MigrationDocumentationIT extends ESRestHighLevelClientTestCase {
}
}
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/27754")
public void testClusterHealth() throws IOException {
RestHighLevelClient client = highLevelClient();
{

View File

@ -259,7 +259,7 @@ public abstract class ESRestTestCase extends ESTestCase {
*/
private void logIfThereAreRunningTasks() throws InterruptedException, IOException {
Set<String> runningTasks = runningTasks(adminClient().performRequest("GET", "_tasks"));
// Ignore the task list API - it doens't count against us
// Ignore the task list API - it doesn't count against us
runningTasks.remove(ListTasksAction.NAME);
runningTasks.remove(ListTasksAction.NAME + "[n]");
if (runningTasks.isEmpty()) {