Skip to content

Commit

Permalink
Fixes #25: Only add message attribute name if not already there
Browse files Browse the repository at this point in the history
If someone re-uses the ReceiveMessageRequest object for multiple
calls to AmazonSQSExtendedClient, the latter will add the
RESERVED_ATTRIBUTE_NAME String as a message attribute name every
time. This can cause 413 / Request Entity Too Large errors from
AWS SQS over time.
  • Loading branch information
dsw2127 committed Nov 27, 2017
1 parent 28afe4d commit edd84df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ public ReceiveMessageResult receiveMessage(ReceiveMessageRequest receiveMessageR
return super.receiveMessage(receiveMessageRequest);
}

receiveMessageRequest.getMessageAttributeNames().add(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
if (!receiveMessageRequest.getMessageAttributeNames().contains(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME)) {
receiveMessageRequest.getMessageAttributeNames().add(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
}

ReceiveMessageResult receiveMessageResult = super.receiveMessage(receiveMessageRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry;
import com.amazonaws.services.sqs.model.SendMessageRequest;
Expand Down Expand Up @@ -138,6 +140,24 @@ public void testWhenSendMessageWithSetMessageSizeThresholdThenThresholdIsHonored
verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}

@Test
public void testReceiveMessageMultipleTimesDoesNotAdditionallyAlterReceiveMessageRequest() {
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
when(mockSqsBackend.receiveMessage(isA(ReceiveMessageRequest.class))).thenReturn(new ReceiveMessageResult());

ReceiveMessageRequest messageRequest = new ReceiveMessageRequest();
ReceiveMessageRequest expectedRequest = new ReceiveMessageRequest()
.withMessageAttributeNames(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);

sqsExtended.receiveMessage(messageRequest);
Assert.assertEquals(expectedRequest, messageRequest);

sqsExtended.receiveMessage(messageRequest);
Assert.assertEquals(expectedRequest, messageRequest);
}

@Test
public void testWhenMessageBatchIsSentThenOnlyMessagesLargerThanThresholdAreStoredInS3() {
// This creates 10 messages, out of which only two are below the threshold (100K and 200K),
Expand Down

0 comments on commit edd84df

Please sign in to comment.