Skip to content

Commit

Permalink
Fix race condition in passive health test (#2473)
Browse files Browse the repository at this point in the history
* Fix race condition in passive health test

* Remove nullable annotations in test
  • Loading branch information
MihaZupan committed Apr 16, 2024
1 parent 6838eea commit 6e95e58
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions test/ReverseProxy.FunctionalTests/PassiveHealthCheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
public async Task PassiveHealthChecksEnabled_MultipleDestinationFailures_ProxyReturnsServiceUnavailable()
{
var destinationReached = false;
IProxyStateLookup lookup = null;
string clusterId = null;

var test = new TestEnvironment(
context =>
Expand All @@ -74,20 +76,51 @@ public async Task PassiveHealthChecksEnabled_MultipleDestinationFailures_ProxyRe
}
};
clusterId = c.ClusterId;
return (c, r);
},
ConfigureProxy = proxyBuilder => proxyBuilder.Services.AddSingleton<IForwarderHttpClientFactory>(new MockHttpClientFactory((_, _) => throw new IOException())),
ConfigureProxyApp = proxyApp =>
{
lookup = proxyApp.ApplicationServices.GetRequiredService<IProxyStateLookup>();
},
};

await test.Invoke(async uri =>
{
using var client = new HttpClient();
for (var i = 0; i < 42; i++)
for (var i = 0; i < 10; i++)
{
using var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
}
Assert.NotNull(lookup);
Assert.NotNull(clusterId);
// The destination list will be updated asynchronously in the background.
// Wait until that update takes effect.
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
while (true)
{
Assert.True(lookup.TryGetCluster(clusterId, out var cluster));
Assert.Single(cluster.DestinationsState.AllDestinations);
Assert.Equal(i < 10 ? HttpStatusCode.BadGateway : HttpStatusCode.ServiceUnavailable, response.StatusCode);
await Task.Yield();
if (cluster.DestinationsState.AvailableDestinations.Count == 0)
{
break;
}
await Task.Delay(10, cts.Token);
}
for (var i = 0; i < 42; i++)
{
using var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
}
});

Expand Down

0 comments on commit 6e95e58

Please sign in to comment.