对使用异步调用的 MassTransit 消费者进行单元测试

Unit testing MassTransit consumers that make utilize asynchronous calls

我们正在为我们的微服务架构使用 MassT运行sit 异步消息传递(在 RabbitMQ 之上)。

我们 运行 解决了测试消费者的问题,这些消费者反过来会进行异步调用。

下面的示例显示了一个简单的 MassT运行sit 消费者,它使用 RestSharp 进行出站调用并利用了 ExecuteAsync 异步方法。

public class VerifyPhoneNumberConsumer : Consumes<VerifyPhoneNumber>.Context
{
    IRestClient _restClient;
    RestRequest _request;
    PhoneNumber _phoneNumber;
    PhoneNumberVerificationResponse _responseData;

    public VerifyPhoneNumberConsumer(IRestClient client)
    {
        _restClient = client;
    }

    public void Consume(IConsumeContext<VerifyPhoneNumber> context)
    {
        try
        {
            //we can do some standard message verification/validation here 

            _restClient.ExecuteAsync<PhoneNumberVerificationResponse>(_request, (response) =>
            {
                //here we might do some standard response verification

                _responseData = response.Data;

                _phoneNumber = new PhoneNumber()
                {
                    Number = _responseData.PhoneNumber
                };

                context.Respond(new VerifyPhoneNumberSucceeded(context.Message)
                {
                    PhoneNumber = _phoneNumber
                });
            });
        }
        catch (Exception exception)
        {
            context.Respond(new VerifyPhoneNumberFailed(context.Message)
            {
                PhoneNumber = context.Message.PhoneNumber,
                Message = exception.Message
            });
        }
    }
}

此示例单元测试可能如下所示:

[TestFixture]
public class VerifyPhoneNumberConsumerTests
{
    private VerifyPhoneNumberConsumer _consumer;
    private PhoneNumber _phoneNumber;
    private RestResponse _response;
    private VerifyPhoneNumber _command;

    private AutoResetEvent _continuationEvent;
    private const int CONTINUE_WAIT_TIME = 1000;

    [SetUp]
    public void Initialize()
    {
        _continuationEvent = new AutoResetEvent(false);
        _mockRestClient = new Mock<IRestClient>();
        _consumer = new VerifyPhoneNumberConsumer(_mockRestClient.Object);
        _response = new RestResponse();
        _response.Content = "Response Test Content";
        _phoneNumber = new PhoneNumber()
        {
            Number = "123456789"
        };
        _command = new VerifyPhoneNumber(_phoneNumber);
    }

    [Test]
    public  void VerifyPhoneNumber_Succeeded()
    {
        var test = TestFactory.ForConsumer<VerifyPhoneNumberConsumer>().New(x =>
        {
            x.ConstructUsing(() => _consumer);
            x.Send(_command, (scenario, context) => context.SendResponseTo(scenario.Bus));
        });

        _mockRestClient.Setup(
            c =>
            c.ExecuteAsync(Moq.It.IsAny<IRestRequest>(),
                                                            Moq.It
                                                               .IsAny<Action<IRestResponse<PhoneNumberVerificationResponse>, RestRequestAsyncHandle>>()))
                                                               .Callback<IRestRequest, Action<IRestResponse<PhoneNumberVerificationResponse>, RestRequestAsyncHandle>>((
                                                                   request, callback) =>
                                                               {
                                                                   var responseMock = new Mock<IRestResponse<PhoneNumberVerificationResponse>>();
                                                                   responseMock.Setup(r => r.Data).Returns(GetSuccessfulVericationResponse());
                                                                   callback(responseMock.Object, null);
                                                                   _continuationEvent.Set();
                                                               });


        test.Execute();

        _continuationEvent.WaitOne(CONTINUE_WAIT_TIME);

        Assert.IsTrue(test.Sent.Any<VerifyPhoneNumberSucceeded>());
    }

    private PhoneNumberVerificationResponse GetSuccessfulVericationResponse()
    {
        return new PhoneNumberVerificationResponse
            {
                PhoneNumber = _phoneNumber
            };
    }
}

由于在消费者中调用了 ExecuteAsync 方法,如果我们在它发出信号(或超时)之前不放一些东西来阻止它,这个测试方法就会失败).在上面的示例中,我们使用 AutoResetEvent 从回调发出信号以继续和 运行 断言。

这是一个糟糕的方法,我们正在用尽所有资源来寻找替代方法。如果它不明显,这可能会在测试期间导致错误的失败和竞争条件。更不用说潜在的严重自动化测试时间了。

我们有哪些比目前更好的替代方案。

编辑这是我最初用于模拟 RestSharp 异步调用的来源。

How to test/mock RestSharp ExecuteAsync(...)

老实说,执行异步方法的复杂性是 MassTransit 3 的关键驱动因素之一。虽然它还没有准备好,但它使来自消费者的异步方法调用变得更好。

您在上面测试的是什么,因为您在 REST 客户端上调用 ExecuteAsync(),而不是在消费者中等待响应(使用 .Result 或 .Wait),HTTP 调用在消息消费者已返回。所以这可能是你问题的一部分。

在 MT3 中,此消费者将写为:

public async Task Consume(ConsumeContext<VerifyPhoneNumber> context)
{
    try
    {
        var response = await _restClient
            .ExecuteAsync<PhoneNumberVerificationResponse>(_request);
        var phoneNumber = new PhoneNumber()
        {
            Number = response.PhoneNumber
        };

        await context.RespondAsync(new VerifyPhoneNumberSucceeded(context.Message)
        {
            PhoneNumber = _phoneNumber
        });
    }
    catch (Exception exception)
    {
        context.Respond(new VerifyPhoneNumberFailed(context.Message)
        {
            PhoneNumber = context.Message.PhoneNumber,
            Message = exception.Message
        });
    }        
}

我能够想出以下解决方案,它看起来更加优雅和合适。如果我的假设有误,请随时纠正我。

我修改了消费者中的 RestSharp 执行,因此我的消费者看起来如下所示:

public class VerifyPhoneNumberConsumer : Consumes.Context { IRestClient _restClient; 休息请求_请求; 电话号码_电话号码; PhoneNumberVerificationResponse _responseData;

public VerifyPhoneNumberConsumer(IRestClient client)
{
    _restClient = client;
}

public void Consume(IConsumeContext<VerifyPhoneNumber> context)
{
    try
    {
        //we can do some standard message verification/validation here 

        var response = await _restClient.ExecuteGetTaskAsync<PhoneNumberVerificationResponse>(_request);

        _responseData = response.Data;

        _phoneNumber = new PhoneNumber()
        {
            Number = _responseData.PhoneNumber
        };
    }
    catch (Exception exception)
    {
        context.Respond(new VerifyPhoneNumberFailed(context.Message)
        {
            PhoneNumber = context.Message.PhoneNumber,
            Message = exception.Message
        });
    }
}

}

这利用了 RestSharp 的 TPL 异步功能,因此我不必自己动手。

因此,我可以将我的测试代码更改为以下内容:

[Test]
public void VerifyPhoneNumber_Succeeded()
{
    var test = TestFactory.ForConsumer<VerifyPhoneNumberConsumer>().New(x =>
    {
        x.ConstructUsing(() => _consumer);
        x.Send(_command, (scenario, context) => context.SendResponseTo(scenario.Bus));
    });

    var response = (IRestResponse<PhoneNumberVerificationResponse>)new RestResponse<PhoneNumberVerificationResponse>();
    response.Data = GetSuccessfulVericationResponse();

    var taskResponse = Task.FromResult(response);
    Expect.MethodCall(
        () => _client.ExecuteGetTaskAsync<PhoneNumberVerificationResponse>(Any<IRestRequest>.Value.AsInterface))
          .Returns(taskResponse);

    test.Execute();

    Assert.IsTrue(test.Sent.Any<VerifyPhoneNumberSucceeded>());
}