mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): fail the whole SQS batch on malformed batchItemFailures (#3169)
A ReportBatchItemFailures response with an empty, null or unknown itemIdentifier, an entry without itemIdentifier, a non-array batchItemFailures, or invalid JSON deleted every delivered message, so a broken handler response silently dropped messages instead of retrying them and letting the RedrivePolicy move them to the DLQ. AWS treats each of these as a complete batch failure; only empty/null lists and empty/null responses are a complete success. Validate the response against the received message IDs before any delete and route malformed responses through the existing return-to-queue path. An id naming a filtered-out message stays inert as before. Fixes #3152
This commit is contained in:
1 parent
49dfd1b18a
commit
bcd422ad83
2 files changed
+102
-10
No files matched your search
+69
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Instant;
|
||||
@@ -589,4 +591,71 @@ class SqsEventSourcePollerTest {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ───────────────────────── ReportBatchItemFailures ─────────────────────────
|
||||
|
||||
/** Delivers m1 and m2 to a ReportBatchItemFailures mapping whose function returns {@code payload}. */
|
||||
private EventSourceMapping pollReportingBatch(String payload) {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setBatchSize(2);
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
stubReceive(esm, List.of(message("m1"), message("m2")));
|
||||
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
|
||||
.thenReturn(Map.of("VisibilityTimeout", "2"));
|
||||
InvokeResult result = new InvokeResult();
|
||||
result.setPayload(payload.getBytes());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(result);
|
||||
|
||||
poller.pollAndInvoke(esm);
|
||||
return esm;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"\"}]}",
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":null}]}",
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"unknown\"}]}",
|
||||
"{\"batchItemFailures\":[{\"wrongKey\":\"m1\"}]}",
|
||||
"{\"batchItemFailures\":\"invalid\"}",
|
||||
"not json",
|
||||
})
|
||||
void malformedBatchItemFailuresResponseFailsWholeBatch(String payload) {
|
||||
EventSourceMapping esm = pollReportingBatch(payload);
|
||||
|
||||
// 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, never()).deleteMessage(any(), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"batchItemFailures\":[]}",
|
||||
"{\"batchItemFailures\":null}",
|
||||
"{}",
|
||||
"null",
|
||||
})
|
||||
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, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
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, never()).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(eq(esm.getQueueUrl()), eq("rh-m1"), anyInt(), any());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user