[TEST] Awaits tasks termination in the RestHighLevelClient tests (#37302)

This change ensures that TasksIT#testGetValidTask and ReindexIT#testReindexTask
don't leave a non-completed task on the cluster when they finish.

Closes #35644
This commit is contained in:
Jim Ferenczi 2019-01-11 09:47:46 +01:00 committed by GitHub
parent 3e73911cbe
commit b24dc2c541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 14 deletions

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.tasks.TaskSubmissionResponse; import org.elasticsearch.client.tasks.TaskSubmissionResponse;
import org.elasticsearch.common.CheckedRunnable;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.query.IdsQueryBuilder;
@ -32,7 +33,6 @@ import org.elasticsearch.rest.RestStatus;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.function.BooleanSupplier;
public class ReindexIT extends ESRestHighLevelClientTestCase { public class ReindexIT extends ESRestHighLevelClientTestCase {
@ -82,7 +82,7 @@ public class ReindexIT extends ESRestHighLevelClientTestCase {
} }
} }
public void testReindexTask() throws IOException, InterruptedException { public void testReindexTask() throws Exception {
final String sourceIndex = "source123"; final String sourceIndex = "source123";
final String destinationIndex = "dest2"; final String destinationIndex = "dest2";
{ {
@ -118,20 +118,14 @@ public class ReindexIT extends ESRestHighLevelClientTestCase {
String taskId = reindexSubmission.getTask(); // <3> String taskId = reindexSubmission.getTask(); // <3>
// end::submit-reindex-task // end::submit-reindex-task
BooleanSupplier hasUpgradeCompleted = checkCompletionStatus(taskId); assertBusy(checkCompletionStatus(client(), taskId));
awaitBusy(hasUpgradeCompleted);
} }
} }
private BooleanSupplier checkCompletionStatus(String taskId) { static CheckedRunnable<Exception> checkCompletionStatus(RestClient client, String taskId) {
return () -> { return () -> {
try { Response response = client.performRequest(new Request("GET", "/_tasks/" + taskId));
Response response = client().performRequest(new Request("GET", "/_tasks/" + taskId)); assertTrue((boolean) entityAsMap(response).get("completed"));
return (boolean) entityAsMap(response).get("completed");
} catch (IOException e) {
fail(e.getMessage());
return false;
}
}; };
} }
} }

View File

@ -72,7 +72,7 @@ public class TasksIT extends ESRestHighLevelClientTestCase {
assertTrue("List tasks were not found", listTasksFound); assertTrue("List tasks were not found", listTasksFound);
} }
public void testGetValidTask() throws IOException { public void testGetValidTask() throws Exception {
// Run a Reindex to create a task // Run a Reindex to create a task
@ -113,6 +113,9 @@ public class TasksIT extends ESRestHighLevelClientTestCase {
assertTrue(info.isCancellable()); assertTrue(info.isCancellable());
assertEquals("reindex from [source1] to [dest][_doc]", info.getDescription()); assertEquals("reindex from [source1] to [dest][_doc]", info.getDescription());
assertEquals("indices:data/write/reindex", info.getAction()); assertEquals("indices:data/write/reindex", info.getAction());
if (taskResponse.isCompleted() == false) {
assertBusy(ReindexIT.checkCompletionStatus(client(), taskId.toString()));
}
} }
public void testGetInvalidTask() throws IOException { public void testGetInvalidTask() throws IOException {