From 0c0ad630d21590cffbfa5f9b709f0594776292e1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 29 Jan 2025 22:54:46 +0000 Subject: [PATCH] Fix flaky TransportMultiSearchActionTests.testCancellation (#17193) I recently added this test, but incorrectly placed a CountDownLatch#await call on the test thread. With this change, we actually kick off the request, return control to the testy thread, cancel the request, then continue executing. Signed-off-by: Michael Froh (cherry picked from commit 6f644e1c12de709b2136b999c8cb112a27c62100) Signed-off-by: github-actions[bot] --- .../TransportMultiSearchActionTests.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/server/src/test/java/org/opensearch/action/search/TransportMultiSearchActionTests.java b/server/src/test/java/org/opensearch/action/search/TransportMultiSearchActionTests.java index 45980e7137ce4..f969313b71833 100644 --- a/server/src/test/java/org/opensearch/action/search/TransportMultiSearchActionTests.java +++ b/server/src/test/java/org/opensearch/action/search/TransportMultiSearchActionTests.java @@ -321,11 +321,8 @@ public TaskManager getTaskManager() { // and if there are more searches than is allowed create an error and remember that. int maxAllowedConcurrentSearches = 1; // Allow 1 search at a time. AtomicInteger counter = new AtomicInteger(); - AtomicReference errorHolder = new AtomicReference<>(); - // randomize whether or not requests are executed asynchronously ExecutorService executorService = threadPool.executor(ThreadPool.Names.GENERIC); - final Set requests = Collections.newSetFromMap(Collections.synchronizedMap(new IdentityHashMap<>())); - CountDownLatch countDownLatch = new CountDownLatch(1); + CountDownLatch canceledLatch = new CountDownLatch(1); CancellableTask[] parentTask = new CancellableTask[1]; NodeClient client = new NodeClient(settings, threadPool) { @Override @@ -333,14 +330,15 @@ public void search(final SearchRequest request, final ActionListener { + try { + if (!canceledLatch.await(1, TimeUnit.SECONDS)) { + fail("Latch should have counted down"); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } counter.decrementAndGet(); listener.onResponse( new SearchResponse( @@ -399,7 +397,7 @@ public void onFailure(Task task, Exception e) { } }); parentTask[0].cancel("Giving up"); - countDownLatch.countDown(); + canceledLatch.countDown(); assertNull(responses[0]); assertNull(exceptions[0]);