diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java index 149ceadafb..1f490af709 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java @@ -317,6 +317,21 @@ public class SqsService { "Message body must be shorter than " + maxMessageSize + " bytes.", 400); } + int queueDelaySeconds = parseDelaySecondsAttribute(queue.getAttributes().get("DelaySeconds")); + + // Resolve the effective delay: + // - FIFO queues only support queue-level DelaySeconds per AWS SQS, + // so any per-message value is ignored and we always use the + // queue attribute. Without this, FIFO silently dropped the + // queue-level default (issue #475). + // - Standard queues honor per-message DelaySeconds when provided + // (> 0). Applying the queue-level default on the standard path + // requires distinguishing "omitted" from "explicit 0" in the + // handlers, which the current int-parameter API cannot express; + // that's left as follow-up work -- this patch only addresses + // the FIFO regression called out in the issue. + int effectiveDelaySeconds = queue.isFifo() ? queueDelaySeconds : delaySeconds; + // FIFO queue validation if (queue.isFifo()) { if (messageGroupId == null || messageGroupId.isEmpty()) { @@ -353,6 +368,9 @@ public class SqsService { message.setMessageGroupId(messageGroupId); message.setMessageDeduplicationId(dedupId); message.setSequenceNumber(sequenceCounter.incrementAndGet()); + if (effectiveDelaySeconds > 0) { + message.setVisibleAt(Instant.now().plusSeconds(effectiveDelaySeconds)); + } if (messageAttributes != null && !messageAttributes.isEmpty()) { message.getMessageAttributes().putAll(messageAttributes); message.updateMd5OfMessageAttributes(); @@ -367,8 +385,8 @@ public class SqsService { // Standard queue Message message = new Message(body); - if (delaySeconds > 0) { - message.setVisibleAt(Instant.now().plusSeconds(delaySeconds)); + if (effectiveDelaySeconds > 0) { + message.setVisibleAt(Instant.now().plusSeconds(effectiveDelaySeconds)); } if (messageAttributes != null && !messageAttributes.isEmpty()) { message.getMessageAttributes().putAll(messageAttributes); @@ -381,6 +399,23 @@ public class SqsService { return message; } + /** + * Parse the queue-level DelaySeconds attribute. Returns 0 when the + * attribute is null, empty, non-numeric, or negative -- the queue falls + * back to "no default delay" rather than failing the SendMessage call. + */ + private int parseDelaySecondsAttribute(String value) { + if (value == null || value.isEmpty()) { + return 0; + } + try { + int parsed = Integer.parseInt(value); + return parsed > 0 ? parsed : 0; + } catch (NumberFormatException e) { + return 0; + } + } + private void notifyReceivers(String storageKey) { Object lock = queueLocks.get(storageKey); if (lock != null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java index fbc7cde855..f6715ecf25 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java @@ -311,4 +311,38 @@ class SqsServiceTest { List third = sqsService.receiveMessage(queue.getQueueUrl(), 1, -1, 0); assertEquals(1, third.size(), "Message should become visible after queue's VisibilityTimeout (1s), not global default (30s)"); } + + // --- Queue-level DelaySeconds for FIFO queues (issue #475) --- + + @Test + void queueLevelDelaySecondsAppliesToFifoQueue() { + Queue queue = sqsService.createQueue("delay-fifo.fifo", + Map.of("ContentBasedDeduplication", "true", "DelaySeconds", "1")); + sqsService.sendMessage(queue.getQueueUrl(), "msg", 0, "group1", null); + + List immediate = sqsService.receiveMessage(queue.getQueueUrl(), 1, 0, 0); + assertTrue(immediate.isEmpty(), + "FIFO queue should honor queue-level DelaySeconds (issue #475)"); + + try { Thread.sleep(1100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + + List later = sqsService.receiveMessage(queue.getQueueUrl(), 1, 0, 0); + assertEquals(1, later.size(), + "Message should become visible once DelaySeconds elapses"); + } + + @Test + void fifoQueueIgnoresPerMessageDelaySeconds() { + // AWS SQS FIFO queues only support queue-level DelaySeconds; any + // per-message value is ignored. Here the queue default is 0 and the + // caller passes a positive per-message delay -- the message must be + // immediately visible. + Queue queue = sqsService.createQueue("fifo-ignores-per-msg.fifo", + Map.of("ContentBasedDeduplication", "true")); + sqsService.sendMessage(queue.getQueueUrl(), "msg", 60, "group1", null); + + List immediate = sqsService.receiveMessage(queue.getQueueUrl(), 1, 0, 0); + assertEquals(1, immediate.size(), + "FIFO queues must ignore per-message DelaySeconds"); + } }