Skip to content

Commit 0795bb2

Browse files
kkewweiandrross
andauthored
Fix flaky TransportMultiSearchActionTests.testCancellation (#18099)
* Fix flaky TransportMultiSearchActionTests.testCancellation Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com> * Fix typo Signed-off-by: Andrew Ross <andrross@amazon.com> --------- Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com> Signed-off-by: Andrew Ross <andrross@amazon.com> Co-authored-by: Andrew Ross <andrross@amazon.com>
1 parent 794a33a commit 0795bb2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

server/src/test/java/org/opensearch/action/search/TransportMultiSearchActionTests.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void testDefaultMaxConcurrentSearches() {
293293
assertThat(result, equalTo(1));
294294
}
295295

296-
public void testCancellation() {
296+
public void testCancellation() throws InterruptedException {
297297
// Initialize dependencies of TransportMultiSearchAction
298298
Settings settings = Settings.builder().put("node.name", TransportMultiSearchActionTests.class.getSimpleName()).build();
299299
ActionFilters actionFilters = mock(ActionFilters.class);
@@ -385,22 +385,35 @@ public String getLocalNodeId() {
385385
}
386386
MultiSearchResponse[] responses = new MultiSearchResponse[1];
387387
Exception[] exceptions = new Exception[1];
388+
CountDownLatch executedLatch = new CountDownLatch(1);
388389
parentTask[0] = (CancellableTask) action.execute(multiSearchRequest, new TaskListener<>() {
389390
@Override
390391
public void onResponse(Task task, MultiSearchResponse items) {
391392
responses[0] = items;
393+
executedLatch.countDown();
392394
}
393395

394396
@Override
395397
public void onFailure(Task task, Exception e) {
396398
exceptions[0] = e;
399+
executedLatch.countDown();
397400
}
398401
});
399402
parentTask[0].cancel("Giving up");
400403
canceledLatch.countDown();
401404

402-
assertNull(responses[0]);
403-
assertNull(exceptions[0]);
405+
if (!executedLatch.await(10, TimeUnit.SECONDS)) {
406+
fail("Latch should have counted down");
407+
}
408+
409+
boolean cancelled = false;
410+
for (MultiSearchResponse.Item item : responses[0].getResponses()) {
411+
if (item.isFailure() && item.getFailure().getMessage().contains("Parent task was cancelled")) {
412+
cancelled = true;
413+
break;
414+
}
415+
}
416+
assertTrue(cancelled);
404417
} finally {
405418
assertTrue(OpenSearchTestCase.terminate(threadPool));
406419
}

0 commit comments

Comments
 (0)