Masstransit 如何测试/调试不响应的消费者?
Masstransit how to test / debug a consumer which doesn't response?
在测试项目中我想执行CallbackReceivedEventHandler
中的代码
所以我试图向这个消费者“发布”一些东西,但最终没有调用消费者。我做错了什么?
我不得不让消费者响应一些东西然后在测试项目中使用 RequestClient...这不是一个好的解决方法...消费者不应该响应任何东西
请帮忙
申请项目
public class CallbackReceivedEventHandler : IConsumer<CallbackReceivedEvent>
{
public async Task Consume(ConsumeContext<CallbackReceivedEvent> context)
{
//Breakpoint is here
...
await context.RespondAsync(new CallbackReceivedEventResponse()); // I want to remove this thing
}
测试项目
provider = new ServiceCollection()
.AddMassTransitInMemoryTestHarness(cfg =>
{
configurator.AddConsumers(typeof(CallbackReceivedEventHandler));
})
.AddGenericRequestClient()
.BuildServiceProvider(true);
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.Start();
var bus = provider.GetRequiredService<IBus>();
CallbackReceivedEvent input = new()
// NOT WORK
await bus.Publish(input); //KO! the consumer is not called (breakpoint in Consume is not hit)
// WORK!
var requester = bus.CreateRequestClient<CallbackReceivedEvent>();
await requester.GetResponse<CallbackReceivedEventResponse>(input); //OK! the consumer is called (breakpoint in Consume is hit)
您的测试很可能在消息发送给消费者之前完成。消息是异步传递的。
在您的测试中,发布后,您可以等待测试工具上的 InactivityTask 以确保消息已发送。或者您可以等待消息被使用。两种方法都有效,但一种方法更具体,可确保消息已发布。
await bus.Publish(input);
await harness.Consumed.Any<CallbackReceivedEvent>();
在测试项目中我想执行CallbackReceivedEventHandler
中的代码
所以我试图向这个消费者“发布”一些东西,但最终没有调用消费者。我做错了什么?
我不得不让消费者响应一些东西然后在测试项目中使用 RequestClient...这不是一个好的解决方法...消费者不应该响应任何东西
请帮忙
申请项目
public class CallbackReceivedEventHandler : IConsumer<CallbackReceivedEvent>
{
public async Task Consume(ConsumeContext<CallbackReceivedEvent> context)
{
//Breakpoint is here
...
await context.RespondAsync(new CallbackReceivedEventResponse()); // I want to remove this thing
}
测试项目
provider = new ServiceCollection()
.AddMassTransitInMemoryTestHarness(cfg =>
{
configurator.AddConsumers(typeof(CallbackReceivedEventHandler));
})
.AddGenericRequestClient()
.BuildServiceProvider(true);
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.Start();
var bus = provider.GetRequiredService<IBus>();
CallbackReceivedEvent input = new()
// NOT WORK
await bus.Publish(input); //KO! the consumer is not called (breakpoint in Consume is not hit)
// WORK!
var requester = bus.CreateRequestClient<CallbackReceivedEvent>();
await requester.GetResponse<CallbackReceivedEventResponse>(input); //OK! the consumer is called (breakpoint in Consume is hit)
您的测试很可能在消息发送给消费者之前完成。消息是异步传递的。
在您的测试中,发布后,您可以等待测试工具上的 InactivityTask 以确保消息已发送。或者您可以等待消息被使用。两种方法都有效,但一种方法更具体,可确保消息已发布。
await bus.Publish(input);
await harness.Consumed.Any<CallbackReceivedEvent>();