Skip to content

Commit 5a3c2d7

Browse files
Merge pull request #720 from Azure/zhiyuanliang/merge-main-to-preview
Merge main to preview
2 parents 80a9f30 + e210b04 commit 5a3c2d7

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationHealthCheck.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,27 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
3737
return HealthCheckResult.Unhealthy(HealthCheckConstants.NoProviderFoundMessage);
3838
}
3939

40+
HealthCheckResult worstResult = HealthCheckResult.Healthy();
41+
4042
foreach (IHealthCheck healthCheck in _healthChecks)
4143
{
4244
var result = await healthCheck.CheckHealthAsync(context, cancellationToken).ConfigureAwait(false);
4345

46+
// Keep track of the worst health status found
47+
// HealthStatus enum is ordered: Unhealthy(0) < Degraded(1) < Healthy(2)
48+
if (result.Status < worstResult.Status)
49+
{
50+
worstResult = result;
51+
}
52+
53+
// If an Unhealthy status is found, short-circuit since that's the worst possible
4454
if (result.Status == HealthStatus.Unhealthy)
4555
{
4656
return result;
4757
}
4858
}
4959

50-
return HealthCheckResult.Healthy();
60+
return worstResult;
5161
}
5262

5363
private void FindHealthChecks(IConfigurationRoot configurationRoot, List<IHealthCheck> healthChecks)

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,15 +576,17 @@ public void ProcessPushNotification(PushNotification pushNotification, TimeSpan?
576576

577577
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
578578
{
579+
HealthStatus failureStatus = context.Registration?.FailureStatus ?? HealthStatus.Unhealthy;
580+
579581
if (!_lastSuccessfulAttempt.HasValue)
580582
{
581-
return HealthCheckResult.Unhealthy(HealthCheckConstants.LoadNotCompletedMessage);
583+
return new HealthCheckResult(status: failureStatus, description: HealthCheckConstants.LoadNotCompletedMessage);
582584
}
583585

584586
if (_lastFailedAttempt.HasValue &&
585587
_lastSuccessfulAttempt.Value < _lastFailedAttempt.Value)
586588
{
587-
return HealthCheckResult.Unhealthy(HealthCheckConstants.RefreshFailedMessage);
589+
return new HealthCheckResult(status: failureStatus, description: HealthCheckConstants.RefreshFailedMessage);
588590
}
589591

590592
return HealthCheckResult.Healthy();

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationRefresherProvider.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ private void FindRefreshers(IConfigurationRoot configurationRoot, ILoggerFactory
6363
{
6464
foreach (IConfigurationProvider provider in configurationRoot.Providers)
6565
{
66-
if (provider is AzureAppConfigurationProvider appConfigurationProvider)
66+
if (provider is IConfigurationRefresher configurationRefresher)
6767
{
68-
appConfigurationProvider.LoggerFactory = loggerFactory;
69-
refreshers.Add(appConfigurationProvider);
68+
refreshers.Add(configurationRefresher);
69+
70+
if (configurationRefresher is AzureAppConfigurationProvider azureAppConfigurationProvider)
71+
{
72+
azureAppConfigurationProvider.LoggerFactory = loggerFactory;
73+
}
7074
}
7175
else if (provider is ChainedConfigurationProvider chainedProvider)
7276
{

tests/Tests.AzureAppConfiguration/Unit/HealthCheckTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,54 @@ public async Task HealthCheckTests_RegisterAzureAppConfigurationHealthCheck()
136136
Assert.Contains(HealthCheckConstants.HealthCheckRegistrationName, result.Entries.Keys);
137137
Assert.Equal(HealthStatus.Healthy, result.Entries[HealthCheckConstants.HealthCheckRegistrationName].Status);
138138
}
139+
140+
[Fact]
141+
public async Task HealthCheckTests_ShouldRespectHealthCheckRegistration()
142+
{
143+
IConfigurationRefresher refresher = null;
144+
var mockResponse = new Mock<Response>();
145+
var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict);
146+
147+
mockClient.SetupSequence(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>()))
148+
.Returns(new MockAsyncPageable(kvCollection))
149+
.Throws(new RequestFailedException(503, "Request failed."));
150+
151+
var config = new ConfigurationBuilder()
152+
.AddAzureAppConfiguration(options =>
153+
{
154+
options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object);
155+
options.MinBackoffDuration = TimeSpan.FromSeconds(2);
156+
options.ConfigurationSettingPageIterator = new MockConfigurationSettingPageIterator();
157+
options.ConfigureRefresh(refreshOptions =>
158+
{
159+
refreshOptions.RegisterAll()
160+
.SetRefreshInterval(TimeSpan.FromSeconds(1));
161+
});
162+
refresher = options.GetRefresher();
163+
})
164+
.Build();
165+
166+
var services = new ServiceCollection();
167+
services.AddSingleton<IConfiguration>(config);
168+
services.AddLogging(); // add logging for health check service
169+
services.AddHealthChecks()
170+
.AddAzureAppConfiguration(
171+
name: "TestName",
172+
failureStatus: HealthStatus.Degraded);
173+
var provider = services.BuildServiceProvider();
174+
var healthCheckService = provider.GetRequiredService<HealthCheckService>();
175+
176+
var result = await healthCheckService.CheckHealthAsync();
177+
Assert.Equal(HealthStatus.Healthy, result.Status);
178+
179+
// Wait for the refresh interval to expire
180+
Thread.Sleep(2000);
181+
182+
await refresher.TryRefreshAsync();
183+
result = await healthCheckService.CheckHealthAsync();
184+
Assert.Equal(HealthStatus.Degraded, result.Status);
185+
Assert.Contains("TestName", result.Entries.Keys);
186+
Assert.Equal(HealthStatus.Degraded, result.Entries["TestName"].Status);
187+
}
139188
}
140189
}

0 commit comments

Comments
 (0)