3
votes

We have a service which listens to a Azure service bus queue. We have another service which listens to the dead letter queue of this queue. We also have a series of tests which run against our Azure application when it has have been deployed.

We would like to extend our automated tests so that the parts that deal with the dead letters are also tested, but are not sure how we can write tests which will ensure that messages get to the dead letter queue, as under normal circumstances messages will not dead letter.

currently we are testing this manually, but I feel like this is not a long term option.

any ideas?

2

2 Answers

5
votes

According to the Service Bus docs, if you set EnableDeadLetteringOnMessageExpiration to true, if a message expires it goes to the dead letter queue. So, you can do:

var desc = new QueueDescription(QueueName);
desc.EnableDeadLetteringOnMessageExpiration = true;
desc.DefaultMessageTimeToLive = TimeSpan.FromSeconds(1); // one second is the min value

You can test by just waiting > 1 second to ensure that the message goes to the dead letter queue.

Edit: You can also dead letter a received message by doing receivedMessage.DeadLetter(). This will automatically place it in the dead letter queue.

5
votes

In Service Bus API, Microsoft doesn't provide ability to send message to dead letter queue straight away without putting message to the main queue. If you try to create MessageSender using path to dead letter queue, it will fail with exception.

You need to mock out your main queue receiver with fake implementation that invokes BrokeredMessage.DeadLetter() method that transfers message to dead letter queue.