Skip to content

Commit 791bd21

Browse files
update
1 parent ff455b1 commit 791bd21

5 files changed

Lines changed: 34 additions & 25 deletions

File tree

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
1010
{
11-
/// <summary>
12-
/// Health check for Azure App Configuration.
13-
/// </summary>
14-
public sealed class AzureAppConfigurationHealthCheck : IHealthCheck
11+
internal class AzureAppConfigurationHealthCheck : IConfigurationHealthCheck
1512
{
1613
private AzureAppConfigurationProvider _provider = null;
1714

@@ -20,9 +17,6 @@ internal void SetProvider(AzureAppConfigurationProvider provider)
2017
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
2118
}
2219

23-
/// <summary>
24-
/// Checks the health of the Azure App Configuration provider.
25-
/// </summary>
2620
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
2721
{
2822
if (_provider == null)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ public class AzureAppConfigurationOptions
3333
private List<Func<ConfigurationSetting, ValueTask<ConfigurationSetting>>> _mappers = new List<Func<ConfigurationSetting, ValueTask<ConfigurationSetting>>>();
3434
private List<KeyValueSelector> _selectors;
3535
private IConfigurationRefresher _refresher = new AzureAppConfigurationRefresher();
36+
private IConfigurationHealthCheck _healthCheck = new AzureAppConfigurationHealthCheck();
3637
private bool _selectCalled = false;
3738

3839
// The following set is sorted in descending order.
3940
// Since multiple prefixes could start with the same characters, we need to trim the longest prefix first.
4041
private SortedSet<string> _keyPrefixes = new SortedSet<string>(Comparer<string>.Create((k1, k2) => -string.Compare(k1, k2, StringComparison.OrdinalIgnoreCase)));
4142

42-
/// <summary>
43-
/// Health check for Azure App Configuration.
44-
/// </summary>
45-
public AzureAppConfigurationHealthCheck HealthCheck { get; set; } = new AzureAppConfigurationHealthCheck();
46-
4743
/// <summary>
4844
/// Flag to indicate whether replica discovery is enabled.
4945
/// </summary>
@@ -464,6 +460,15 @@ public IConfigurationRefresher GetRefresher()
464460
return _refresher;
465461
}
466462

463+
/// <summary>
464+
/// Get an instance of <see cref="IConfigurationHealthCheck"/> that can be used to do health checks for the configuration provider.
465+
/// </summary>
466+
/// <returns>An instance of <see cref="IConfigurationHealthCheck"/>.</returns>
467+
public IConfigurationHealthCheck GetHealthCheck()
468+
{
469+
return _healthCheck;
470+
}
471+
467472
/// <summary>
468473
/// Configures the Azure App Configuration provider to use the provided Key Vault configuration to resolve key vault references.
469474
/// </summary>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ public AzureAppConfigurationProvider(IConfigurationClientManager configClientMan
119119
bool hasWatchers = watchers.Any();
120120
TimeSpan minWatcherRefreshInterval = hasWatchers ? watchers.Min(w => w.RefreshInterval) : TimeSpan.MaxValue;
121121

122-
if (options.HealthCheck != null)
123-
{
124-
options.HealthCheck.SetProvider(this);
125-
}
122+
var healthCheck = (AzureAppConfigurationHealthCheck)_options.GetHealthCheck();
123+
healthCheck.SetProvider(this);
126124

127125
if (options.RegisterAllEnabled)
128126
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
6+
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
7+
{
8+
/// <summary>
9+
/// An interface for Azure App Configuration health check.
10+
/// </summary>
11+
public interface IConfigurationHealthCheck : IHealthCheck
12+
{
13+
}
14+
}

tests/Tests.AzureAppConfiguration/HealthCheckTest.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ public class HealthCheckTest
3636
};
3737

3838
[Fact]
39-
public async Task HealthCheckTests_ReturnsUnhealthyWhenInitialLoadIsNotCompleted()
39+
public async Task HealthCheckTests_ReturnsHealthyWhenInitialLoadIsCompleted()
4040
{
41-
var healthCheck = new AzureAppConfigurationHealthCheck();
42-
HealthCheckResult result = await healthCheck.CheckHealthAsync(new HealthCheckContext());
43-
Assert.Equal(HealthStatus.Unhealthy, result.Status);
41+
IHealthCheck healthCheck = null;
4442

4543
var mockResponse = new Mock<Response>();
4644
var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict);
@@ -52,20 +50,20 @@ public async Task HealthCheckTests_ReturnsUnhealthyWhenInitialLoadIsNotCompleted
5250
.AddAzureAppConfiguration(options =>
5351
{
5452
options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object);
55-
options.HealthCheck = healthCheck;
53+
healthCheck = options.GetHealthCheck();
5654
})
5755
.Build();
5856

5957
Assert.True(config["TestKey1"] == "TestValue1");
60-
result = await healthCheck.CheckHealthAsync(new HealthCheckContext());
58+
var result = await healthCheck.CheckHealthAsync(new HealthCheckContext());
6159
Assert.Equal(HealthStatus.Healthy, result.Status);
6260
}
6361

6462
[Fact]
6563
public async Task HealthCheckTests_ReturnsUnhealthyWhenRefreshFailed()
6664
{
6765
IConfigurationRefresher refresher = null;
68-
var healthCheck = new AzureAppConfigurationHealthCheck();
66+
IHealthCheck healthCheck = null;
6967
var mockResponse = new Mock<Response>();
7068
var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict);
7169

@@ -80,18 +78,18 @@ public async Task HealthCheckTests_ReturnsUnhealthyWhenRefreshFailed()
8078
{
8179
options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object);
8280
options.MinBackoffDuration = TimeSpan.FromSeconds(2);
83-
options.HealthCheck = healthCheck;
8481
options.ConfigurationSettingPageIterator = new MockConfigurationSettingPageIterator();
8582
options.ConfigureRefresh(refreshOptions =>
8683
{
8784
refreshOptions.RegisterAll()
8885
.SetRefreshInterval(TimeSpan.FromSeconds(1));
8986
});
9087
refresher = options.GetRefresher();
88+
healthCheck = options.GetHealthCheck();
9189
})
9290
.Build();
9391

94-
HealthCheckResult result = await healthCheck.CheckHealthAsync(new HealthCheckContext());
92+
var result = await healthCheck.CheckHealthAsync(new HealthCheckContext());
9593
Assert.Equal(HealthStatus.Healthy, result.Status);
9694

9795
// Wait for the refresh interval to expire

0 commit comments

Comments
 (0)