@@ -109,4 +109,68 @@ public void add_health_check_with_connection_multiplexer_when_properly_configure
109109 // the factory is called when it's used for the first time, as it can throw
110110 factoryCalled . ShouldBeFalse ( ) ;
111111 }
112- }
112+
113+ [ Fact ]
114+ public async Task add_health_check_with_async_connection_multiplexer_factory_when_properly_configured ( )
115+ {
116+ var connectionMultiplexer = Substitute . For < IConnectionMultiplexer > ( ) ;
117+ connectionMultiplexer . GetEndPoints ( configuredOnly : true ) . Returns ( [ ] ) ;
118+
119+ var services = new ServiceCollection ( ) ;
120+ var factoryCalled = false ;
121+ CancellationToken capturedCancellationToken = default ;
122+
123+ services . AddHealthChecks ( )
124+ . AddRedis ( async ( _ , cancellationToken ) =>
125+ {
126+ factoryCalled = true ;
127+ capturedCancellationToken = cancellationToken ;
128+ await Task . Yield ( ) ;
129+ return connectionMultiplexer ;
130+ } ) ;
131+
132+ using var serviceProvider = services . BuildServiceProvider ( ) ;
133+ var options = serviceProvider . GetRequiredService < IOptions < HealthCheckServiceOptions > > ( ) ;
134+
135+ var registration = options . Value . Registrations . First ( ) ;
136+ var check = registration . Factory ( serviceProvider ) ;
137+
138+ registration . Name . ShouldBe ( "redis" ) ;
139+ check . ShouldBeOfType < RedisHealthCheck > ( ) ;
140+ factoryCalled . ShouldBeFalse ( ) ;
141+
142+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
143+ var result = await check . CheckHealthAsync (
144+ new HealthCheckContext { Registration = registration } ,
145+ cancellationTokenSource . Token ) ;
146+
147+ result . Status . ShouldBe ( HealthStatus . Healthy ) ;
148+ factoryCalled . ShouldBeTrue ( ) ;
149+ capturedCancellationToken . ShouldBe ( cancellationTokenSource . Token ) ;
150+ }
151+
152+ [ Fact ]
153+ public async Task return_timeout_when_async_connection_multiplexer_factory_is_cancelled ( )
154+ {
155+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
156+ cancellationTokenSource . Cancel ( ) ;
157+
158+ var services = new ServiceCollection ( ) ;
159+ services . AddHealthChecks ( )
160+ . AddRedis ( ( _ , cancellationToken ) =>
161+ Task . FromCanceled < IConnectionMultiplexer > ( cancellationToken ) ) ;
162+
163+ using var serviceProvider = services . BuildServiceProvider ( ) ;
164+ var registration = serviceProvider
165+ . GetRequiredService < IOptions < HealthCheckServiceOptions > > ( )
166+ . Value . Registrations . First ( ) ;
167+ var check = registration . Factory ( serviceProvider ) ;
168+
169+ var result = await check . CheckHealthAsync (
170+ new HealthCheckContext { Registration = registration } ,
171+ cancellationTokenSource . Token ) ;
172+
173+ result . Status . ShouldBe ( HealthStatus . Unhealthy ) ;
174+ result . Description . ShouldBe ( "Healthcheck timed out" ) ;
175+ }
176+ }
0 commit comments