Skip to content

Commit d3fbb3e

Browse files
authored
Fix PersistentTasksClusterServiceTests (#92002)
Today these tests don't wait for the assertions to run before shutting down the master service, and today if the master service shuts down then work (such as these assertions) is silently dropped. This commit ensures that the assertions have run before the test completes.
1 parent 2b268d3 commit d3fbb3e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

server/src/test/java/org/elasticsearch/persistent/PersistentTasksClusterServiceTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Optional;
5151
import java.util.Set;
5252
import java.util.concurrent.CountDownLatch;
53+
import java.util.concurrent.TimeUnit;
5354
import java.util.concurrent.atomic.AtomicBoolean;
5455
import java.util.concurrent.atomic.AtomicReference;
5556
import java.util.stream.Collectors;
@@ -527,7 +528,7 @@ public void testPeriodicRecheckOffMaster() {
527528
assertFalse(service.getPeriodicRechecker().isScheduled());
528529
}
529530

530-
public void testUnassignTask() {
531+
public void testUnassignTask() throws InterruptedException {
531532
ClusterState clusterState = initialState();
532533
ClusterState.Builder builder = ClusterState.builder(clusterState);
533534
PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder(
@@ -545,14 +546,18 @@ public void testUnassignTask() {
545546
clusterState = builder.metadata(metadata).nodes(nodes).build();
546547
setState(clusterService, clusterState);
547548
PersistentTasksClusterService service = createService((params, candidateNodes, currentState) -> new Assignment("_node_2", "test"));
549+
final var countDownLatch = new CountDownLatch(1);
548550
service.unassignPersistentTask(unassignedId, tasks.getLastAllocationId(), "unassignment test", ActionListener.wrap(task -> {
549551
assertThat(task.getAssignment().getExecutorNode(), is(nullValue()));
550552
assertThat(task.getId(), equalTo(unassignedId));
551553
assertThat(task.getAssignment().getExplanation(), equalTo("unassignment test"));
554+
countDownLatch.countDown();
552555
}, e -> fail()));
556+
557+
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
553558
}
554559

555-
public void testUnassignNonExistentTask() {
560+
public void testUnassignNonExistentTask() throws InterruptedException {
556561
ClusterState clusterState = initialState();
557562
ClusterState.Builder builder = ClusterState.builder(clusterState);
558563
PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder(
@@ -568,12 +573,18 @@ public void testUnassignNonExistentTask() {
568573
clusterState = builder.metadata(metadata).nodes(nodes).build();
569574
setState(clusterService, clusterState);
570575
PersistentTasksClusterService service = createService((params, candidateNodes, currentState) -> new Assignment("_node_2", "test"));
576+
final var countDownLatch = new CountDownLatch(1);
571577
service.unassignPersistentTask(
572578
"missing-task",
573579
tasks.getLastAllocationId(),
574580
"unassignment test",
575-
ActionListener.wrap(task -> fail(), e -> assertThat(e, instanceOf(ResourceNotFoundException.class)))
581+
ActionListener.wrap(task -> fail(), e -> {
582+
assertThat(e, instanceOf(ResourceNotFoundException.class));
583+
countDownLatch.countDown();
584+
})
576585
);
586+
587+
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
577588
}
578589

579590
public void testTasksNotAssignedToShuttingDownNodes() {

0 commit comments

Comments
 (0)