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();
|
var duration = stopwatch.GetElapsedTime();
|
||||||
entry = new HealthReportEntry(
|
entry = new HealthReportEntry(
|
||||||
status: HealthStatus.Unhealthy,
|
status: registration.FailureStatus,
|
||||||
description: "A timeout occurred while running check.",
|
description: "A timeout occurred while running check.",
|
||||||
duration: duration,
|
duration: duration,
|
||||||
exception: ex,
|
exception: ex,
|
||||||
|
|
@ -136,7 +136,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
{
|
{
|
||||||
var duration = stopwatch.GetElapsedTime();
|
var duration = stopwatch.GetElapsedTime();
|
||||||
entry = new HealthReportEntry(
|
entry = new HealthReportEntry(
|
||||||
status: HealthStatus.Unhealthy,
|
status: registration.FailureStatus,
|
||||||
description: ex.Message,
|
description: ex.Message,
|
||||||
duration: duration,
|
duration: duration,
|
||||||
exception: ex,
|
exception: ex,
|
||||||
|
|
|
||||||
|
|
@ -531,6 +531,40 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
Assert.False(hangs);
|
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)
|
private static DefaultHealthCheckService CreateHealthChecksService(Action<IHealthChecksBuilder> configure)
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
|
|
@ -571,5 +605,13 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
return Task.FromResult(HealthCheckResult.Healthy(data: data));
|
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