<#470 NativeInterop.CleanupMockServer() takes more...
# pact-net
g
#470 NativeInterop.CleanupMockServer() takes more than 1 minute Issue created by sbekeris The test that normally runs in 4 seconds sometimes takes 1.3 min. After debugging pact-net I see that a call to NativeInterop.CleanupMockServer() sometimes takes so long. Any ideas how to fix it? Sample code: https://github.com/sbekeris/slow-tests The sample contains multiple identical tests, and some of them take 1.3 min. You may need to run it multiple times, as it is happening not every time.

image

Tests source code:
Copy code
[Fact]
public async Task Test01() => await Test();

private static async Task Test()
{
    var api1PactBuilder = CreatePactBuilder("API-1", 9091);
    AddGetRequest(api1PactBuilder, "/api-1-endpoint-1");
    AddGetRequest(api1PactBuilder, "/api-1-endpoint-2");

    var api2PactBuilder = CreatePactBuilder("API-2", 9092);
    AddGetRequest(api2PactBuilder, "/api-2-endpoint-1");

    await api1PactBuilder.VerifyAsync(async _ =>
    {
        await api2PactBuilder.VerifyAsync(async _ =>
        {
            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("/slow-tests", UriKind.RelativeOrAbsolute));
            var httpClient = new WebApplicationFactory<Program>().CreateClient();
            await httpClient.SendAsync(request);
        });
    });
}

private static void AddGetRequest(IPactBuilderV3 pactBuilder, string url)
{
    pactBuilder
        .UponReceiving("Test")
        .Given("Test")
            .WithRequest(HttpMethod.Get, url)
        .WillRespond();
}

private static IPactBuilderV3 CreatePactBuilder(string provider, int port)
{
    var pact = Pact.V3("Test", provider);

    return pact.WithHttpInteractions(port);
}
API source code:
Copy code
public class SlowTestsController : ControllerBase
{
    [HttpGet()]
    public async Task<IActionResult> Get()
    {
        var httpClient = new HttpClient();

        var api1Endpoint2Task = HttpRequest(httpClient, "<http://localhost:9091/api-1-endpoint-1>");
        var api1Endpoint3Task = HttpRequest(httpClient, "<http://localhost:9091/api-1-endpoint-2>");
        await Task.WhenAll(api1Endpoint2Task, api1Endpoint3Task);

        await HttpRequest(httpClient, "<http://localhost:9092/api-2-endpoint-1>");

        return Ok();
    }

    public async Task HttpRequest(HttpClient httpClient, string url)
    {
        using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
        await httpClient.SendAsync(request);
    }
}
pact-foundation/pact-net
👀 1