Xunit Integration Test-Async method not completing when tests run in parallel

46 views Asked by At

I am using .net 7 and xunit to do integration tests.

I have over 50+ test cases, each test case in a file so that my tests run in parallel. All my test methods are marked with async Task since I need to send an http request and do my assertions based on the result

The project uses the following:

    <PackageReference Include="FluentAssertions" Version="6.12.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.12" />
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.12" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="Serilog" Version="3.1.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
    <PackageReference Include="xunit" Version="2.7.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">

here is a sample code:


 public class ItemShouldEnterTheDb
    {
    private readonly IDbService _dbService;
    private readonly IHttpService _httpService;

    public ItemShouldEnterTheDb()
    {
     var serviceProvider = new WebApplicationFactory<Program>().Services.CreateScope().ServiceProvider;
    _dbService= serviceProvider.GetRequiredService<IDbService >();
    _httpService= serviceProvider.GetRequiredService<IHttpService >();
    }

    [Fact]
    public async Task Item_Should_Enter_The_Db()
    {
       var responseFromHttp= await _httpService.SendRequest(Params))); 
       var dbResult= await _dbService.GetItems(responseFromHttp.Id); //does not wait for the results here
      
       ShoppingCartTestHelper.AssertResults(dbResult); // skips to assertion causing my tests to pass when they should not

    }
}

The http service has the following method:

public async Task<CustomResponse> SendRequest(CustomParameter customParameter)
 {
    CustomResponse customResponse= null;
    using var gdiHttpClient = _httpClientFactory.CreateClient("ShoppingCart");
    {
       var httpResponseMessage =
       await gdiHttpClient.PostAsync(customParameter.Uri, customParameter.Content);

       var response = await httpResponseMessage.Content.ReadAsStringAsync();

       if (httpResponseMessage.StatusCode == HttpStatusCode.Accepted)
       {
          customResponse= Serializer.DeserializeJson<CustomResponse >(response);
       }
        return customResponse;
       }
    }
 }

When my tests run in parallel, I notice that the line:

  var dbResult= await _dbService.GetItems(responseFromHttp.Id); //does not wait for the results here

does not complete execution. I changed _dbService.GetItems(responseFromHttp.Id) to synchronous and still the same behaviour.

This happens mainly on azure devops visual studio test runner

I had the call to the db synchronous and the issue still persists.

I have used the IAsyncLifetime but I noticed that the dispose method is executed before the actual test completes

What could be the cause and how can I resolve this?

0

There are 0 answers