Skip to content

Commit b24dc2c

Browse files
authored
[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
1 parent 3e73911 commit b24dc2c

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.action.index.IndexRequest;
2424
import org.elasticsearch.action.support.WriteRequest;
2525
import org.elasticsearch.client.tasks.TaskSubmissionResponse;
26+
import org.elasticsearch.common.CheckedRunnable;
2627
import org.elasticsearch.common.settings.Settings;
2728
import org.elasticsearch.common.xcontent.XContentType;
2829
import org.elasticsearch.index.query.IdsQueryBuilder;
@@ -32,7 +33,6 @@
3233

3334
import java.io.IOException;
3435
import java.util.Collections;
35-
import java.util.function.BooleanSupplier;
3636

3737
public class ReindexIT extends ESRestHighLevelClientTestCase {
3838

@@ -82,7 +82,7 @@ public void testReindex() throws IOException {
8282
}
8383
}
8484

85-
public void testReindexTask() throws IOException, InterruptedException {
85+
public void testReindexTask() throws Exception {
8686
final String sourceIndex = "source123";
8787
final String destinationIndex = "dest2";
8888
{
@@ -118,20 +118,14 @@ public void testReindexTask() throws IOException, InterruptedException {
118118
String taskId = reindexSubmission.getTask(); // <3>
119119
// end::submit-reindex-task
120120

121-
BooleanSupplier hasUpgradeCompleted = checkCompletionStatus(taskId);
122-
awaitBusy(hasUpgradeCompleted);
121+
assertBusy(checkCompletionStatus(client(), taskId));
123122
}
124123
}
125124

126-
private BooleanSupplier checkCompletionStatus(String taskId) {
125+
static CheckedRunnable<Exception> checkCompletionStatus(RestClient client, String taskId) {
127126
return () -> {
128-
try {
129-
Response response = client().performRequest(new Request("GET", "/_tasks/" + taskId));
130-
return (boolean) entityAsMap(response).get("completed");
131-
} catch (IOException e) {
132-
fail(e.getMessage());
133-
return false;
134-
}
127+
Response response = client.performRequest(new Request("GET", "/_tasks/" + taskId));
128+
assertTrue((boolean) entityAsMap(response).get("completed"));
135129
};
136130
}
137131
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testListTasks() throws IOException {
7272
assertTrue("List tasks were not found", listTasksFound);
7373
}
7474

75-
public void testGetValidTask() throws IOException {
75+
public void testGetValidTask() throws Exception {
7676

7777
// Run a Reindex to create a task
7878

@@ -112,7 +112,10 @@ public void testGetValidTask() throws IOException {
112112
TaskInfo info = taskResponse.getTaskInfo();
113113
assertTrue(info.isCancellable());
114114
assertEquals("reindex from [source1] to [dest][_doc]", info.getDescription());
115-
assertEquals("indices:data/write/reindex", info.getAction());
115+
assertEquals("indices:data/write/reindex", info.getAction());
116+
if (taskResponse.isCompleted() == false) {
117+
assertBusy(ReindexIT.checkCompletionStatus(client(), taskId.toString()));
118+
}
116119
}
117120

118121
public void testGetInvalidTask() throws IOException {

0 commit comments

Comments
 (0)