2020package org .elasticsearch .persistent ;
2121
2222import org .elasticsearch .common .settings .Settings ;
23+ import org .elasticsearch .common .unit .TimeValue ;
2324import org .elasticsearch .common .util .concurrent .BaseFuture ;
2425import org .elasticsearch .plugins .Plugin ;
2526import org .elasticsearch .tasks .TaskId ;
2627import org .elasticsearch .tasks .TaskInfo ;
2728import org .elasticsearch .test .ESIntegTestCase ;
28- import org .elasticsearch .persistent .PersistentTasksService .PersistentTaskOperationListener ;
29+ import org .elasticsearch .persistent .PersistentTasksService .WaitForPersistentTaskStatusListener ;
2930import org .elasticsearch .persistent .TestPersistentTasksPlugin .TestPersistentTasksExecutor ;
3031import org .elasticsearch .persistent .TestPersistentTasksPlugin .TestRequest ;
3132import org .elasticsearch .persistent .TestPersistentTasksPlugin .TestTasksRequestBuilder ;
3435import java .util .Collection ;
3536import java .util .Collections ;
3637import java .util .List ;
38+ import java .util .Objects ;
3739
38- import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
3940import static org .hamcrest .Matchers .empty ;
4041import static org .hamcrest .Matchers .equalTo ;
41- import static org .hamcrest .Matchers .not ;
42- import static org .hamcrest .Matchers .notNullValue ;
4342import static org .hamcrest .Matchers .nullValue ;
4443
4544@ ESIntegTestCase .ClusterScope (scope = ESIntegTestCase .Scope .SUITE , minNumDataNodes = 2 )
@@ -64,7 +63,7 @@ public void cleanup() throws Exception {
6463 assertNoRunningTasks ();
6564 }
6665
67- public static class PersistentTaskOperationFuture extends BaseFuture <Long > implements PersistentTaskOperationListener {
66+ public static class PersistentTaskOperationFuture extends BaseFuture <Long > implements WaitForPersistentTaskStatusListener {
6867
6968 @ Override
7069 public void onResponse (long taskId ) {
@@ -166,7 +165,7 @@ public void testPersistentActionStatusUpdate() throws Exception {
166165 PersistentTasksService persistentTasksService = internalCluster ().getInstance (PersistentTasksService .class );
167166 PersistentTaskOperationFuture future = new PersistentTaskOperationFuture ();
168167 persistentTasksService .createPersistentActionTask (TestPersistentTasksExecutor .NAME , new TestRequest ("Blah" ), future );
169- future .get ();
168+ long taskId = future .get ();
170169
171170 assertBusy (() -> {
172171 // Wait for the task to start
@@ -189,20 +188,30 @@ public void testPersistentActionStatusUpdate() throws Exception {
189188 .get ().getTasks ().size (), equalTo (1 ));
190189
191190 int finalI = i ;
192- assertBusy (() -> {
193- PersistentTasksCustomMetaData tasks = internalCluster ().clusterService ().state ().getMetaData ()
194- .custom (PersistentTasksCustomMetaData .TYPE );
195- assertThat (tasks .tasks ().size (), equalTo (1 ));
196- assertThat (tasks .tasks ().iterator ().next ().getStatus (), notNullValue ());
197- assertThat (tasks .tasks ().iterator ().next ().getStatus ().toString (), equalTo ("{\" phase\" :\" phase " + (finalI + 1 ) + "\" }" ));
198- });
199-
191+ PersistentTaskOperationFuture future1 = new PersistentTaskOperationFuture ();
192+ persistentTasksService .waitForPersistentTaskStatus (taskId ,
193+ task -> task != null && task .isCurrentStatus ()&& task .getStatus ().toString () != null &&
194+ task .getStatus ().toString ().equals ("{\" phase\" :\" phase " + (finalI + 1 ) + "\" }" ),
195+ TimeValue .timeValueSeconds (10 ), future1 );
196+ assertThat (future1 .get (), equalTo (taskId ));
200197 }
201198
199+ PersistentTaskOperationFuture future1 = new PersistentTaskOperationFuture ();
200+ persistentTasksService .waitForPersistentTaskStatus (taskId ,
201+ task -> false , TimeValue .timeValueMillis (10 ), future1 );
202+
203+ expectThrows (Exception .class , future1 ::get );
204+
205+ // Wait for the task to disappear
206+ PersistentTaskOperationFuture future2 = new PersistentTaskOperationFuture ();
207+ persistentTasksService .waitForPersistentTaskStatus (taskId , Objects ::isNull , TimeValue .timeValueSeconds (10 ), future2 );
208+
202209 logger .info ("Completing the running task" );
203210 // Complete the running task and make sure it finishes properly
204211 assertThat (new TestTasksRequestBuilder (client ()).setOperation ("finish" ).setTaskId (firstRunningTask .getTaskId ())
205212 .get ().getTasks ().size (), equalTo (1 ));
213+
214+ assertThat (future2 .get (), equalTo (taskId ));
206215 }
207216
208217
0 commit comments