Pregunta

Say you have an AWS SQS queue. Your Publisher class and Receiver class is already unit tested.

Now you have a worker process that uses the Receiver class to actually receive the messages from SQS. My question how do you usually test that worker process in a automated way just like unit testing?

¿Fue útil?

Solución

The strategy for unit testing a worker process that listens for messages on a queue is the same as unit testing any other component. You need to hide the message receiver and/or queue behind one or more interfaces, and then use dependency injection to pass those objects to your worker class. This allows you to mock the message queue and receiver in your unit tests.

Really, any object that references a resource out of the current process or thread on the machine should be hidden behind an interface of some sort so you can mock it.

Setting up these mocks might be a little complex, but you'll get fine grained control over the state of the system. For a process as complex as processing messages on a queue where you need to worry about duplicate messages, dropped messages — and worse yet, partial messages, then you'll want to unit test your worker process for robustness as much as possible.

public class MessageReceiver : IMessageReceiver
{
    // ...
}

public class Worker
{
    private IMessageReceiver receiver;

    public Worker(IMessageReceiver receiver)
    {
        this.receiver = receiver;
    }

    // ...
}

The important thing to remember is the Receiver needs to implement an interface, and your Worker needs to accept an instance of the Receiver typed to its interface.

Then your unit test can use an object mocking framework to mock the Receiver, or you can implement your own test Receiver:

public class TestMessageReceiver : IMessageReceiver
{
    // Fake the methods and logic
}

And your test would look something like:

var receiver = new TestMessageReceiver();
var worker = new Worker(receiver);

receiver.SetUpSomeDate(some, arguments);
worker.DoWork();

Assert.IsTrue(receiver.EmailSent);
Licenciado bajo: CC-BY-SA con atribución
scroll top