Health Checks - EF health check should respect configured failureStatus
Signed-off-by: Tom Kerkhove <kerkhove.tom@gmail.com>
This commit is contained in:
parent
026b9df3e0
commit
1f6d235a5e
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -49,8 +49,8 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
{
|
{
|
||||||
return HealthCheckResult.Healthy();
|
return HealthCheckResult.Healthy();
|
||||||
}
|
}
|
||||||
|
|
||||||
return HealthCheckResult.Unhealthy();
|
return new HealthCheckResult(context.Registration.FailureStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -61,7 +61,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CheckAsync_CustomTest_Degraded()
|
public async Task CheckAsync_CustomTestWithDegradedFailureStatusSpecified_Degraded()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var services = CreateServices(async (c, ct) =>
|
var services = CreateServices(async (c, ct) =>
|
||||||
|
|
@ -78,12 +78,12 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, });
|
var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, });
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HealthStatus.Unhealthy, result.Status);
|
Assert.Equal(HealthStatus.Degraded, result.Status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CheckAsync_CustomTest_Unhealthy()
|
public async Task CheckAsync_CustomTestWithUnhealthyFailureStatusSpecified_Unhealthy()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var services = CreateServices(async (c, ct) =>
|
var services = CreateServices(async (c, ct) =>
|
||||||
|
|
@ -104,12 +104,34 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CheckAsync_CustomTestWithNoFailureStatusSpecified_Unhealthy()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var services = CreateServices(async (c, ct) =>
|
||||||
|
{
|
||||||
|
return 0 < await c.Blogs.CountAsync();
|
||||||
|
}, failureStatus: null);
|
||||||
|
|
||||||
|
using (var scope = services.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||||
|
{
|
||||||
|
var registration = Assert.Single(services.GetRequiredService<IOptions<HealthCheckServiceOptions>>().Value.Registrations);
|
||||||
|
var check = ActivatorUtilities.CreateInstance<DbContextHealthCheck<TestDbContext>>(scope.ServiceProvider);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, });
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HealthStatus.Unhealthy, result.Status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// used to ensure each test uses a unique in-memory database
|
// used to ensure each test uses a unique in-memory database
|
||||||
private static int _testDbCounter;
|
private static int _testDbCounter;
|
||||||
|
|
||||||
private static IServiceProvider CreateServices(
|
private static IServiceProvider CreateServices(
|
||||||
Func<TestDbContext, CancellationToken, Task<bool>> testQuery = null,
|
Func<TestDbContext, CancellationToken, Task<bool>> testQuery = null,
|
||||||
HealthStatus failureStatus = HealthStatus.Unhealthy)
|
HealthStatus? failureStatus = HealthStatus.Unhealthy)
|
||||||
{
|
{
|
||||||
var serviceCollection = new ServiceCollection();
|
var serviceCollection = new ServiceCollection();
|
||||||
serviceCollection.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase("Test" + Interlocked.Increment(ref _testDbCounter)));
|
serviceCollection.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase("Test" + Interlocked.Increment(ref _testDbCounter)));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue