fix(sqs): honor MaximumMessageSize attribute on SendMessage (#782)

* fix(sqs): honor MaximumMessageSize attribute on SendMessage

SendMessage previously enforced a hard-coded 256 KiB limit regardless
of the queue's MaximumMessageSize attribute, and the error text always
referenced 262144 bytes. Two behavioral problems:

- Queues created with MaximumMessageSize=1048576 still rejected
  payloads over 256 KiB.
- The size check only inspected the message body (in chars), not body
  bytes plus message attribute name/type/value bytes, the formula AWS
  uses to compute total message size.

Both are now fixed: SendMessage reads the queue's MaximumMessageSize
attribute (clamped to AWS's 1024..1048576 range), computes total size
as UTF-8 body bytes + per-attribute (name + type + value) bytes, and
reports the queue's configured limit in the error message.

Refs #768.

* fix(sqs): clamp MaximumMessageSize to AWS range, rename test var

Address PR review feedback:
- parseMaxMessageSize now clamps out-of-range values to [1024, 1048576]
  instead of silently falling back to the service default, matching the
  PR description and avoiding surprising behavior for queues configured
  with values just outside the AWS range.
- Rename misleading test variable halfMeg (300 KiB body) to body.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-11 23:52:58 -05:00
1 parent 6f9782d093
commit ec9631e8c5
2 files changed
+76 -2

No files matched your search

@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.model.Message;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import io.github.hectorvent.floci.services.sqs.model.Queue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -439,6 +440,41 @@ class SqsServiceTest {
"Dedup store must be empty after purge with clearFifoDeduplicationCacheOnPurge=true");
}
@Test
void sendMessage_usesQueueMaximumMessageSizeAttribute() {
Queue queue = sqsService.createQueue("big-queue",
Map.of("MaximumMessageSize", "524288"));
String body = "x".repeat(300_000);
assertDoesNotThrow(() -> sqsService.sendMessage(queue.getQueueUrl(), body, 0),
"Body within the queue's MaximumMessageSize must be accepted");
}
@Test
void sendMessage_oversize_errorReportsQueueLimit() {
Queue queue = sqsService.createQueue("limited-queue",
Map.of("MaximumMessageSize", "2048"));
String oversized = "x".repeat(3000);
AwsException ex = assertThrows(AwsException.class,
() -> sqsService.sendMessage(queue.getQueueUrl(), oversized, 0));
assertTrue(ex.getMessage().contains("2048"),
"Error message must reference the queue's configured MaximumMessageSize, got: " + ex.getMessage());
}
@Test
void sendMessage_attributesCountTowardsLimit() {
Queue queue = sqsService.createQueue("attr-limit-queue",
Map.of("MaximumMessageSize", "2048"));
String body = "x".repeat(2040);
Map<String, MessageAttributeValue> attrs = Map.of(
"key", new MessageAttributeValue("value", "String"));
// Body alone fits; body + attributes exceed the 2048 byte limit.
assertThrows(AwsException.class,
() -> sqsService.sendMessage(queue.getQueueUrl(), body, 0, null, null, attrs, "us-east-1"));
}
@Test
void purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {
final var sns = mock(SnsService.class);