1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
@@ -61,7 +61,7 @@ public async Task CheckAsync_CustomTest_Healthy()
61
61
}
62
62
63
63
[ Fact ]
64
- public async Task CheckAsync_CustomTest_Degraded ( )
64
+ public async Task CheckAsync_CustomTestWithDegradedFailureStatusSpecified_Degraded ( )
65
65
{
66
66
// Arrange
67
67
var services = CreateServices ( async ( c , ct ) =>
@@ -78,12 +78,12 @@ public async Task CheckAsync_CustomTest_Degraded()
78
78
var result = await check . CheckHealthAsync ( new HealthCheckContext ( ) { Registration = registration , } ) ;
79
79
80
80
// Assert
81
- Assert . Equal ( HealthStatus . Unhealthy , result . Status ) ;
81
+ Assert . Equal ( HealthStatus . Degraded , result . Status ) ;
82
82
}
83
83
}
84
84
85
85
[ Fact ]
86
- public async Task CheckAsync_CustomTest_Unhealthy ( )
86
+ public async Task CheckAsync_CustomTestWithUnhealthyFailureStatusSpecified_Unhealthy ( )
87
87
{
88
88
// Arrange
89
89
var services = CreateServices ( async ( c , ct ) =>
@@ -104,12 +104,34 @@ public async Task CheckAsync_CustomTest_Unhealthy()
104
104
}
105
105
}
106
106
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
+
107
129
// used to ensure each test uses a unique in-memory database
108
130
private static int _testDbCounter ;
109
131
110
132
private static IServiceProvider CreateServices (
111
133
Func < TestDbContext , CancellationToken , Task < bool > > testQuery = null ,
112
- HealthStatus failureStatus = HealthStatus . Unhealthy )
134
+ HealthStatus ? failureStatus = HealthStatus . Unhealthy )
113
135
{
114
136
var serviceCollection = new ServiceCollection ( ) ;
115
137
serviceCollection . AddDbContext < TestDbContext > ( o => o . UseInMemoryDatabase ( "Test" + Interlocked . Increment ( ref _testDbCounter ) ) ) ;
0 commit comments