Skip to content

Commit e02a12f

Browse files
author
Damian Nardelli
committed
ISSUE-30: fix - ignorePayloadNotFound was not checked before deleting the SQS message
1 parent 43f4957 commit e02a12f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/main/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ public ReceiveMessageResult receiveMessage(ReceiveMessageRequest receiveMessageR
369369
try {
370370
origMsgBody = getTextFromS3(s3MsgBucketName, s3MsgKey);
371371
} catch (AmazonServiceException e) {
372-
if (((AmazonServiceException) e.getCause()).getErrorCode().equals("NoSuchKey")) {
372+
if (((AmazonServiceException) e.getCause()).getErrorCode().equals("NoSuchKey")
373+
&& clientConfiguration.isIgnorePayloadNotFound()) {
373374
deleteMessage(receiveMessageRequest.getQueueUrl(), message.getReceiptHandle());
374375
LOG.warn("SQS message deleted as it could not be found in S3");
375376
continue;

src/test/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClientTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,38 @@ public void testWhenIgnorePayloadNotFoundIsSentThenNotFoundKeysInS3AreDeletedInS
265265
Assert.assertEquals("receipt-handle", deleteMessageRequestArgumentCaptor.getValue().getReceiptHandle());
266266
}
267267

268+
@Test
269+
public void testWhenIgnorePayloadNotFoundIsNotSentThenNotFoundKeysInS3AreNotDeletedInSQS() throws Exception {
270+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
271+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);
272+
273+
AmazonServiceException mockException = mock(AmazonServiceException.class);
274+
when(mockException.getErrorCode()).thenReturn("NoSuchKey");
275+
276+
Message mockMessage = mock(Message.class);
277+
MessageS3Pointer messageS3Pointer = new MessageS3Pointer();
278+
when(mockMessage.getBody()).thenReturn(new JsonDataConverter().serializeToJson(messageS3Pointer));
279+
when(mockMessage.getReceiptHandle()).thenReturn("receipt-handle");
280+
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
281+
messageAttributes.put(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME, new MessageAttributeValue());
282+
when(mockMessage.getMessageAttributes()).thenReturn(messageAttributes);
283+
284+
ReceiveMessageResult mockReceiveMessageResult = mock(ReceiveMessageResult.class);
285+
when(mockSqsBackend.receiveMessage(any(ReceiveMessageRequest.class))).thenReturn(mockReceiveMessageResult);
286+
when(mockReceiveMessageResult.getMessages()).thenReturn(Collections.singletonList(mockMessage));
287+
288+
doThrow(mockException).when(mockS3).getObject(any(GetObjectRequest.class));
289+
290+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
291+
292+
try {
293+
sqsExtended.receiveMessage(SQS_QUEUE_URL);
294+
Assert.fail("exception should have been thrown");
295+
} catch (AmazonServiceException e) {
296+
verify(mockSqsBackend, never()).deleteMessage(any(DeleteMessageRequest.class));
297+
}
298+
}
299+
268300
private String generateStringWithLength(int messageLength) {
269301
char[] charArray = new char[messageLength];
270302
Arrays.fill(charArray, 'x');

0 commit comments

Comments
 (0)