From fa60aa2ba8694707650f45d94d85204b319da8f1 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 4 Dec 2026 08:09:02 +0530 Subject: [PATCH] test(dedup): gate CompletableFuture thenAccept fixtures with a latch In AsyncHonestyApp, /cf-then-accept and /external-cf-dependent relied on 20ms and 25ms sleeps before completing their upstream CompletableFuture. On a busy host the HTTP servlet thread could stall past 20-25ms before attaching .thenAccept(...), causing the dependent to run inline on the servlet thread before the HTTP response was sent and flaking the windowCut:true assertions in ForkedAsyncAndHonestyTest. Gate both upstream completions on a CountDownLatch released in a finally block after .thenAccept(...) attaches and the HTTP response flushes. Signed-off-by: slayerjain --- .../fakeexternal/FakeExternalHttpClient.java | 15 ++++++ .../dedup/ForkedAsyncAndHonestyTest.java | 2 +- .../org/example/servers/AsyncHonestyApp.java | 54 ++++++++++++++----- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/keploy-sdk/src/test/java/io/fakeexternal/FakeExternalHttpClient.java b/keploy-sdk/src/test/java/io/fakeexternal/FakeExternalHttpClient.java index 59060025..a601bbb0 100644 --- a/keploy-sdk/src/test/java/io/fakeexternal/FakeExternalHttpClient.java +++ b/keploy-sdk/src/test/java/io/fakeexternal/FakeExternalHttpClient.java @@ -1,7 +1,9 @@ package io.fakeexternal; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import java.util.function.LongConsumer; /** @@ -51,6 +53,19 @@ public static CompletableFuture fetchAsync(final long delayMs, final Str return future; } + public static CompletableFuture fetchAsync(final CountDownLatch readyToComplete, final String body) { + final CompletableFuture future = new CompletableFuture<>(); + LOOP.offer(() -> { + try { + readyToComplete.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + future.complete(body); + }); + return future; + } + public static void startBrokerConsumer(final long deliveryDelayMs, final LongConsumer onRecord) { Thread container = new Thread(() -> { while (!Thread.currentThread().isInterrupted()) { diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ForkedAsyncAndHonestyTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ForkedAsyncAndHonestyTest.java index 0c0d5029..4ea16ee8 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/ForkedAsyncAndHonestyTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ForkedAsyncAndHonestyTest.java @@ -93,7 +93,7 @@ public void forkedExternalJacocoPrefixModeTracksCompletableFutureDependentsAndCh assertTrue("k8s external-JaCoCo prefix mode must declare asyncExecutors=true", hello.get("asyncExecutors").getAsBoolean()); - // 1. Fast supplier with slow synchronous dependent: supplyAsync(20ms, pool).thenAccept(350ms audit) + // 1. Fast supplier with slow synchronous dependent: supplyAsync(latch-gated, pool).thenAccept(350ms audit) // with endWait=80ms -> MUST report windowCut:true while audit runs! String cov1 = app.runHttpTest("ts/cf-dep", "/cf-then-accept?ms=350"); assertTrue("CF dependent thenAccept(audit) must keep window open and trigger windowCut: " + cov1, diff --git a/keploy-sdk/src/test/java/org/example/servers/AsyncHonestyApp.java b/keploy-sdk/src/test/java/org/example/servers/AsyncHonestyApp.java index ca2de1ee..7f1b9bdd 100644 --- a/keploy-sdk/src/test/java/org/example/servers/AsyncHonestyApp.java +++ b/keploy-sdk/src/test/java/org/example/servers/AsyncHonestyApp.java @@ -257,13 +257,27 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO }); } else if ("/cf-then-accept".equals(path)) { // Fast supplier followed by a slow synchronous thenAccept(audit) dependent. - CompletableFuture.supplyAsync(() -> { - sleepQuietly(20); - return "result"; - }, PLAIN_POOL).thenAccept(v -> { - sleepQuietly(ms); - AsyncTail.ran(); - }); + // Gate the supplier on a latch released after thenAccept attaches and the + // HTTP response flushes so a busy CPU cannot complete the future early and + // run thenAccept inline on the servlet thread. + final java.util.concurrent.CountDownLatch gate = new java.util.concurrent.CountDownLatch(1); + try { + CompletableFuture.supplyAsync(() -> { + try { + gate.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return "result"; + }, PLAIN_POOL).thenAccept(v -> { + sleepQuietly(ms); + AsyncTail.ran(); + }); + writeOk(resp); + } finally { + gate.countDown(); + } + return; } else if ("/cf-then-apply-async".equals(path)) { CompletableFuture.supplyAsync(() -> "seed", PLAIN_POOL) .thenApplyAsync(v -> { @@ -290,13 +304,21 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO } else if ("/external-cf-dependent".equals(path)) { // Simulates an external library future (e.g., HttpClient.sendAsync) // completed on an external event-loop thread, with app code attaching - // a plain .thenAccept(audit) dependent. - final CompletableFuture externalFuture = - io.fakeexternal.FakeExternalHttpClient.fetchAsync(25, "http-200"); - externalFuture.thenAccept(v -> { - sleepQuietly(ms); - AsyncTail.ran(); - }); + // a plain .thenAccept(audit) dependent. Gate completion on a latch + // released after thenAccept attaches and the HTTP response flushes. + final java.util.concurrent.CountDownLatch gate = new java.util.concurrent.CountDownLatch(1); + try { + final CompletableFuture externalFuture = + io.fakeexternal.FakeExternalHttpClient.fetchAsync(gate, "http-200"); + externalFuture.thenAccept(v -> { + sleepQuietly(ms); + AsyncTail.ran(); + }); + writeOk(resp); + } finally { + gate.countDown(); + } + return; } else if ("/completion-stage-async".equals(path)) { CompletionStage stage = CompletableFuture.completedFuture("seed"); stage.thenApplyAsync(v -> { @@ -445,6 +467,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO } else { Tail.ran(); } + writeOk(resp); + } + + private static void writeOk(HttpServletResponse resp) throws IOException { byte[] body = "ok".getBytes(StandardCharsets.UTF_8); resp.setContentType("text/plain"); resp.setContentLength(body.length);