diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java index 909416eca0..245c868ed2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java @@ -26,9 +26,11 @@ import java.util.HashSet; import java.util.IdentityHashMap; import java.util.List; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.function.LongSupplier; import java.util.stream.Collectors; @@ -152,15 +154,18 @@ public class SqsEventSourcePoller implements Resettable { } } - /** Package-private (not private) only so unit tests can drive a single poll directly. */ - void pollAndInvoke(EventSourceMapping esm) { + /** + * Package-private (not private) only so unit tests can drive a single poll directly and wait + * for it to finish through the returned future. + */ + Future pollAndInvoke(EventSourceMapping esm) { // Skip this tick if a previous poll for this ESM is still in progress. // This prevents concurrent deliveries of the same message when the Lambda // cold-start / execution time exceeds the SQS visibility timeout. if (activePolls.putIfAbsent(esm.getUuid(), Boolean.TRUE) != null) { - return; + return CompletableFuture.completedFuture(null); } - pollExecutor.submit(() -> { + return pollExecutor.submit(() -> { try { // Look up the function first so we can set an appropriate visibility // timeout: fn.timeout + 30s keeps messages hidden while Lambda runs. diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java index b6a79423db..7796627fbc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java @@ -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))