test(lambda): await SQS polls and close fixture executors (#8578)

SqsEventSourcePollerTest now waits for each submitted poll to finish
before asserting deletes, visibility changes and invocations, instead of
timed Mockito verifications and retry loops, and shuts down each
fixture's poller after the test.
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-16 08:44:05 -04:00
1 parent 3e0cd437d7
commit f94d5e9e58
1 file changed
+58 -109
@@ -13,6 +13,7 @@ import io.floci.aws.services.sqs.SqsService;
import io.floci.aws.services.sqs.model.Message;
import io.floci.aws.services.sqs.model.MessageAttributeValue;
import io.vertx.core.Vertx;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -39,7 +40,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -79,6 +79,11 @@ class SqsEventSourcePollerTest {
);
}
@AfterEach
void tearDown() {
poller.shutdown();
}
@Test
void buildSqsEventIncludesAllRequiredAttributes() throws Exception {
Instant firstReceived = Instant.parse("2026-01-15T10:31:00Z");
@@ -326,12 +331,12 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(failure);
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
// The failed message must be made visible again after the queue's own visibility
// timeout (2s here) so the next poll re-receives it and the queue's RedrivePolicy
// can move it to the DLQ — rather than staying in-flight for fn.timeout + 30s.
verify(sqsService, timeout(2000)).changeMessageVisibility(
verify(sqsService).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@@ -351,9 +356,9 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1")))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService, timeout(2000)).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1"));
verify(executorService).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1"));
}
@Test
@@ -377,9 +382,9 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live")))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService, timeout(2000)).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live"));
verify(executorService).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live"));
}
@Test
@@ -403,10 +408,10 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(failure);
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
// Falls back to the AWS default of 30s rather than 0 (which would spin a tight retry loop).
verify(sqsService, timeout(2000)).changeMessageVisibility(
verify(sqsService).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 30, "us-east-1");
}
@@ -426,9 +431,9 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
}
@@ -530,44 +535,6 @@ class SqsEventSourcePollerTest {
.thenReturn(messages);
}
/**
* Hard happens-after barrier: re-stub the fetch to return empty, then re-kick until a SECOND
* receiveMessage is observed. {@code activePolls} serializes polls per ESM, so fetch #2 cannot begin until
* poll #1's finally-block ran, so poll #1 is fully complete. The empty re-stub makes poll #2 a no-op, so
* poll #1's exact delete/return counts are preserved for the assertions.
*/
private void awaitPollCompleted(EventSourceMapping esm) {
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of());
long deadline = System.currentTimeMillis() + 5000;
while (true) {
poller.pollAndInvoke(esm);
try {
verify(sqsService, atLeast(2))
.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1"));
return;
} catch (AssertionError retry) {
if (System.currentTimeMillis() > deadline) {
throw retry;
}
try {
Thread.sleep(25);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw retry;
}
}
}
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
private static void awaitPoll(Future<?> poll) {
try {
poll.get(5, TimeUnit.SECONDS);
@@ -579,23 +546,6 @@ class SqsEventSourcePollerTest {
}
}
private void pollUntilInvoked(EventSourceMapping esm, LambdaFunction fn) {
long deadline = System.currentTimeMillis() + 5000;
while (true) {
poller.pollAndInvoke(esm);
try {
verify(executorService, timeout(2000))
.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse));
return;
} catch (AssertionError retry) {
if (System.currentTimeMillis() > deadline) {
throw retry;
}
sleep(25);
}
}
}
@Test
void batchingWindowHoldsUnderfilledBatchUntilTheWindowElapses() {
LambdaFunction fn = stubThrowFn();
@@ -625,8 +575,9 @@ class SqsEventSourcePollerTest {
// Once the window elapses the buffered record is delivered.
now.set(5000);
pollUntilInvoked(esm, fn);
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService).invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse));
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
}
@Test
@@ -645,10 +596,12 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
pollUntilInvoked(esm, fn);
awaitPoll(poller.pollAndInvoke(esm));
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService).invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse));
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
}
@Test
@@ -664,10 +617,10 @@ class SqsEventSourcePollerTest {
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
ArgumentCaptor<Integer> visibility = ArgumentCaptor.forClass(Integer.class);
verify(sqsService, timeout(2000)).receiveMessage(
verify(sqsService).receiveMessage(
eq(esm.getQueueUrl()), anyInt(), visibility.capture(), anyInt(), eq("us-east-1"));
// held up to 60s buffered, then up to 120s executing, plus AWS's 30s margin
assertEquals(120 + 60 + 30, visibility.getValue());
@@ -683,7 +636,8 @@ class SqsEventSourcePollerTest {
when(lambdaConfig.pollIntervalMs()).thenReturn(1000L);
when(lambdaConfig.initTimeoutSeconds()).thenReturn(120);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
SqsEventSourcePoller slowInitPoller = new SqsEventSourcePoller(mock(Vertx.class), sqsService,
poller.shutdown();
poller = new SqsEventSourcePoller(mock(Vertx.class), sqsService,
executorService, new LambdaTargetResolver(functionStore, aliasStore), mock(EsmStore.class),
config, OBJECT_MAPPER, new PipesFilterMatcher(OBJECT_MAPPER));
EventSourceMapping esm = esm();
@@ -695,10 +649,10 @@ class SqsEventSourcePollerTest {
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of());
slowInitPoller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
ArgumentCaptor<Integer> visibility = ArgumentCaptor.forClass(Integer.class);
verify(sqsService, timeout(2000)).receiveMessage(
verify(sqsService).receiveMessage(
eq(esm.getQueueUrl()), anyInt(), visibility.capture(), anyInt(), eq("us-east-1"));
// up to 120s for a cold runtime to request the invocation, then 3s executing, plus the 30s margin
assertEquals(3 + 120 + 30, visibility.getValue());
@@ -719,15 +673,16 @@ class SqsEventSourcePollerTest {
.thenReturn(new InvokeResult());
// Poll once with the window open: the record is buffered, not delivered.
poller.pollAndInvoke(esm);
verify(sqsService, timeout(2000))
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService)
.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1"));
verify(executorService, never()).invoke(any(), any(), any());
// Window turned off: the next poll must flush the stranded batch instead of ignoring it.
esm.setMaximumBatchingWindowInSeconds(0);
pollUntilInvoked(esm, fn);
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService).invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse));
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
}
private static final String BODY_PATTERN = "{\"body\":{\"type\":[\"order\"]}}";
@@ -742,13 +697,13 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
// Non-matching m2 is deleted immediately (consumed, per AWS SQS filtering).
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
// Only m1 is delivered to the function.
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(executorService, timeout(2000)).invoke(eq(fn), payload.capture(), eq(InvocationType.RequestResponse));
verify(executorService).invoke(eq(fn), payload.capture(), eq(InvocationType.RequestResponse));
try {
JsonNode records = OBJECT_MAPPER.readTree(payload.getValue()).get("Records");
assertEquals(1, records.size());
@@ -757,7 +712,7 @@ class SqsEventSourcePollerTest {
throw new RuntimeException(e);
}
// m1 deleted after successful invoke; nothing returned to the queue.
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
}
@@ -769,11 +724,10 @@ class SqsEventSourcePollerTest {
Message m2 = msgBody("m2", "{\"type\":\"chargeback\"}");
stubReceive(esm, List.of(m1, m2));
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(executorService, never()).invoke(any(), any(byte[].class), any());
}
@@ -791,13 +745,12 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(err);
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
// Filtered-out m2 is consumed (deleted) regardless of the invoke outcome.
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
// Matched m1 failed → returned to the queue (visibility reset), NOT deleted.
verify(sqsService, timeout(2000)).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 5, "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 5, "us-east-1");
verify(sqsService, never()).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
}
@@ -815,12 +768,11 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(result);
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
// m1 (delivered, not reported failed) is deleted post-success; m2 was deleted once as filtered-out.
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
// The stale failure id does not cause a second delete or a visibility reset.
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
}
@@ -835,11 +787,11 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(executorService, timeout(2000)).invoke(eq(fn), payload.capture(), eq(InvocationType.RequestResponse));
verify(executorService).invoke(eq(fn), payload.capture(), eq(InvocationType.RequestResponse));
try {
JsonNode records = OBJECT_MAPPER.readTree(payload.getValue()).get("Records");
assertEquals(1, records.size());
@@ -865,7 +817,7 @@ class SqsEventSourcePollerTest {
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(result);
poller.pollAndInvoke(esm);
awaitPoll(poller.pollAndInvoke(esm));
return esm;
}
@@ -883,9 +835,8 @@ class SqsEventSourcePollerTest {
// AWS treats these as a complete failure: nothing is deleted, every delivered
// message goes back to the queue for retry/redrive.
verify(sqsService, timeout(2000)).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService, timeout(2000)).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@@ -899,9 +850,8 @@ class SqsEventSourcePollerTest {
void emptyOrNullBatchItemFailuresResponseDeletesWholeBatch(String payload) {
EventSourceMapping esm = pollReportingBatch(payload);
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
}
@@ -909,9 +859,8 @@ class SqsEventSourcePollerTest {
void validPartialBatchFailureRetriesOnlyReportedMessage() {
EventSourceMapping esm = pollReportingBatch("{\"batchItemFailures\":[{\"itemIdentifier\":\"m2\"}]}");
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, timeout(2000)).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
awaitPollCompleted(esm);
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
verify(sqsService, never()).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(eq(esm.getQueueUrl()), eq("rh-m1"), anyInt(), any());
}