3

Trying to write some tests for my AWS SQS Queue and its associated Dead letter queue. I want to somehow in my tests force the message from the queue to its DLQ, then read from the dlq to see if the message is there.

Reading from the DLQ is no problem. But does anyone know a quick and easy way I can programmatically force an sqs queue to send a message to its associated DLQ?

Slippy
  • 93
  • 1
  • 10
  • Did you try simply getting it from the original queue and writing it to the associated DLQ (which is just another queue)? – jarmod Feb 16 '22 at 20:34
  • I could do that, but that would defeat the purpose of a test. What I am trying to do is test that the SQS queue will move messages to its associated DLQ. I am new to AWS though, and I am unsure of when an SQS queue goes about moving its messages to a DLQ. I know it is related to the maxReceiveCount of the queue – Slippy Feb 16 '22 at 20:44
  • Ah, I see. Read the section on [Understanding SQS dead-letter queues (DLQs)](https://aws.amazon.com/blogs/compute/introducing-amazon-simple-queue-service-dead-letter-queue-redrive-to-source-queues/): "When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, SQS moves the message to the DLQ." You could trigger a dummy Lambda function from the queue and have your function simply throw an exception (or otherwise indicate error) every time. – jarmod Feb 16 '22 at 20:52

1 Answers1

8

The Dead Letter Queue is simply a SQS Queue, so you could send a message to it like you would any other queue.

The DLQ is configured when you create your normal queue and you need to pass a arn of a queue that will be used as DLQ.

When you configure your DLQ you set the maxReceiveCount (Maximum receives on the console) that is the number of times a message is delivered to the source queue before being moved to the dead-letter queue. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, Amazon SQS moves the message to the dead-letter-queue.

enter image description here

If want to test the process to send messages to DLQs, you need to force in your tests an error on the queue messages' processing to send a message to the DLQ queue, this will be the best way to understand if the errors are going to the queue correctly.

The process to send messages to the DLQ can be done in the following ways:

  • You explicitly send a message to the DLQ, if you found some error and do not want to process the message or delete at that time.

  • If you read the messages more times than the maxReceiveCount and do not process the message (read and delete from the queue) the AWS SQS service will understand that you are having problems on that message and will send automatically to the DLQ for you. (eg. maxReceiveCount equals 1 and your read the message and did not delete 2 times)

To understand more about DLQs, take a look here: Amazon SQS dead-letter queues.

Harsha pulikollu
  • 2,386
  • 15
  • 28
valdeci
  • 13,962
  • 6
  • 55
  • 80
  • Any suggestions on how I can force that error to occur? Wouldn't the error be occurring on the AWS servers? I am unsure how I can get a sendMessage request to successfully go through, but also cause an error once it arrives to AWS servers – Slippy Feb 16 '22 at 20:46
  • 3
    I updated my answer to explain how DLQs works and how you can do what you want – valdeci Feb 16 '22 at 21:09