Apache Camel,如何测试带有Wiretap的路由?

huangapple go评论47阅读模式
英文:

Apache Camel, how to test route with wiretaps?

问题

以下是您要翻译的内容:

我想为具有wireTaps的路由编写一个测试。

from("direct:inhouse")
.wireTap("direct:persist")
.process(...)
.unmarshal()
.wireTap("direct:transferToX")
.to("direct:createResponse")
.marshal()
.process()
.wireTap("direct:persist")

在某些端点上,wireTaps发送消息,使用uuidService生成UUIDs。
我的测试模拟了uuid服务,每次请求下一个uuid时返回一个递增的uuid,如"uuid-1","uuid-2"等。

由于wireTaps不以有保证的顺序执行,我无法编写一个稳定的测试来检查消息头是否包含预期的UUID。

我正在寻找一种以路由中定义的顺序执行wireTap路径的方法。

我尝试使用AdviceWithRouteBuilder来替换wireTaps()为multicast()来实现这一点,
但发现这不够通用,因为我必须显式定义multicast的端点。
如果有一种方式可以让multicast将消息发送到与它替换的wireTap将要使用的相同端点,那将起作用。但我找不到配置它的方法。

英文:

I want to write a test for a route with wireTaps.

from("direct:inhouse")
.wireTap("direct:persist")
.process(...)
.unmarshal()
.wireTap("direct:transferToX")
.to("direct:createResponse")
.marshal()
.process()
.wireTap("direct:persist")

At some of the endpoints, to which the wireTaps send messages, a uuidService is used to generate UUIDs.
My Test mocks the uuid service, to return an incremented uuid every time it is asked for the next uuid like so "uuid-1", "uuid-2" and so on.

Since the wireTaps are not executed in a guaranteed order, I can't write a stable test to check, whether the messages headers contain the expected UUIDs.

I'm looking for a way to have the wireTap paths executed in the order in which they are defined in the route.

I tried to achieve this with an AdviceWithRouteBuilder which replaces wireTaps() with multicast(),
but found this not to be generic enough, since I have to explicitly define the endpoints for the multicast.
If there was a way to have the multicast send the message to the same endpoint that the wireTap (it replaces) would have used, that could work. But I couldn't find a way to configure that.

答案1

得分: 1

使用executorService选项,您可以为wiretap提供一个使其同步执行的执行器服务:

.wireTap("direct:persist").executorService(new SynchronousExecutorService())

您还可以指定从注册表中查找的服务的名称,这在运行时查找,允许您在测试和生产中使用不同的实现:

.wireTap("direct:persist").executorService("mySynchronousExecutorService")
英文:

Using the executorService option, you could give the wiretap an executor service that makes it execute synchronously:

.wireTap("direct:persist").executorService(new SynchronousExecutorService())

You could also specify the name of the service from the registry, which is looked up at runtime, allowing you to use different implementations for test and production

.wireTap("direct:persist").executorService("mySynchronousExecutorService")

答案2

得分: 0

假设您的UUID可能是简单的字符串(而不是真正格式化良好的UUID),一种替代方法是使您的测试与顺序无关。
例如,通过使用位掩码。

3个窃听器 => 使用3位掩码

001,即1
010,即2
100,即4

因此,通过在不同的窃听器模拟中分别添加+1、+2和+4,您可以无论执行顺序如何。

  • 检查所有窃听器是否被调用,如果最终的UUID是7(=1+2+4)。
  • 随时知道已经被调用的窃听器(通过测试掩码值)。
英文:

Assuming your UUID may be simple string (and not a real, well formatted UUID), one alternative could be to make your test order-independant.
For instance, by using bitmask.

3 wiretaps => use a 3-digits mask

001 which is 1
010 which is 2
100 which is 4

So by adding respectively +1, +2, and +4 in the different wiretap mocks, you could, whatever the execution order.

  • check all wiretaps were invoked if the final UUID is 7 (=1+2+4)
  • know at any moment which have been already invoked (by testing the mask value)

huangapple
  • 本文由 发表于 2023年4月17日 18:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定