mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): keep the visibility a function sets on failed SQS messages (#9003)
This commit is contained in:
1 parent
07f93dc8e5
commit
ede9601b82
6 files changed
+281
-24
No files matched your search
@@ -22,11 +22,14 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -126,9 +129,14 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
/** Messages received across earlier polls that a batching window is still holding open. */
|
||||
private static final class PendingBatch {
|
||||
final List<Message> messages = new ArrayList<>();
|
||||
final Map<String, ReceivedMessage> receivedById = new HashMap<>();
|
||||
long windowStartedAtMs;
|
||||
}
|
||||
|
||||
/** The receipt handle and visibility one receive gave a message. */
|
||||
private record ReceivedMessage(String receiptHandle, Instant visibleAt) {
|
||||
}
|
||||
|
||||
public void startPolling(EventSourceMapping esm) {
|
||||
if (timerIds.containsKey(esm.getUuid())) {
|
||||
return; // already polling
|
||||
@@ -201,13 +209,16 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
esm.getQueueUrl(), Math.min(10, wanted), visibilityTimeout, 0, esm.getRegion());
|
||||
|
||||
List<Message> messages;
|
||||
Map<String, ReceivedMessage> receivedById;
|
||||
if (window <= 0) {
|
||||
// No window, or one that was just turned off: deliver now, draining anything a
|
||||
// previous positive window had buffered so those messages are not stranded
|
||||
// until their visibility expires (and cannot be replayed if the window returns).
|
||||
List<Message> batch = pending != null ? pending.messages : new ArrayList<>();
|
||||
receivedById = pending != null ? pending.receivedById : new HashMap<>();
|
||||
pendingBatches.remove(esm.getUuid());
|
||||
batch.addAll(received);
|
||||
recordReceipts(received, receivedById);
|
||||
if (batch.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -221,6 +232,7 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
pending.windowStartedAtMs = clockMs.getAsLong();
|
||||
}
|
||||
pending.messages.addAll(received);
|
||||
recordReceipts(received, pending.receivedById);
|
||||
if (pending.messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -231,6 +243,7 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
return;
|
||||
}
|
||||
messages = new ArrayList<>(pending.messages);
|
||||
receivedById = new HashMap<>(pending.receivedById);
|
||||
pendingBatches.remove(esm.getUuid());
|
||||
}
|
||||
|
||||
@@ -307,12 +320,12 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
if (!failedIds.isEmpty()) {
|
||||
List<Message> toReturn = matched.stream()
|
||||
.filter(m -> failedIds.contains(m.getMessageId())).toList();
|
||||
returnMessagesToQueue(esm, toReturn);
|
||||
returnMessagesToQueue(esm, toReturn, receivedById);
|
||||
}
|
||||
} else {
|
||||
LOG.warnv("ESM {0}: Lambda returned error [{1}], returning {2} delivered message(s) to queue for retry/redrive",
|
||||
esm.getUuid(), result.getFunctionError(), matched.size());
|
||||
returnMessagesToQueue(esm, matched);
|
||||
returnMessagesToQueue(esm, matched, receivedById);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ESM {0}: poll/invoke error: {1} ({2})",
|
||||
@@ -334,13 +347,30 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
* 0 would cause for a persistently failing function) — so ApproximateReceiveCount
|
||||
* climbs and the queue's RedrivePolicy moves them to the DLQ once
|
||||
* {@code maxReceiveCount} is exceeded.
|
||||
*
|
||||
* <p>A message whose visibility changed during the invocation keeps it, as on AWS, where the
|
||||
* mapping leaves a failed message's visibility alone: the function may have called
|
||||
* ChangeMessageVisibility, or another consumer may have received the message since. The reset
|
||||
* uses the handle and visibility this mapping's receive recorded, never the live message's,
|
||||
* which a later receive replaces.
|
||||
*/
|
||||
private void returnMessagesToQueue(EventSourceMapping esm, List<Message> messages) {
|
||||
private void returnMessagesToQueue(EventSourceMapping esm, List<Message> messages,
|
||||
Map<String, ReceivedMessage> receivedById) {
|
||||
int retryVisibility = retryVisibilityTimeout(esm);
|
||||
for (Message msg : messages) {
|
||||
ReceivedMessage receipt = receivedById.get(msg.getMessageId());
|
||||
if (receipt == null) {
|
||||
LOG.warnv("ESM {0}: no receive recorded for message {1}, leaving its visibility as it is",
|
||||
esm.getUuid(), msg.getMessageId());
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
sqsService.changeMessageVisibility(
|
||||
esm.getQueueUrl(), msg.getReceiptHandle(), retryVisibility, esm.getRegion());
|
||||
boolean returned = sqsService.changeMessageVisibilityIfUnchanged(esm.getQueueUrl(),
|
||||
receipt.receiptHandle(), receipt.visibleAt(), retryVisibility, esm.getRegion());
|
||||
if (!returned) {
|
||||
LOG.debugv("ESM {0}: message {1} keeps the visibility set during the invocation",
|
||||
esm.getUuid(), msg.getMessageId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ESM {0}: failed to return message {1} to queue: {2}",
|
||||
esm.getUuid(), msg.getMessageId(), e.getMessage());
|
||||
@@ -348,6 +378,17 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Received messages are the queue's live instances, which a ChangeMessageVisibility call or a
|
||||
* later receive updates during the invocation, so the handle and visibility each receive gave
|
||||
* them are recorded separately.
|
||||
*/
|
||||
private static void recordReceipts(List<Message> received, Map<String, ReceivedMessage> receivedById) {
|
||||
for (Message msg : received) {
|
||||
receivedById.put(msg.getMessageId(), new ReceivedMessage(msg.getReceiptHandle(), msg.getVisibleAt()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The visibility timeout to apply when returning a failed message to the queue: the
|
||||
* queue's configured {@code VisibilityTimeout}, or the AWS default of 30s when unset
|
||||
|
||||
@@ -178,7 +178,7 @@ class GuardedMessageQueue {
|
||||
}
|
||||
}
|
||||
|
||||
enum HandleResult { APPLIED, MESSAGE_GONE, HANDLE_EXPIRED }
|
||||
enum HandleResult { APPLIED, MESSAGE_GONE, HANDLE_EXPIRED, VISIBILITY_CHANGED }
|
||||
|
||||
record Removal(HandleResult result, Message message) {
|
||||
}
|
||||
@@ -196,8 +196,25 @@ class GuardedMessageQueue {
|
||||
}
|
||||
|
||||
HandleResult changeVisibility(ReceiptHandle handle, int visibilityTimeout, boolean fifo) {
|
||||
return changeVisibility(handle, null, visibilityTimeout, fifo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the message's visibility only while it is still {@code expectedVisibleAt}, so a change
|
||||
* made since (by a ChangeMessageVisibility call or another receive) is kept. A null
|
||||
* {@code expectedVisibleAt} changes it unconditionally.
|
||||
*
|
||||
* <p>The visibility is compared before the handle is checked: after another receive, a FIFO
|
||||
* queue would reject the caller's older handle as expired, but the outcome the caller asked
|
||||
* for, leaving a changed message alone, is the same.
|
||||
*/
|
||||
HandleResult changeVisibility(ReceiptHandle handle, Instant expectedVisibleAt, int visibilityTimeout,
|
||||
boolean fifo) {
|
||||
try (Guard _ = hold()) {
|
||||
Message msg = findByMessageId(handle.messageId());
|
||||
if (msg != null && expectedVisibleAt != null && !expectedVisibleAt.equals(msg.getVisibleAt())) {
|
||||
return HandleResult.VISIBILITY_CHANGED;
|
||||
}
|
||||
HandleResult result = checkHandle(msg, handle, fifo);
|
||||
if (result == HandleResult.APPLIED) {
|
||||
Instant previous = msg.getVisibleAt();
|
||||
|
||||
@@ -1712,11 +1712,22 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public void changeMessageVisibility(String queueUrl, String receiptHandle, int visibilityTimeout, String region) {
|
||||
changeMessageVisibility(queueUrl, receiptHandle, visibilityTimeout, region, false);
|
||||
changeMessageVisibility(queueUrl, receiptHandle, null, visibilityTimeout, region, false);
|
||||
}
|
||||
|
||||
private void changeMessageVisibility(String queueUrl, String receiptHandle, int visibilityTimeout,
|
||||
String region, boolean inBatch) {
|
||||
/**
|
||||
* Changes a message's visibility timeout unless its visibility is no longer
|
||||
* {@code expectedVisibleAt}: a consumer that called ChangeMessageVisibility since keeps the
|
||||
* timeout it set. Returns whether the timeout was changed.
|
||||
*/
|
||||
public boolean changeMessageVisibilityIfUnchanged(String queueUrl, String receiptHandle,
|
||||
Instant expectedVisibleAt, int visibilityTimeout,
|
||||
String region) {
|
||||
return changeMessageVisibility(queueUrl, receiptHandle, expectedVisibleAt, visibilityTimeout, region, false);
|
||||
}
|
||||
|
||||
private boolean changeMessageVisibility(String queueUrl, String receiptHandle, Instant expectedVisibleAt,
|
||||
int visibilityTimeout, String region, boolean inBatch) {
|
||||
String storageKey = regionKey(region, queueUrl);
|
||||
Queue queue = queueStore.get(storageKey)
|
||||
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
@@ -1725,12 +1736,14 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
|
||||
GuardedMessageQueue messages = getOrCreateQueue(storageKey);
|
||||
messages.activity().accessed();
|
||||
GuardedMessageQueue.HandleResult result = messages.changeVisibility(handle, visibilityTimeout, queue.isFifo());
|
||||
GuardedMessageQueue.HandleResult result =
|
||||
messages.changeVisibility(handle, expectedVisibleAt, visibilityTimeout, queue.isFifo());
|
||||
rejectUnusableHandle(result, receiptHandle, inBatch);
|
||||
if (result == GuardedMessageQueue.HandleResult.MESSAGE_GONE) {
|
||||
throw invalidReceiptHandle(receiptHandle,
|
||||
"Message does not exist or is not available for visibility timeout change", inBatch);
|
||||
}
|
||||
return result == GuardedMessageQueue.HandleResult.APPLIED;
|
||||
}
|
||||
|
||||
private static void rejectUnusableHandle(GuardedMessageQueue.HandleResult result, String receiptHandle,
|
||||
@@ -2096,7 +2109,7 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
List<BatchResultEntry> results = new ArrayList<>();
|
||||
for (ChangeVisibilityBatchEntry entry : entries) {
|
||||
try {
|
||||
changeMessageVisibility(queueUrl, entry.receiptHandle(), entry.visibilityTimeout(), region, true);
|
||||
changeMessageVisibility(queueUrl, entry.receiptHandle(), null, entry.visibilityTimeout(), region, true);
|
||||
results.add(new BatchResultEntry(entry.id(), true, null, null));
|
||||
} catch (AwsException e) {
|
||||
results.add(new BatchResultEntry(entry.id(), false, e.getErrorCode(), e.getMessage()));
|
||||
|
||||
@@ -338,8 +338,7 @@ class SqsEventSourcePollerTest {
|
||||
// The failed message must be made visible again after the queue's own visibility
|
||||
// timeout (2s here) so the next poll re-receives it and the queue's RedrivePolicy
|
||||
// can move it to the DLQ — rather than staying in-flight for fn.timeout + 30s.
|
||||
verify(sqsService).changeMessageVisibility(
|
||||
esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 2, "us-east-1");
|
||||
verify(sqsService, never()).deleteMessage(any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -413,8 +412,7 @@ class SqsEventSourcePollerTest {
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
|
||||
// Falls back to the AWS default of 30s rather than 0 (which would spin a tight retry loop).
|
||||
verify(sqsService).changeMessageVisibility(
|
||||
esm.getQueueUrl(), "rh-m1", 30, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 30, "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -436,7 +434,7 @@ class SqsEventSourcePollerTest {
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -733,7 +731,7 @@ class SqsEventSourcePollerTest {
|
||||
}
|
||||
// m1 deleted after successful invoke; nothing returned to the queue.
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -770,7 +768,7 @@ class SqsEventSourcePollerTest {
|
||||
// Filtered-out m2 is consumed (deleted) regardless of the invoke outcome.
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
// Matched m1 failed → returned to the queue (visibility reset), NOT deleted.
|
||||
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 5, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 5, "us-east-1");
|
||||
verify(sqsService, never()).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
}
|
||||
|
||||
@@ -794,7 +792,7 @@ class SqsEventSourcePollerTest {
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
// The stale failure id does not cause a second delete or a visibility reset.
|
||||
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -855,8 +853,8 @@ class SqsEventSourcePollerTest {
|
||||
|
||||
// AWS treats these as a complete failure: nothing is deleted, every delivered
|
||||
// message goes back to the queue for retry/redrive.
|
||||
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m1", null, 2, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m2", null, 2, "us-east-1");
|
||||
verify(sqsService, never()).deleteMessage(any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -872,7 +870,7 @@ class SqsEventSourcePollerTest {
|
||||
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(any(), any(), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -880,8 +878,100 @@ class SqsEventSourcePollerTest {
|
||||
EventSourceMapping esm = pollReportingBatch("{\"batchItemFailures\":[{\"itemIdentifier\":\"m2\"}]}");
|
||||
|
||||
verify(sqsService).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService).changeMessageVisibility(esm.getQueueUrl(), "rh-m2", 2, "us-east-1");
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(esm.getQueueUrl(), "rh-m2", null, 2, "us-east-1");
|
||||
verify(sqsService, never()).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(eq(esm.getQueueUrl()), eq("rh-m1"), anyInt(), any());
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(eq(esm.getQueueUrl()), eq("rh-m1"), any(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void partialBatchFailure_functionChangedVisibility_returnsTheMessageAgainstItsReceiveTimeVisibility() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
|
||||
Message msg = message("m1");
|
||||
msg.setVisibleAt(setByReceive);
|
||||
stubReceive(esm, List.of(msg));
|
||||
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
|
||||
.thenReturn(Map.of("VisibilityTimeout", "2"));
|
||||
InvokeResult result = new InvokeResult();
|
||||
result.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"m1\"}]}".getBytes());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(invocation -> {
|
||||
// The received message is the queue's live instance, so a ChangeMessageVisibility
|
||||
// call the function makes shows up on it.
|
||||
msg.setVisibleAt(setByReceive.plusSeconds(5));
|
||||
return result;
|
||||
});
|
||||
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(
|
||||
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
|
||||
verify(sqsService, never()).deleteMessage(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchingWindow_failedMessage_isReturnedAgainstTheVisibilityItsReceiveSet() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setBatchSize(2);
|
||||
esm.setMaximumBatchingWindowInSeconds(5);
|
||||
AtomicLong now = new AtomicLong(0);
|
||||
poller.setClockForTest(now::get);
|
||||
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
|
||||
Message msg = message("m1");
|
||||
msg.setVisibleAt(setByReceive);
|
||||
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(msg))
|
||||
.thenReturn(List.of());
|
||||
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
|
||||
.thenReturn(Map.of("VisibilityTimeout", "2"));
|
||||
InvokeResult failure = new InvokeResult();
|
||||
failure.setFunctionError("Handled");
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(invocation -> {
|
||||
msg.setVisibleAt(setByReceive.plusSeconds(5));
|
||||
return failure;
|
||||
});
|
||||
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
verify(executorService, never()).invoke(any(), any(), any());
|
||||
now.set(5000);
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(
|
||||
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void partialBatchFailure_messageReceivedAgainDuringInvocation_returnsItWithTheOriginalReceipt() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
Instant setByReceive = Instant.parse("2026-01-15T10:31:00Z");
|
||||
Message msg = message("m1");
|
||||
msg.setVisibleAt(setByReceive);
|
||||
stubReceive(esm, List.of(msg));
|
||||
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
|
||||
.thenReturn(Map.of("VisibilityTimeout", "2"));
|
||||
InvokeResult result = new InvokeResult();
|
||||
result.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"m1\"}]}".getBytes());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(invocation -> {
|
||||
// Another consumer receives the message once the function's short visibility
|
||||
// runs out, which gives the queue's live instance a new handle and visibility.
|
||||
msg.setReceiptHandle("rh-m1-second");
|
||||
msg.setVisibleAt(setByReceive.plusSeconds(30));
|
||||
return result;
|
||||
});
|
||||
|
||||
awaitPoll(poller.pollAndInvoke(esm));
|
||||
|
||||
verify(sqsService).changeMessageVisibilityIfUnchanged(
|
||||
esm.getQueueUrl(), "rh-m1", setByReceive, 2, "us-east-1");
|
||||
verify(sqsService, never()).changeMessageVisibilityIfUnchanged(
|
||||
eq(esm.getQueueUrl()), eq("rh-m1-second"), any(), anyInt(), any());
|
||||
}
|
||||
}
|
||||
@@ -150,6 +150,42 @@ class GuardedMessageQueueTest {
|
||||
assertEquals(1, reClaimed.claimed().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeVisibility_expectedVisibleAtNoLongerCurrent_keepsTheNewerVisibility() {
|
||||
queue.addMessage(new Message("msg"));
|
||||
Message claimed = queue.claimVisibleMessages(1, 30, false, -1, null).claimed().get(0);
|
||||
Instant setByClaim = claimed.getVisibleAt();
|
||||
ReceiptHandle handle = ReceiptHandle.parse(claimed.getReceiptHandle());
|
||||
assertEquals(HandleResult.APPLIED, queue.changeVisibility(handle, 60, false));
|
||||
|
||||
assertEquals(HandleResult.VISIBILITY_CHANGED, queue.changeVisibility(handle, setByClaim, 0, false));
|
||||
assertTrue(queue.claimVisibleMessages(1, 30, false, -1, null).claimed().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeVisibility_expectedVisibleAtStillCurrent_appliesTheTimeout() {
|
||||
queue.addMessage(new Message("msg"));
|
||||
Message claimed = queue.claimVisibleMessages(1, 30, false, -1, null).claimed().get(0);
|
||||
ReceiptHandle handle = ReceiptHandle.parse(claimed.getReceiptHandle());
|
||||
|
||||
assertEquals(HandleResult.APPLIED, queue.changeVisibility(handle, claimed.getVisibleAt(), 0, false));
|
||||
assertEquals(1, queue.claimVisibleMessages(1, 30, false, -1, null).claimed().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeVisibility_fifoMessageReceivedAgain_keepsTheNewReceiversVisibility() {
|
||||
Message message = new Message("msg");
|
||||
message.setMessageGroupId("group");
|
||||
queue.addMessage(message);
|
||||
Message first = queue.claimVisibleMessages(1, 0, true, -1, null).claimed().get(0);
|
||||
ReceiptHandle firstHandle = ReceiptHandle.parse(first.getReceiptHandle());
|
||||
Instant setByFirstClaim = first.getVisibleAt();
|
||||
assertEquals(1, queue.claimVisibleMessages(1, 30, true, -1, null).claimed().size());
|
||||
|
||||
assertEquals(HandleResult.VISIBILITY_CHANGED, queue.changeVisibility(firstHandle, setByFirstClaim, 0, true));
|
||||
assertTrue(queue.claimVisibleMessages(1, 30, true, -1, null).claimed().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeVisibilityInvalidReturnsFalse() {
|
||||
assertEquals(HandleResult.MESSAGE_GONE, queue.changeVisibility(ReceiptHandle.issue(null, "nonexistent", ReceiptHandle.DEFAULT_SECRET), 0, false));
|
||||
|
||||
@@ -940,6 +940,66 @@ class SqsServiceTest {
|
||||
assertEquals(1, reReceived.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeMessageVisibilityIfUnchanged_visibilityUnchangedSinceReceive_changesIt() {
|
||||
String region = "eu-west-1";
|
||||
Queue queue = sqsService.createQueue("if-unchanged", null, region);
|
||||
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
|
||||
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
|
||||
|
||||
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
|
||||
queue.getQueueUrl(), received.getReceiptHandle(), received.getVisibleAt(), 0, region);
|
||||
|
||||
assertTrue(changed);
|
||||
assertEquals(1, sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeMessageVisibilityIfUnchanged_consumerChangedVisibility_keepsTheConsumersTimeout() {
|
||||
String region = "eu-west-1";
|
||||
Queue queue = sqsService.createQueue("if-changed", null, region);
|
||||
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
|
||||
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
|
||||
Instant setByReceive = received.getVisibleAt();
|
||||
sqsService.changeMessageVisibility(queue.getQueueUrl(), received.getReceiptHandle(), 60, region);
|
||||
|
||||
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
|
||||
queue.getQueueUrl(), received.getReceiptHandle(), setByReceive, 0, region);
|
||||
|
||||
assertFalse(changed);
|
||||
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeMessageVisibilityIfUnchanged_messageReceivedAgain_keepsTheNewReceiversTimeout() {
|
||||
String region = "eu-west-1";
|
||||
Queue queue = sqsService.createQueue("if-received-again", null, region);
|
||||
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
|
||||
Message first = sqsService.receiveMessage(queue.getQueueUrl(), 1, 0, 0, region).getFirst();
|
||||
String firstHandle = first.getReceiptHandle();
|
||||
Instant setByFirstReceive = first.getVisibleAt();
|
||||
assertEquals(1, sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).size());
|
||||
|
||||
boolean changed = sqsService.changeMessageVisibilityIfUnchanged(
|
||||
queue.getQueueUrl(), firstHandle, setByFirstReceive, 0, region);
|
||||
|
||||
assertFalse(changed);
|
||||
assertTrue(sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeMessageVisibilityIfUnchanged_deletedMessage_isRejected() {
|
||||
String region = "eu-west-1";
|
||||
Queue queue = sqsService.createQueue("if-unchanged-gone", null, region);
|
||||
sqsService.sendMessage(queue.getQueueUrl(), "msg1", 0, region);
|
||||
Message received = sqsService.receiveMessage(queue.getQueueUrl(), 1, 30, 0, region).getFirst();
|
||||
Instant setByReceive = received.getVisibleAt();
|
||||
sqsService.deleteMessage(queue.getQueueUrl(), received.getReceiptHandle(), region);
|
||||
|
||||
assertThrows(AwsException.class, () -> sqsService.changeMessageVisibilityIfUnchanged(
|
||||
queue.getQueueUrl(), received.getReceiptHandle(), setByReceive, 0, region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQueueAttributes() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
Reference in new issue
Block a user