Dockerized集成测试ASP.NET Core应用程序与RabbitMQ的连接

时间:2019-11-08 04:32:49

标签: c# docker asp.net-core rabbitmq

我对dockerized集成测试世界还很陌生,所以如果问题听起来很琐碎,请提前道歉。

我有一个与dockerized RabbitMQ实例通信的ASP.NET Core应用程序。当我启动应用程序进行调试时,docker-compose将创建该应用程序和RabbitMQ的容器,然后该应用程序就可以建立与RabbitMQ服务的连接。但是,在运行集成测试时,该应用程序无法建立与RabbitMQ服务的连接。

对于集成测试,我确实有一个单独的docker-compose,它启动了RabbitMQ服务,我在运行集成测试之前先运行它。

测试和应用程序的设置非常简单,并且遵循常见的设置,并且定义为:

public class WebAPITest : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _factory;

    public WebAPITest(WebApplicationFactory<Program> factory)
    {
        _factory = factory;
    }

    [Fact]
    public async Task IfConsumerReceives()
    {
        var client = _factory.CreateClient();
        var response = await client.GetAsync("/api/requestitems");

        // assert
        response.EnsureSuccessStatusCode();
    }
}

该应用程序具有RabbitMQ服务,该服务在Startup类中的定义如下:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc(options => options.EnableEndpointRouting = false)
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

    RegisterEventBus(services);
    services.AddOptions();
}

private void RegisterEventBus(IServiceCollection services)
{
    services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
    {
        var factory = new ConnectionFactory()
        {
            HostName = Configuration["RabbitMQConnection:HostName"],
            Port = Configuration.GetValue<int>("RabbitMQConnection:Port"),
            DispatchConsumersAsync = true
        };

        factory.UserName = Configuration["RabbitMQConnection:UserName"];
        factory.Password = Configuration["RabbitMQConnection:Password"];
        var retryCount = int.Parse(Configuration["RabbitMQConnection:RetryCount"]);
        return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
    });

    var queueName = Configuration["QueueName"];
    services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
    {
        var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
        var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
        var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
        var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();

        var retryCount = int.Parse(Configuration["RabbitMQConnection:RetryCount"]);

        return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, queueName, retryCount);
    });

    services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpsRedirection();
    app.UseMvc();
    ConfigureEventBus(app);
}

private void ConfigureEventBus(IApplicationBuilder app)
{
    // The following fails to establish connection to RabbitMQ when running integration tests.
    var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
    eventBus.Subscribe<CollectionCompletedIntegrationEvent, CollectionCompletedIntegrationEventHandler>();
}

我收到的错误消息是:

  

RabbitMQ.Client.Exceptions.BrokerUnreachableException:'指定的端点均不可访问'

任何建议都将受到赞赏。


更新1

根据要求,docker撰写文件如下所示;但是,我不确定在运行集成测试时是否会调用这些文件,仅在调试时会调用它们。

  • docker-compose
version: '3.4'

services:

  sql.data:
    image: microsoft/mssql-server-linux:2017-latest

  rabbitmq:
    image: rabbitmq:3-management-alpine

  myapp:
    image: ${DOCKER_REGISTRY-}myapp
    build:
      context: .
      dockerfile: myapp/Dockerfile
    depends_on:
      - sql.data
      - rabbitmq
  • docker-compose.override.yml
version: '3.4'

services:
  sql.data:
    environment:
      - SA_PASSWORD=Pass_word
      - ACCEPT_EULA=Y
    ports:
      - "1433:1433"

  rabbitmq:
    ports:
      - "15672:15672"
      - "5672:5672"

  myapp:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44376
    ports:
      - "61936:80"
      - "44376:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

0 个答案:

没有答案