mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sns): apply the subscribed queue's DelaySeconds to SQS fan-out (#4170)
The SQS arm of the fan-out passed a literal 0 as the per-message delaySeconds. On a standard queue a non-null value is an explicit per-message delay and overrides the queue attribute, so a queue created with DelaySeconds never withheld a published message, while a direct SendMessage to the same queue did. SNS carries no per-message delay, so the argument is now null and the queue default applies, as on AWS. FIFO queues were unaffected: that branch ignores the argument.
This commit is contained in:
1 parent
1abc817c5d
commit
f07b3fd3be
4 files changed
+174
-2
No files matched your search
@@ -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 queue
|
||||
|
||||
@@ -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" -> {
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
+102
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user