test(lambda): wait for each SQS poll before advancing the batching-window clock (#4796)

batchingWindowHoldsUnderfilledBatchUntilTheWindowElapses slept 25ms between
polls and then moved the fake clock forward. The poll runs on a separate
thread and starts the window when it reads the clock, so a poll that took
longer than 25ms on a busy runner started the window late (for example at
3s). The later polls were skipped while it was still running, and at 5s the
window had not elapsed, so the function was never invoked.

pollAndInvoke now returns the future of the submitted poll, and the test
waits for each poll to finish before moving the clock.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-10-01 07:48:12 -04:00
1 parent 419e840260
commit 95b5acb1db
2 files changed
+27 -7

No files matched your search

@@ -22,6 +22,10 @@ import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.jupiter.api.Assertions.*;
@@ -531,6 +535,17 @@ class SqsEventSourcePollerTest {
}
}
private static void awaitPoll(Future<?> poll) {
try {
poll.get(5, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new AssertionError("interrupted while waiting for the poll", ie);
} catch (ExecutionException | TimeoutException e) {
throw new AssertionError("poll did not complete", e);
}
}
private void pollUntilInvoked(EventSourceMapping esm, LambdaFunction fn) {
long deadline = System.currentTimeMillis() + 5000;
while (true) {
@@ -564,10 +579,10 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
// Polls inside the window buffer the record without invoking.
// Polls inside the window buffer the record without invoking. Each poll finishes before
// the clock moves, so the window starts at 0 however long the poll thread takes to run.
for (int i = 0; i < 4; i++) {
poller.pollAndInvoke(esm);
sleep(25);
awaitPoll(poller.pollAndInvoke(esm));
now.addAndGet(1000); // reaches 4s, short of the 5s window
}
verify(sqsService, atLeast(1))