Explorar o código

[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
Jim Ferenczi %!s(int64=6) %!d(string=hai) anos
pai
achega
b24dc2c541

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

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

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

@@ -72,7 +72,7 @@ public class TasksIT extends ESRestHighLevelClientTestCase {
         assertTrue("List tasks were not found", listTasksFound);
     }
     
-    public void testGetValidTask() throws IOException {
+    public void testGetValidTask() throws Exception {
 
         // Run a Reindex to create a task
 
@@ -112,7 +112,10 @@ public class TasksIT extends ESRestHighLevelClientTestCase {
         TaskInfo info = taskResponse.getTaskInfo();
         assertTrue(info.isCancellable());
         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 {