fix(lambda): keep the visibility a function sets on failed SQS messages (#9003)

This commit is contained in:
Michael Edwards authored and GitHub committed 2026-11-19 13:26:45 -04:00
1 parent 07f93dc8e5
commit ede9601b82
6 files changed
+281 -24

No files matched your search

@@ -338,8 +338,7 @@ class SqsEventSourcePollerTest {
// 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).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 2, "us-east-1");
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@@ -413,8 +412,7 @@ class SqsEventSourcePollerTest {
awaitPoll(poller.pollAndInvoke(esm));
// Falls back to the AWS default of 30s rather than 0 (which would spin a tight retry loop).
verify(sqsService).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 30, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 30, "us-east-1");
}
@Test
@@ -436,7 +434,7 @@ class SqsEventSourcePollerTest {
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
}
@Test
@@ -733,7 +731,7 @@ class SqsEventSourcePollerTest {
}
// m1 deleted after successful invoke; nothing returned to the queue.
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
}
@Test
@@ -770,7 +768,7 @@ class SqsEventSourcePollerTest {
// Filtered-out m2 is consumed (deleted) regardless of the invoke outcome.
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
// Matched m1 failed → returned to the queue (visibility reset), NOT deleted.
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 5, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 5, "us-east-1");
verify(sqsService, never()).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
}
@@ -794,7 +792,7 @@ class SqsEventSourcePollerTest {
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());
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
}
@Test
@@ -855,8 +853,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).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 2, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m2", null, 2, "us-east-1");
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@@ -872,7 +870,7 @@ class SqsEventSourcePollerTest {
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());
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
}
@Test
@@ -880,8 +878,100 @@ class SqsEventSourcePollerTest {
EventSourceMapping esm = pollReportingBatch("{\"batchItemFailures\":[{\"itemIdentifier\":\"m2\"}]}");
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m2", null, 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());
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(eq(esm.getQueueUrl()), eq("rh-m1"), any(), anyInt(), any());
}
@Test
void partialBatchFailure_functionChangedVisibility_returnsTheMessageAgainstItsReceiveTimeVisibility() {
LambdaFunction fn = stubThrowFn();
EventSourceMapping esm = esm();
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
Message msg = message("m1");
msg.setVisibleAt(setByReceive);
stubReceive(esm, List.of(msg));
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
.thenReturn(Map.of("VisibilityTimeout", "2"));
InvokeResult result = new InvokeResult();
result.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"m1\"}]}".getBytes());
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
// The received message is the queue's live instance, so a ChangeMessageVisibility
// call the function makes shows up on it.
msg.setVisibleAt(setByReceive.plusSeconds(5));
return result;
});
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService).changeMessageVisibilityIfUnchanged(
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@Test
void batchingWindow_failedMessage_isReturnedAgainstTheVisibilityItsReceiveSet() {
LambdaFunction fn = stubThrowFn();
EventSourceMapping esm = esm();
esm.setBatchSize(2);
esm.setMaximumBatchingWindowInSeconds(5);
AtomicLong now = new AtomicLong(0);
poller.setClockForTest(now::get);
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
Message msg = message("m1");
msg.setVisibleAt(setByReceive);
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of(msg))
.thenReturn(List.of());
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
.thenReturn(Map.of("VisibilityTimeout", "2"));
InvokeResult failure = new InvokeResult();
failure.setFunctionError("Handled");
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
msg.setVisibleAt(setByReceive.plusSeconds(5));
return failure;
});
awaitPoll(poller.pollAndInvoke(esm));
verify(executorService, never()).invoke(any(), any(), any());
now.set(5000);
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService).changeMessageVisibilityIfUnchanged(
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
}
@Test
void partialBatchFailure_messageReceivedAgainDuringInvocation_returnsItWithTheOriginalReceipt() {
LambdaFunction fn = stubThrowFn();
EventSourceMapping esm = esm();
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
Message msg = message("m1");
msg.setVisibleAt(setByReceive);
stubReceive(esm, List.of(msg));
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
.thenReturn(Map.of("VisibilityTimeout", "2"));
InvokeResult result = new InvokeResult();
result.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"m1\"}]}".getBytes());
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
// Another consumer receives the message once the function's short visibility
// runs out, which gives the queue's live instance a new handle and visibility.
msg.setReceiptHandle("rh-m1-second");
msg.setVisibleAt(setByReceive.plusSeconds(30));
return result;
});
awaitPoll(poller.pollAndInvoke(esm));
verify(sqsService).changeMessageVisibilityIfUnchanged(
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(
eq(esm.getQueueUrl()), eq("rh-m1-second"), any(), anyInt(), any());
}
}
@@ -150,6 +150,42 @@ class GuardedMessageQueueTest {
assertEquals(1, reClaimed.claimed().size());
}
@Test
void changeVisibility_expectedVisibleAtNoLongerCurrent_keepsTheNewerVisibility() {
queue.addMessage(new Message("msg"));
Message claimed = queue.claimVisibleMessages(1, 30, false, -1, null).claimed().get(0);
Instant setByClaim = claimed.getVisibleAt();
ReceiptHandle handle = ReceiptHandle.parse(claimed.getReceiptHandle());
assertEquals(HandleResult.APPLIED, queue.changeVisibility(handle, 60, false));
assertEquals(HandleResult.VISIBILITY_CHANGED, queue.changeVisibility(handle, setByClaim, 0, false));
assertTrue(queue.claimVisibleMessages(1, 30, false, -1, null).claimed().isEmpty());
}
@Test
void changeVisibility_expectedVisibleAtStillCurrent_appliesTheTimeout() {
queue.addMessage(new Message("msg"));
Message claimed = queue.claimVisibleMessages(1, 30, false, -1, null).claimed().get(0);
ReceiptHandle handle = ReceiptHandle.parse(claimed.getReceiptHandle());
assertEquals(HandleResult.APPLIED, queue.changeVisibility(handle, claimed.getVisibleAt(), 0, false));
assertEquals(1, queue.claimVisibleMessages(1, 30, false, -1, null).claimed().size());
}
@Test
void changeVisibility_fifoMessageReceivedAgain_keepsTheNewReceiversVisibility() {
Message message = new Message("msg");
message.setMessageGroupId("group");
queue.addMessage(message);
Message first = queue.claimVisibleMessages(1, 0, true, -1, null).claimed().get(0);
ReceiptHandle firstHandle = ReceiptHandle.parse(first.getReceiptHandle());
Instant setByFirstClaim = first.getVisibleAt();
assertEquals(1, queue.claimVisibleMessages(1, 30, true, -1, null).claimed().size());
assertEquals(HandleResult.VISIBILITY_CHANGED, queue.changeVisibility(firstHandle, setByFirstClaim, 0, true));
assertTrue(queue.claimVisibleMessages(1, 30, true, -1, null).claimed().isEmpty());
}
@Test
void changeVisibilityInvalidReturnsFalse() {
assertEquals(HandleResult.MESSAGE_GONE, queue.changeVisibility(ReceiptHandle.issue(null, "nonexistent", ReceiptHandle.DEFAULT_SECRET), 0, false));
@@ -940,6 +940,66 @@ class SqsServiceTest {
assertEquals(1, reReceived.size());
}
@Test
void changeMessageVisibilityIfUnchanged_visibilityUnchangedSinceReceive_changesIt() {
String region = "eu-west-1";
Queue queue = sqsService.createQueue("if-unchanged", null, region);
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
queue.getQueueUrl(), received.getReceiptHandle(), received.getVisibleAt(), 0, region);
assertTrue(changed);
assertEquals(1, sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).size());
}
@Test
void changeMessageVisibilityIfUnchanged_consumerChangedVisibility_keepsTheConsumersTimeout() {
String region = "eu-west-1";
Queue queue = sqsService.createQueue("if-changed", null, region);
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
Instant setByReceive = received.getVisibleAt();
sqsService.changeMessageVisibility(queue.getQueueUrl(), received.getReceiptHandle(), 60, region);
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
queue.getQueueUrl(), received.getReceiptHandle(), setByReceive, 0, region);
assertFalse(changed);
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).isEmpty());
}
@Test
void changeMessageVisibilityIfUnchanged_messageReceivedAgain_keepsTheNewReceiversTimeout() {
String region = "eu-west-1";
Queue queue = sqsService.createQueue("if-received-again", null, region);
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
Message first = sqsService.receiveMessage(queue.getQueueUrl(), 1, 0, 0, region).getFirst();
String firstHandle = first.getReceiptHandle();
Instant setByFirstReceive = first.getVisibleAt();
assertEquals(1, sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).size());
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
queue.getQueueUrl(), firstHandle, setByFirstReceive, 0, region);
assertFalse(changed);
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).isEmpty());
}
@Test
void changeMessageVisibilityIfUnchanged_deletedMessage_isRejected() {
String region = "eu-west-1";
Queue queue = sqsService.createQueue("if-unchanged-gone", null, region);
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
Instant setByReceive = received.getVisibleAt();
sqsService.deleteMessage(queue.getQueueUrl(), received.getReceiptHandle(), region);
assertThrows(AwsException.class, () -> sqsService.changeMessageVisibilityIfUnchanged(
queue.getQueueUrl(), received.getReceiptHandle(), setByReceive, 0, region));
}
@Test
void getQueueAttributes() {
String region = "eu-west-1";