test(core): control socket deadline boundaries (#9725)

Reuse DeadlineWaits to test socket timeout and completion boundaries on a controlled timeline. Preserve the production monotonic clock, timed future wait and worker-ownership release behavior.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-29 08:01:23 -04:00
1 parent a28ebbf308
commit 0520877cfd
2 files changed
+87 -2

No files matched your search

@@ -24,7 +24,13 @@ public final class SocketDeadline {
* until it actually finishes, even when it ignores interruption.
*/
public static <T> T call(int timeoutSeconds, Callable<T> operation, Runnable onWorkerExit) throws IOException {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds);
return call(timeoutSeconds, operation, onWorkerExit, DeadlineWaits.SYSTEM);
}
/** As {@link #call(int, Callable, Runnable)}, with the deadline counted and waited for by {@code waits}. */
public static <T> T call(int timeoutSeconds, Callable<T> operation, Runnable onWorkerExit,
DeadlineWaits waits) throws IOException {
long deadline = waits.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds);
AtomicBoolean exited = new AtomicBoolean();
Runnable finish = () -> {
if (exited.compareAndSet(false, true)) {
@@ -53,7 +59,7 @@ public final class SocketDeadline {
throw failure;
}
try {
return result.get(Math.max(0, deadline - System.nanoTime()), TimeUnit.NANOSECONDS);
return waits.get(result, Math.max(0, deadline - waits.nanoTime()));
} catch (TimeoutException timeout) {
throw new SocketTimeoutException("Socket operation exceeded " + timeoutSeconds + " seconds");
} catch (InterruptedException interrupted) {
@@ -1,6 +1,8 @@
package io.floci.aws.core.common.concurrent;
import io.floci.aws.testing.TestTime;
import io.floci.aws.testing.ThreadStates;
import io.floci.aws.testing.TimelineDeadlineWaits;
import org.junit.jupiter.api.Test;
import java.io.IOException;
@@ -124,4 +126,81 @@ class SocketDeadlineTest {
})));
assertEquals(1, exits.get());
}
@Test
void anOperationStillRunningWhenItsSecondsAreUpTimesOutAndIsInterrupted() throws Exception {
TestTime time = TestTime.create();
TimelineDeadlineWaits waits = new TimelineDeadlineWaits(time);
CountDownLatch started = new CountDownLatch(1);
CountDownLatch interrupted = new CountDownLatch(1);
CountDownLatch exited = new CountDownLatch(1);
CountDownLatch never = new CountDownLatch(1);
AtomicInteger exits = new AtomicInteger();
FutureTask<String> result = new FutureTask<>(() -> SocketDeadline.call(10, () -> {
started.countDown();
try {
never.await();
} catch (InterruptedException e) {
interrupted.countDown();
throw e;
}
return "late";
}, () -> {
exits.incrementAndGet();
exited.countDown();
}, waits));
Thread caller = Thread.ofVirtual().start(result);
try {
assertTrue(started.await(5, TimeUnit.SECONDS));
waits.awaitWaiting(1);
time.advance(Duration.ofSeconds(10).minusNanos(1));
assertFalse(result.isDone(), "the call gave up before its ten seconds had passed");
assertEquals(1, interrupted.getCount());
assertEquals(1, waits.waiting());
time.advance(Duration.ofNanos(1));
ExecutionException failure = assertThrows(ExecutionException.class,
() -> result.get(5, TimeUnit.SECONDS));
assertInstanceOf(SocketTimeoutException.class, failure.getCause());
assertEquals("Socket operation exceeded 10 seconds", failure.getCause().getMessage());
assertTrue(interrupted.await(5, TimeUnit.SECONDS), "the timed out operation must be interrupted");
assertTrue(exited.await(5, TimeUnit.SECONDS));
assertEquals(1, exits.get());
} finally {
never.countDown();
caller.join(5000);
assertFalse(caller.isAlive());
}
}
@Test
void anOperationThatFinishesOneNanosecondBeforeTheDeadlineReturnsItsResult() throws Exception {
TestTime time = TestTime.create();
TimelineDeadlineWaits waits = new TimelineDeadlineWaits(time);
CountDownLatch started = new CountDownLatch(1);
CompletableFuture<String> answer = new CompletableFuture<>();
AtomicInteger exits = new AtomicInteger();
FutureTask<String> result = new FutureTask<>(() -> SocketDeadline.call(10, () -> {
started.countDown();
return answer.get();
}, exits::incrementAndGet, waits));
Thread caller = Thread.ofVirtual().start(result);
try {
assertTrue(started.await(5, TimeUnit.SECONDS));
waits.awaitWaiting(1);
time.advance(Duration.ofSeconds(10).minusNanos(1));
assertFalse(result.isDone());
answer.complete("healthy");
assertEquals("healthy", result.get(5, TimeUnit.SECONDS));
assertEquals(1, exits.get());
assertEquals(0, time.pendingTasks(), "a finished call must leave no deadline behind");
} finally {
answer.complete("healthy");
caller.join(5000);
assertFalse(caller.isAlive());
}
}
}