如何防止使用RabbitMQ TestContainer在Springboot集成测试中窃取消息?
想象以下设置:
- 一个带有RabbitListener的Springboot申请与RabbitMQ TestContainer测试的队列
- 两个Springboot Integration测试
A进行一些任意的测试用例,需要RabbitMQ 测试B具有略有不同的测试配置,并通过RabbitMQ将消息从测试代码发送到应用程序,并期望发生某些事情。
由于具有不同的配置,测试A将具有与测试B不同(和缓存的)春季上下文。
测试A运行时,始于兔子MQ消费者。测试后,弹簧上下文被缓存,兔子消费者仍在运行。
当测试B运行时,第二个兔子MQ消费者将在第二个春季上下文中开始。现在测试发送消息时,有时是从上下文中的消费者“窃取”消息,因此测试B失败了。
我只能“修复”它,但是使用@dirtiesContext
运行所有集成测试,但这使得测试套件非常慢。
还有其他事情可以防止这种情况吗?是否有某种春季上下文“停顿”可以暂时阻止消费者,直到将上下文重复使用时恢复它们?
Imagine the following setup:
- A springBoot Applicaiton with a RabbitListener to a queue
- Two SpringBoot integration tests with RabbitMQ testcontainer
Test A does some arbitrary test cases which require RabbitMQ
Test B has a slightly different test configuration and sends a message from the test code to the application via RabbitMQ and expects something to happen.
Because of the different configuration, Test A will have a different (and cached) spring context than Test B.
When Test A runs, a Rabbit MQ consumer is started. After the test, the Spring context is cached and the rabbit consumer is still running.
When then Test B runs, a second Rabbit MQ consumer is started in the second spring context. When now the test sends a message, sometimes the consumer from context A "steals" the message and thus the test B fails.
I could only "fix" that but running all integration tests with RabbitMQ with @DirtiesContext
but that make the test suite very very slow.
Is there any other thing that can be done to prevent that? Is there some kind of spring context "pause" that can be called to stop the consumers temporarily until they are resumed when the context is reused?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论