Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -51,6 +53,19 @@ public static CompletableFuture<String> fetchAsync(final long delayMs, final Str
return future;
}

public static CompletableFuture<String> fetchAsync(final CountDownLatch readyToComplete, final String body) {
final CompletableFuture<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 40 additions & 14 deletions keploy-sdk/src/test/java/org/example/servers/AsyncHonestyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand All @@ -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<String> 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<String> 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<String> stage = CompletableFuture.completedFuture("seed");
stage.thenApplyAsync(v -> {
Expand Down Expand Up @@ -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);
Expand Down
Loading