Skip to content

Commit 1f6d235

Browse files
tomkerkhoverynowak
authored andcommitted
Health Checks - EF health check should respect configured failureStatus
Signed-off-by: Tom Kerkhove <[email protected]>
1 parent 026b9df commit 1f6d235

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/Middleware/HealthChecks.EntityFrameworkCore/src/DbContextHealthCheck.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -49,8 +49,8 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
4949
{
5050
return HealthCheckResult.Healthy();
5151
}
52-
53-
return HealthCheckResult.Unhealthy();
52+
53+
return new HealthCheckResult(context.Registration.FailureStatus);
5454
}
5555
}
5656
}

src/Middleware/HealthChecks.EntityFrameworkCore/test/DbContextHealthCheckTest.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -61,7 +61,7 @@ public async Task CheckAsync_CustomTest_Healthy()
6161
}
6262

6363
[Fact]
64-
public async Task CheckAsync_CustomTest_Degraded()
64+
public async Task CheckAsync_CustomTestWithDegradedFailureStatusSpecified_Degraded()
6565
{
6666
// Arrange
6767
var services = CreateServices(async (c, ct) =>
@@ -78,12 +78,12 @@ public async Task CheckAsync_CustomTest_Degraded()
7878
var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, });
7979

8080
// Assert
81-
Assert.Equal(HealthStatus.Unhealthy, result.Status);
81+
Assert.Equal(HealthStatus.Degraded, result.Status);
8282
}
8383
}
8484

8585
[Fact]
86-
public async Task CheckAsync_CustomTest_Unhealthy()
86+
public async Task CheckAsync_CustomTestWithUnhealthyFailureStatusSpecified_Unhealthy()
8787
{
8888
// Arrange
8989
var services = CreateServices(async (c, ct) =>
@@ -104,12 +104,34 @@ public async Task CheckAsync_CustomTest_Unhealthy()
104104
}
105105
}
106106

107+
[Fact]
108+
public async Task CheckAsync_CustomTestWithNoFailureStatusSpecified_Unhealthy()
109+
{
110+
// Arrange
111+
var services = CreateServices(async (c, ct) =>
112+
{
113+
return 0 < await c.Blogs.CountAsync();
114+
}, failureStatus: null);
115+
116+
using (var scope = services.GetRequiredService<IServiceScopeFactory>().CreateScope())
117+
{
118+
var registration = Assert.Single(services.GetRequiredService<IOptions<HealthCheckServiceOptions>>().Value.Registrations);
119+
var check = ActivatorUtilities.CreateInstance<DbContextHealthCheck<TestDbContext>>(scope.ServiceProvider);
120+
121+
// Act
122+
var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, });
123+
124+
// Assert
125+
Assert.Equal(HealthStatus.Unhealthy, result.Status);
126+
}
127+
}
128+
107129
// used to ensure each test uses a unique in-memory database
108130
private static int _testDbCounter;
109131

110132
private static IServiceProvider CreateServices(
111133
Func<TestDbContext, CancellationToken, Task<bool>> testQuery = null,
112-
HealthStatus failureStatus = HealthStatus.Unhealthy)
134+
HealthStatus? failureStatus = HealthStatus.Unhealthy)
113135
{
114136
var serviceCollection = new ServiceCollection();
115137
serviceCollection.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase("Test" + Interlocked.Increment(ref _testDbCounter)));

0 commit comments

Comments
 (0)