fix(sqs): FIFO ReceiveMessage returns multiple messages per group per call (#785)

* fix(sqs): FIFO ReceiveMessage returns multiple messages per group per call

A FIFO ReceiveMessage call may legitimately return multiple messages
from the same MessageGroupId, in insertion order, up to
MaxNumberOfMessages. Floci was capping each call at one message per
group, which in practice limited tests to 1-2 messages regardless of
how many were queued and broke ordering tests for high-throughput
("fair") FIFO queues distributing across groups.

The cross-call group lock is unchanged: a group with an in-flight
message from a previous call is still blocked until that message is
deleted or its visibility expires. Only the within-call constraint
was wrong.

Existing tests that asserted the buggy "one per group per call"
contract have been updated to assert AWS-correct behavior.

Refs #768.

* test(sqs): address PR review feedback on FIFO multi-message tests

- Loosen cross-group ordering assertions in FIFO receive tests:
  FIFO only guarantees ordering within a MessageGroupId, not across
  groups, so assert group1 messages are in-order relative to each
  other and group2 message is present without fixing its position.
- Rename fifoQueue_groupLockBlocksAcrossCallsButNotWithin to
  fifoQueueGroupLockBlocksAcrossCallsButNotWithin to match the
  test class's lowerCamelCase convention.

* test(sqs): add FIFO partial-drain unlock and compat coverage

Address review feedback on PR #785:

- SqsServiceTest: add fifoQueueGroupUnlocksAfterAllInFlightMessagesDeleted
  covering the most common consumer pattern — drain less than a full
  group in one call, delete the in-flight messages, then receive the
  remainder. Verifies the group lock unlocks once all in-flight
  messages from that group are deleted.
- sdk-test-java SqsTest: add fifoReceiveReturnsMultipleMessagesFromSameGroupInOneCall
  as the first FIFO test in the compatibility suite. Sends 3 messages
  to a single MessageGroupId and asserts a single ReceiveMessage with
  MaxNumberOfMessages=10 returns all three — closes the gap noted in
  the review and provides a regression guard against real AWS.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-14 06:52:51 -05:00
1 parent ffd7779c5c
commit 3bf16e3fb9
4 files changed
+115 -27

No files matched your search

@@ -167,7 +167,10 @@ class GuardedMessageQueueTest {
// --- FIFO ---
@Test
void fifoClaimRespectsGroupOrdering() {
void fifoClaimReturnsMultipleMessagesPerGroupWithinSingleCall() {
// AWS FIFO: a single ReceiveMessage may return multiple messages from
// the same MessageGroupId, preserving insertion order. The cross-call
// group lock applies only against future calls.
Message g1m1 = new Message("g1-msg1");
g1m1.setMessageGroupId("group1");
Message g1m2 = new Message("g1-msg2");
@@ -179,13 +182,18 @@ class GuardedMessageQueueTest {
queue.addMessage(g1m2);
queue.addMessage(g2m1);
// Should get first from each group
var first = queue.claimVisibleMessages(10, 30, true, -1, null);
assertEquals(2, first.claimed().size());
assertEquals("g1-msg1", first.claimed().get(0).getBody());
assertEquals("g2-msg1", first.claimed().get(1).getBody());
assertEquals(3, first.claimed().size(),
"Single FIFO ReceiveMessage should return all visible messages up to MaxNumberOfMessages");
List<String> bodies = first.claimed().stream().map(Message::getBody).toList();
// Inter-group ordering is not guaranteed by FIFO; only within-group order is.
assertTrue(bodies.contains("g2-msg1"), "batch must contain group2 message");
int g1m1Idx = bodies.indexOf("g1-msg1");
int g1m2Idx = bodies.indexOf("g1-msg2");
assertTrue(g1m1Idx >= 0 && g1m2Idx >= 0, "batch must contain both group1 messages");
assertTrue(g1m1Idx < g1m2Idx, "group1 messages must be in insertion order");
// Both groups blocked
// All groups now have in-flight messages — second call returns empty
var second = queue.claimVisibleMessages(10, 30, true, -1, null);
assertTrue(second.claimed().isEmpty());
}
@@ -277,27 +277,29 @@ class SqsServiceTest {
}
@Test
void fifoQueueReceiveRespectsGroupOrdering() {
void fifoQueueReceiveReturnsMultipleMessagesPerGroupInOrder() {
// AWS FIFO: a single ReceiveMessage call may return multiple messages
// from the same MessageGroupId (in order), up to MaxNumberOfMessages.
// The group lock only blocks subsequent ReceiveMessage calls.
Queue queue = sqsService.createQueue("test.fifo", null);
sqsService.sendMessage(queue.getQueueUrl(), "g1-msg1", 0, "group1", "d1");
sqsService.sendMessage(queue.getQueueUrl(), "g1-msg2", 0, "group1", "d2");
sqsService.sendMessage(queue.getQueueUrl(), "g2-msg1", 0, "group2", "d3");
// First receive: should get one message per group (first from each)
List<Message> first = sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0);
assertEquals(2, first.size());
assertEquals("g1-msg1", first.get(0).getBody());
assertEquals("g2-msg1", first.get(1).getBody());
assertEquals(3, first.size(),
"Single FIFO ReceiveMessage should drain all visible messages up to MaxNumberOfMessages");
List<String> bodies = first.stream().map(Message::getBody).toList();
// Inter-group ordering is not guaranteed by FIFO; only within-group order is.
assertTrue(bodies.contains("g2-msg1"), "batch must contain group2 message");
int g1m1Idx = bodies.indexOf("g1-msg1");
int g1m2Idx = bodies.indexOf("g1-msg2");
assertTrue(g1m1Idx >= 0 && g1m2Idx >= 0, "batch must contain both group1 messages");
assertTrue(g1m1Idx < g1m2Idx, "group1 messages must be in insertion order");
// Second receive: group1 and group2 both have in-flight messages, so nothing returned
// Both groups are now in-flight; second call returns empty.
List<Message> second = sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0);
assertTrue(second.isEmpty());
// Delete the group1 message, then receive again — should get g1-msg2
sqsService.deleteMessage(queue.getQueueUrl(), first.get(0).getReceiptHandle());
List<Message> third = sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0);
assertEquals(1, third.size());
assertEquals("g1-msg2", third.get(0).getBody());
}
@Test
@@ -553,6 +555,47 @@ class SqsServiceTest {
assertEquals("AWS.SimpleQueueService.NonExistentQueue", ex.getErrorCode());
}
@Test
void fifoQueueGroupLockBlocksAcrossCallsButNotWithin() {
Queue queue = sqsService.createQueue("group-lock.fifo", null);
for (int i = 1; i <= 5; i++) {
sqsService.sendMessage(queue.getQueueUrl(), "msg" + i, 0, "g1", "d" + i);
}
// First call drains all five from the single group.
List<Message> first = sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0);
assertEquals(5, first.size());
// Second call returns nothing because g1 is in-flight.
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0).isEmpty());
}
@Test
void fifoQueueGroupUnlocksAfterAllInFlightMessagesDeleted() {
Queue queue = sqsService.createQueue("unlock-test.fifo", null);
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, "g1", "d1");
sqsService.sendMessage(queue.getQueueUrl(), "msg2", 0, "g1", "d2");
sqsService.sendMessage(queue.getQueueUrl(), "msg3", 0, "g1", "d3");
// Partial drain: MaxNumberOfMessages=2 returns msg1 + msg2
List<Message> first = sqsService.receiveMessage(queue.getQueueUrl(), 2, 30, 0);
assertEquals(2, first.size());
assertEquals("msg1", first.get(0).getBody());
assertEquals("msg2", first.get(1).getBody());
// Group still locked — msg3 is not returned
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0).isEmpty());
// Delete both in-flight messages → group unlocks
sqsService.deleteMessage(queue.getQueueUrl(), first.get(0).getReceiptHandle());
sqsService.deleteMessage(queue.getQueueUrl(), first.get(1).getReceiptHandle());
// Now msg3 should be available
List<Message> third = sqsService.receiveMessage(queue.getQueueUrl(), 10, 30, 0);
assertEquals(1, third.size());
assertEquals("msg3", third.get(0).getBody());
}
@Test
void purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {
final var sns = mock(SnsService.class);