diff --git a/src/Middleware/HealthChecks.EntityFrameworkCore/src/DbContextHealthCheck.cs b/src/Middleware/HealthChecks.EntityFrameworkCore/src/DbContextHealthCheck.cs index 7fa998f296..6481bd29d3 100644 --- a/src/Middleware/HealthChecks.EntityFrameworkCore/src/DbContextHealthCheck.cs +++ b/src/Middleware/HealthChecks.EntityFrameworkCore/src/DbContextHealthCheck.cs @@ -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. using System; @@ -49,8 +49,8 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks { return HealthCheckResult.Healthy(); } - - return HealthCheckResult.Unhealthy(); + + return new HealthCheckResult(context.Registration.FailureStatus); } } } diff --git a/src/Middleware/HealthChecks.EntityFrameworkCore/test/DbContextHealthCheckTest.cs b/src/Middleware/HealthChecks.EntityFrameworkCore/test/DbContextHealthCheckTest.cs index fe5383c6f5..df22c3361f 100644 --- a/src/Middleware/HealthChecks.EntityFrameworkCore/test/DbContextHealthCheckTest.cs +++ b/src/Middleware/HealthChecks.EntityFrameworkCore/test/DbContextHealthCheckTest.cs @@ -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. using System; @@ -61,7 +61,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks } [Fact] - public async Task CheckAsync_CustomTest_Degraded() + public async Task CheckAsync_CustomTestWithDegradedFailureStatusSpecified_Degraded() { // Arrange var services = CreateServices(async (c, ct) => @@ -78,12 +78,12 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks var result = await check.CheckHealthAsync(new HealthCheckContext() { Registration = registration, }); // Assert - Assert.Equal(HealthStatus.Unhealthy, result.Status); + Assert.Equal(HealthStatus.Degraded, result.Status); } } [Fact] - public async Task CheckAsync_CustomTest_Unhealthy() + public async Task CheckAsync_CustomTestWithUnhealthyFailureStatusSpecified_Unhealthy() { // Arrange 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().CreateScope()) + { + var registration = Assert.Single(services.GetRequiredService>().Value.Registrations); + var check = ActivatorUtilities.CreateInstance>(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 private static int _testDbCounter; private static IServiceProvider CreateServices( Func> testQuery = null, - HealthStatus failureStatus = HealthStatus.Unhealthy) + HealthStatus? failureStatus = HealthStatus.Unhealthy) { var serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext(o => o.UseInMemoryDatabase("Test" + Interlocked.Increment(ref _testDbCounter)));