gpt4 book ai didi

c# - 公共(public)交通如何测试/调试不响应的消费者?

转载 作者:行者123 更新时间:2023-12-05 05:51:51 28 4
gpt4 key购买 nike

在测试项目中,我想执行 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>();

关于c# - 公共(public)交通如何测试/调试不响应的消费者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70307540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com