Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sns): apply the subscribed queue's DelaySeconds to SQS fan-out (#4170)
Mihai Blidaruf07b3fd3beec
diff --git a/docs/services/sns.md b/docs/services/sns.mdindex 96e754e89e..a220ba36c9 100644--- a/docs/services/sns.md+++ b/docs/services/sns.md@@ -80,7 +80,7 @@ aws sqs receive-message \## SNS → SQS Fan-Out-Floci supports real SNS → SQS fan-out. When you publish to a topic, all SQS-subscribed queues receive the message immediately.+Floci supports real SNS → SQS fan-out. When you publish to a topic, every SQS-subscribed queue receives the message.Supported subscription protocols:- `sqs`: delivers to a Floci SQS queuediff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.javaindex 7774e2c91b..503207ab6f 100644--- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java@@ -1677,7 +1677,7 @@ public class SnsService implements Resettable, ResourceProvider {Map<String, MessageAttributeValue> sqsAttributes = rawDelivery? toSqsMessageAttributes(messageAttributes): Collections.emptyMap();- sqsService.sendMessage(queueUrl, body, 0, messageGroupId, messageDeduplicationId, sqsAttributes, region);+ sqsService.sendMessage(queueUrl, body, null, messageGroupId, messageDeduplicationId, sqsAttributes, region);LOG.debugv("Delivered SNS message to SQS: {0} ({1}) raw={2}", sub.getEndpoint(), queueUrl, rawDelivery);}case "lambda" -> {diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.javaindex 080e31a118..0af2386348 100644--- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java@@ -1148,6 +1148,76 @@ class SnsIntegrationTest {}}+ @Test+ @Order(69)+ void publish_toQueueWithDelaySeconds_withholdsTheMessage() {+ String delayQueueUrl = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateQueue")+ .formParam("QueueName", "sns-delay-fanout-queue")+ .formParam("Attribute.1.Name", "DelaySeconds")+ .formParam("Attribute.1.Value", "2")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");++ String delayTopicArn = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateTopic")+ .formParam("Name", "sns-delay-fanout-topic")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Subscribe")+ .formParam("TopicArn", delayTopicArn)+ .formParam("Protocol", "sqs")+ .formParam("Endpoint", delayQueueUrl)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Publish")+ .formParam("TopicArn", delayTopicArn)+ .formParam("Message", "delayed fanout")+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ReceiveMessage")+ .formParam("QueueUrl", delayQueueUrl)+ .formParam("MaxNumberOfMessages", "1")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(not(containsString("<Message>")));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "GetQueueAttributes")+ .formParam("QueueUrl", delayQueueUrl)+ .formParam("AttributeName.1", "ApproximateNumberOfMessagesDelayed")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("GetQueueAttributesResponse.GetQueueAttributesResult.Attribute.Value",+ equalTo("1"));+ }+/*** Drains all pending messages from the given SQS queue using PurgeQueue.*/diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsSqsFanoutDelaySecondsTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsSqsFanoutDelaySecondsTest.javanew file mode 100644index 0000000000..6b717efa3c--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsSqsFanoutDelaySecondsTest.java@@ -0,0 +1,102 @@+package io.github.hectorvent.floci.services.sns;++import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+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.*;++/**+ * SNS fan-out carries no per-message delay, so a subscribed queue's own DelaySeconds+ * must apply to messages published through a topic exactly as it does to SendMessage.+ */+class SnsSqsFanoutDelaySecondsTest {++ 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_toStandardQueueWithDelaySeconds_withholdsTheMessage() {+ String queueUrl = createQueue("delay-standard-queue", Map.of("DelaySeconds", "2"));+ String topicArn = createTopicSubscribedTo("delay-standard-topic", "delay-standard-queue", Map.of());++ snsService.publish(topicArn, null, "via-sns", null, REGION);++ assertTrue(sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION).isEmpty());+ assertEquals("1", sqsService.getQueueAttributes(queueUrl,+ List.of("ApproximateNumberOfMessagesDelayed"), REGION)+ .get("ApproximateNumberOfMessagesDelayed"));+ }++ @Test+ void publish_toStandardQueueWithRawDeliveryAndDelaySeconds_withholdsTheMessage() {+ String queueUrl = createQueue("delay-raw-queue", Map.of("DelaySeconds", "2"));+ String topicArn = createTopicSubscribedTo("delay-raw-topic", "delay-raw-queue",+ Map.of("RawMessageDelivery", "true"));++ snsService.publish(topicArn, null, "via-sns-raw", null, REGION);++ assertTrue(sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION).isEmpty());+ }++ @Test+ void publish_toStandardQueueWithoutDelaySeconds_deliversImmediately() {+ String queueUrl = createQueue("no-delay-queue", Map.of());+ String topicArn = createTopicSubscribedTo("no-delay-topic", "no-delay-queue", Map.of());++ snsService.publish(topicArn, null, "no-delay", null, REGION);++ List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);+ assertEquals(1, messages.size());+ assertTrue(messages.getFirst().getBody().contains("no-delay"));+ }++ @Test+ void publish_toFifoQueueWithDelaySeconds_withholdsTheMessage() {+ String queueUrl = createQueue("delay-fifo-queue.fifo",+ Map.of("FifoQueue", "true", "DelaySeconds", "2"));++ snsService.createTopic("delay-fifo-topic.fifo", Map.of("FifoTopic", "true"), null, REGION);+ String topicArn = "arn:aws:sns:" + REGION + ":" + ACCOUNT + ":delay-fifo-topic.fifo";+ snsService.subscribe(topicArn, "sqs",+ "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":delay-fifo-queue.fifo", REGION, Map.of());++ snsService.publish(topicArn, null, null, "via-sns-fifo",+ null, null, "group-1", "dedup-1", REGION);++ assertTrue(sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION).isEmpty());+ }++ private String createQueue(String queueName, Map<String, String> attributes) {+ sqsService.createQueue(queueName, attributes, REGION);+ return BASE_URL + "/" + ACCOUNT + "/" + queueName;+ }++ private String createTopicSubscribedTo(String topicName, String queueName,+ Map<String, String> subscriptionAttributes) {+ snsService.createTopic(topicName, null, null, REGION);+ String topicArn = "arn:aws:sns:" + REGION + ":" + ACCOUNT + ":" + topicName;+ snsService.subscribe(topicArn, "sqs",+ "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + queueName, REGION, subscriptionAttributes);+ return topicArn;+ }+}