fix : failureStatus (#21175)
* fix : failureStatus use HealthCheckRegistration.FailureStatus when CheckHealthAsync failure Fixes https://github.com/dotnet/aspnetcore/issues/22503
This commit is contained in:
parent
50fb513fda
commit
2831aaa08a
|
|
@ -121,7 +121,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
|||
{
|
||||
var duration = stopwatch.GetElapsedTime();
|
||||
entry = new HealthReportEntry(
|
||||
status: HealthStatus.Unhealthy,
|
||||
status: registration.FailureStatus,
|
||||
description: "A timeout occurred while running check.",
|
||||
duration: duration,
|
||||
exception: ex,
|
||||
|
|
@ -136,7 +136,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
|||
{
|
||||
var duration = stopwatch.GetElapsedTime();
|
||||
entry = new HealthReportEntry(
|
||||
status: HealthStatus.Unhealthy,
|
||||
status: registration.FailureStatus,
|
||||
description: ex.Message,
|
||||
duration: duration,
|
||||
exception: ex,
|
||||
|
|
|
|||
|
|
@ -530,6 +530,40 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
|||
// Assert
|
||||
Assert.False(hangs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckHealthAsync_WithFailureStatus()
|
||||
{
|
||||
// Arrange
|
||||
var service = CreateHealthChecksService(b =>
|
||||
{
|
||||
b.AddCheck<FailCapturingCheck>("degraded", HealthStatus.Degraded);
|
||||
b.AddCheck<FailCapturingCheck>("healthy", HealthStatus.Healthy);
|
||||
b.AddCheck<FailCapturingCheck>("unhealthy", HealthStatus.Unhealthy);
|
||||
});
|
||||
|
||||
// Act
|
||||
var results = await service.CheckHealthAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Collection(
|
||||
results.Entries,
|
||||
actual =>
|
||||
{
|
||||
Assert.Equal("degraded", actual.Key);
|
||||
Assert.Equal(HealthStatus.Degraded, actual.Value.Status);
|
||||
},
|
||||
actual =>
|
||||
{
|
||||
Assert.Equal("healthy", actual.Key);
|
||||
Assert.Equal(HealthStatus.Healthy, actual.Value.Status);
|
||||
},
|
||||
actual =>
|
||||
{
|
||||
Assert.Equal("unhealthy", actual.Key);
|
||||
Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status);
|
||||
});
|
||||
}
|
||||
|
||||
private static DefaultHealthCheckService CreateHealthChecksService(Action<IHealthChecksBuilder> configure)
|
||||
{
|
||||
|
|
@ -571,5 +605,13 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
|||
return Task.FromResult(HealthCheckResult.Healthy(data: data));
|
||||
}
|
||||
}
|
||||
|
||||
private class FailCapturingCheck : IHealthCheck
|
||||
{
|
||||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new Exception("check failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue