Skip to content

Commit 89504a2

Browse files
authored
[Test] Wait for cancellation before assertions (#134532) (#134868)
Resolves: #134277 (cherry picked from commit 2ea81d0) # Conflicts: # muted-tests.yml
1 parent c55e880 commit 89504a2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

server/src/test/java/org/elasticsearch/action/support/nodes/TransportNodesActionTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.List;
5959
import java.util.Map;
6060
import java.util.Set;
61+
import java.util.concurrent.CountDownLatch;
6162
import java.util.concurrent.CyclicBarrier;
6263
import java.util.concurrent.Executor;
6364
import java.util.concurrent.TimeUnit;
@@ -377,7 +378,13 @@ protected void newResponseAsync(
377378
public void testConcurrentlyCompletionAndCancellation() throws InterruptedException {
378379
final var action = getTestTransportNodesAction();
379380

380-
final CancellableTask cancellableTask = new CancellableTask(randomLong(), "transport", "action", "", null, emptyMap());
381+
final CountDownLatch onCancelledLatch = new CountDownLatch(1);
382+
final CancellableTask cancellableTask = new CancellableTask(randomLong(), "transport", "action", "", null, emptyMap()) {
383+
@Override
384+
protected void onCancelled() {
385+
onCancelledLatch.countDown();
386+
}
387+
};
381388

382389
final PlainActionFuture<TestNodesResponse> future = new PlainActionFuture<>();
383390
action.execute(cancellableTask, new TestNodesRequest(), future);
@@ -414,6 +421,11 @@ public void testConcurrentlyCompletionAndCancellation() throws InterruptedExcept
414421
assertNotNull("expect task cancellation exception, but got\n" + ExceptionsHelper.stackTrace(e), taskCancelledException);
415422
assertThat(e.getMessage(), containsString("task cancelled [simulated]"));
416423
assertTrue(cancellableTask.isCancelled());
424+
// Wait for the latch, the listener for releasing node responses is called before it.
425+
// We need to wait for the latch because the cancellation may be detected in CancellableFanOut#onCompletion with
426+
// the responseHandled flag being true. The flag is set by the cancellation listener which is still in process of
427+
// draining existing responses.
428+
safeAwait(onCancelledLatch);
417429
// All previously captured responses are released due to cancellation
418430
assertTrue(nodeResponses.stream().allMatch(r -> r.hasReferences() == false));
419431
// Wait for the last response to be gathered and assert it is also released by either the concurrent cancellation or

0 commit comments

Comments
 (0)