简体   繁体   中英

MassTransit & RabbitMQ - How to verify that messaged got processed inside of containerized consumer?

I am working on integration tests for my project. I have containerized RabbitMQ queue, containerized consumer of this queue (using MassTransit) and containerized API that this consumer calls during processing of the message.

My test pushes a message to the queue, it gets picked up by the consumer and here is where my problem comes in - is there a way to check when this consumer inside of container proccesed the message, from my test perspective?
For now I just used Thread.Sleep() for 10 seconds and run my assertions after that. It works but, obviously, as the number of tests grow this is becoming tedious...

How about using the actual rabbitmq REST API for that? From an integration test, you could use basic authentication, and query the queue endpoint, eg

/api/queues/%2F/foo

to query the queue foo on the default virtual host / (url-encoded as %2F). This will give you back a JSON data structure with the same details that you can see via the UI (in fact the UI is using this API as well), like the below (heavily truncated).

{
    "messages": 1,
    "messages_ready": 1,
    "messages_unacknowledged": 0
}

You can poll this endpoint until messages is equal to 0.

Here is how I eventually resolved this:

I didn't mention that I also have Seq server running as I didn't think it would be relevant in my case but turned out it was. At the end of message consuming method I have put log that I have enriched with custom property that indicates that this log means that message has been processed. Then in my integration test I set Polly policy that asks Seq for this "processing finished" log (with correct message id of course) until it appears (or until set timeout). However, I feel like @Driedas answer is way simpler

Maybe this can help you

https://event-driven.io/en/testing_asynchronous_processes_with_a_little_help_from_do.net_channels/

Allows for awaiting rabbitmq events from within the tests without doing thread.Sleep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM