fix(sns): retry failed sqs subscription deliveries (#4468)

* fix(sns): retry failed sqs subscription deliveries

* fix(sns): support fifo subscription dead letter queues

* fix(sns): distinguish shared fifo dlq deliveries

---------

Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
vamsiakandVamsi Akavarapu authored and GitHub committed 2026-09-26 13:59:29 -04:00
1 parent 5af1d4048e
commit f3dde75c0b
6 files changed
+285 -3

No files matched your search

@@ -11,6 +11,8 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.sqs.model.Message;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -24,9 +26,13 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class SnsServiceTest {
@@ -80,6 +86,55 @@ class SnsServiceTest {
assertThrows(AwsException.class, () -> snsService.createTopic("", null, null, REGION));
}
@Test
void publish_retriesTransientSqsSubscriptionDeliveryFailure() {
SqsService sqs = mock(SqsService.class);
SnsService service = new SnsService(new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT), sqs, null);
Topic topic = service.createTopic("retry-topic", null, null, REGION);
String queueArn = "arn:aws:sqs:us-east-1:000000000000:retry-queue";
String queueUrl = BASE_URL + "/" + ACCOUNT + "/retry-queue";
service.subscribe(topic.getTopicArn(), "sqs", queueArn, REGION, Map.of());
doThrow(new AwsException("ServiceUnavailable", "temporarily unavailable", 503))
.doReturn(new Message("delivered"))
.when(sqs).sendMessage(eq(queueUrl), anyString(), isNull(), isNull(), isNull(), anyMap(), eq(REGION));
String messageId = service.publish(topic.getTopicArn(), null, "payload", null, REGION);
assertNotNull(messageId);
verify(sqs, times(2)).sendMessage(eq(queueUrl), anyString(), isNull(), isNull(), isNull(), anyMap(), eq(REGION));
}
@Test
void publish_sendsExhaustedSqsSubscriptionDeliveryToConfiguredDlq() throws Exception {
SqsService sqs = mock(SqsService.class);
SnsService service = new SnsService(new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT), sqs, null);
Topic topic = service.createTopic("dlq-topic", null, null, REGION);
String queueArn = "arn:aws:sqs:us-east-1:000000000000:unavailable-queue";
String queueUrl = BASE_URL + "/" + ACCOUNT + "/unavailable-queue";
String dlqArn = "arn:aws:sqs:us-east-1:000000000000:subscription-dlq";
String dlqUrl = BASE_URL + "/" + ACCOUNT + "/subscription-dlq";
service.subscribe(topic.getTopicArn(), "sqs", queueArn, REGION,
Map.of("RedrivePolicy", "{\"deadLetterTargetArn\":\"" + dlqArn + "\"}"));
doThrow(new AwsException("AWS.SimpleQueueService.NonExistentQueue", "unavailable", 400))
.when(sqs).sendMessage(eq(queueUrl), anyString(), isNull(), isNull(), isNull(), anyMap(), eq(REGION));
String messageId = service.publish(topic.getTopicArn(), null,
"payload", "subject", REGION);
assertNotNull(messageId, "SNS Publish acceptance is independent of downstream SQS delivery");
verify(sqs, times(3)).sendMessage(eq(queueUrl), anyString(), isNull(), isNull(), isNull(), anyMap(), eq(REGION));
ArgumentCaptor<String> deadLetterBody = ArgumentCaptor.forClass(String.class);
verify(sqs).sendMessage(eq(dlqUrl), deadLetterBody.capture(), isNull(), isNull(), isNull(), anyMap(), eq(REGION));
JsonNode envelope = new ObjectMapper().readTree(deadLetterBody.getValue());
assertEquals("Notification", envelope.path("Type").asText());
assertEquals(topic.getTopicArn(), envelope.path("TopicArn").asText());
assertEquals("payload", envelope.path("Message").asText());
assertEquals("subject", envelope.path("Subject").asText());
assertTrue(envelope.hasNonNull("MessageId"));
}
@Test
void listTopics_returnsCreatedTopics() {
snsService.createTopic("topic-a", null, null, REGION);
@@ -0,0 +1,123 @@
package io.github.hectorvent.floci.services.sns;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.sqs.SqsServiceFactory;
import io.github.hectorvent.floci.services.sqs.model.Message;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SnsSqsSubscriptionDlqIntegrationTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private static final String BASE_URL = "http://localhost:4566";
private SnsService snsService;
private SqsService sqsService;
@BeforeEach
void setUp() {
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
sqsService = SqsServiceFactory.createInMemory(BASE_URL, regionResolver);
snsService = new SnsService(new InMemoryStorage<>(), new InMemoryStorage<>(),
regionResolver, sqsService, null);
}
@Test
void publish_routesExhaustedDeliveryToSubscriptionDlq() throws Exception {
String dlqName = "sns-subscription-dlq";
String dlqArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + dlqName;
String dlqUrl = BASE_URL + "/" + ACCOUNT + "/" + dlqName;
sqsService.createQueue(dlqName, null, REGION);
Topic topic = snsService.createTopic("sns-dlq-topic", null, null, REGION);
String unavailableQueueArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":unavailable-queue";
Subscription subscription = snsService.subscribe(
topic.getTopicArn(), "sqs", unavailableQueueArn, REGION, Map.of());
snsService.setSubscriptionAttribute(subscription.getSubscriptionArn(), "RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\"}", REGION);
String publishMessageId = snsService.publish(topic.getTopicArn(), null,
"synthetic-payload", "synthetic-subject", REGION);
assertNotNull(publishMessageId);
List<Message> deadLetterMessages = sqsService.receiveMessage(dlqUrl, 1, 30, 0, REGION);
assertEquals(1, deadLetterMessages.size());
JsonNode envelope = new ObjectMapper().readTree(deadLetterMessages.getFirst().getBody());
assertEquals("Notification", envelope.path("Type").asText());
assertEquals(publishMessageId, envelope.path("MessageId").asText());
assertEquals(topic.getTopicArn(), envelope.path("TopicArn").asText());
assertEquals("synthetic-payload", envelope.path("Message").asText());
assertEquals("synthetic-subject", envelope.path("Subject").asText());
assertTrue(sqsService.receiveMessage(dlqUrl, 1, 30, 0, REGION).isEmpty(),
"The notification should appear only once in the subscription DLQ");
}
@Test
void publish_routesStandardTopicNotificationToFifoSubscriptionDlq() throws Exception {
String dlqName = "sns-subscription-dlq.fifo";
String dlqArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + dlqName;
String dlqUrl = BASE_URL + "/" + ACCOUNT + "/" + dlqName;
sqsService.createQueue(dlqName, Map.of("FifoQueue", "true"), REGION);
Topic topic = snsService.createTopic("sns-standard-topic", null, null, REGION);
Subscription subscription = snsService.subscribe(topic.getTopicArn(), "sqs",
"arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":unavailable-fifo-source.fifo", REGION, Map.of());
snsService.setSubscriptionAttribute(subscription.getSubscriptionArn(), "RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\"}", REGION);
String publishMessageId = snsService.publish(topic.getTopicArn(), null,
"synthetic-payload", null, REGION);
List<Message> deadLetterMessages = sqsService.receiveMessage(dlqUrl, 1, 30, 0, REGION);
assertEquals(1, deadLetterMessages.size());
Message deadLetter = deadLetterMessages.getFirst();
JsonNode envelope = new ObjectMapper().readTree(deadLetter.getBody());
assertEquals(publishMessageId, deadLetter.getMessageGroupId());
assertNotNull(deadLetter.getMessageDeduplicationId());
assertEquals(publishMessageId, envelope.path("MessageId").asText());
}
@Test
void publish_keepsBothFailedSubscriptionsInSharedFifoDlq() throws Exception {
String dlqName = "shared-subscription-dlq.fifo";
String dlqArn = "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + dlqName;
String dlqUrl = BASE_URL + "/" + ACCOUNT + "/" + dlqName;
sqsService.createQueue(dlqName, Map.of("FifoQueue", "true"), REGION);
Topic topic = snsService.createTopic("shared-dlq-topic", null, null, REGION);
for (String endpoint : List.of("missing-subscription-a", "missing-subscription-b")) {
Subscription subscription = snsService.subscribe(topic.getTopicArn(), "sqs",
"arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + endpoint, REGION, Map.of());
snsService.setSubscriptionAttribute(subscription.getSubscriptionArn(), "RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\"}", REGION);
}
String publishMessageId = snsService.publish(topic.getTopicArn(), null, "payload", null, REGION);
List<Message> deadLetters = sqsService.receiveMessage(dlqUrl, 10, 30, 0, REGION);
assertEquals(2, deadLetters.size());
assertEquals(publishMessageId, deadLetters.get(0).getMessageGroupId());
assertEquals(publishMessageId, deadLetters.get(1).getMessageGroupId());
assertNotEquals(deadLetters.get(0).getMessageDeduplicationId(),
deadLetters.get(1).getMessageDeduplicationId());
ObjectMapper mapper = new ObjectMapper();
for (Message deadLetter : deadLetters) {
assertEquals(publishMessageId, mapper.readTree(deadLetter.getBody()).path("MessageId").asText());
}
}
}
@@ -175,6 +175,28 @@ class GuardedMessageQueueTest {
assertEquals(1, target.messageCounts().visible());
}
@Test
void addMessageRollsBackTheInMemoryAddWhenPersistFails() {
AtomicBoolean storeDown = new AtomicBoolean();
InMemoryStorage<String, List<Message>> store = new InMemoryStorage<String, List<Message>>() {
@Override
public void put(String key, List<Message> value) {
if (storeDown.get()) {
throw new IllegalStateException("store unavailable");
}
super.put(key, value);
}
};
GuardedMessageQueue target = new GuardedMessageQueue(store, "us-east-1::/000000000000/target");
target.addMessage(new Message("already-there"));
storeDown.set(true);
assertThrows(IllegalStateException.class, () -> target.addMessage(new Message("incoming")));
assertEquals(List.of("already-there"), target.peekAll().stream().map(Message::getBody).toList());
assertEquals(1, target.messageCounts().visible());
}
@Test
void messageCountsReturnsVisibleInFlightAndDelayed() {
queue.addMessage(new Message("msg1"));