简体   繁体   English

MassTransit & RabbitMQ - 如何验证消息已在容器化消费者内部得到处理?

[英]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.我已经容器化了 RabbitMQ 队列,这个队列的容器化消费者(使用 MassTransit)和容器化了这个消费者在处理消息期间调用的 API。

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.现在我只使用Thread.Sleep() 10 秒,然后运行我的断言。 It works but, obviously, as the number of tests grow this is becoming tedious...它有效,但显然,随着测试数量的增加,这变得乏味......

How about using the actual rabbitmq REST API for that?为此使用实际的 rabbitmq REST API 怎么样? 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).在默认虚拟主机/ (url 编码为 %2F)上查询队列foo 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).这将为您返回一个 JSON 数据结构,其中包含您可以通过 UI 看到的相同详细信息(实际上 UI 也在使用这个 API),如下所示(被严重截断)。

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

You can poll this endpoint until messages is equal to 0.您可以轮询此端点,直到messages等于 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.我没有提到我还运行了 Seq 服务器,因为我认为它与我的情况无关,但事实证明确实如此。 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).然后在我的集成测试中,我设置了 Polly 策略,要求 Seq 提供此“处理完成”日志(当然具有正确的消息 ID),直到它出现(或直到设置超时)。 However, I feel like @Driedas answer is way simpler但是,我觉得@Driedas 的回答更简单

Maybe this can help you也许这可以帮助你

https://event-driven.io/en/testing_asynchronous_processes_with_a_little_help_from_do.net_channels/ 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允许在测试中等待 rabbitmq 事件而不执行 thread.Sleep

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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