Skip to content

Dedupe when returning all feature names #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,17 @@ public IAsyncEnumerable<string> GetFeatureNamesAsync()
/// <returns>An enumerator which provides asynchronous iteration over the feature names registered in the feature manager.</returns>
public async IAsyncEnumerable<string> GetFeatureNamesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var featureNamesReturned = new HashSet<string>();
await foreach (FeatureDefinition featureDefinition in _featureDefinitionProvider.GetAllFeatureDefinitionsAsync().ConfigureAwait(false))
{
cancellationToken.ThrowIfCancellationRequested();

yield return featureDefinition.Name;
if (!featureNamesReturned.Contains(featureDefinition.Name))
{
yield return featureDefinition.Name;

featureNamesReturned.Add(featureDefinition.Name);
}
}
}

Expand Down
73 changes: 73 additions & 0 deletions tests/Tests.FeatureManagement/FeatureManagementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,79 @@ public async Task LastFeatureFlagWins()

Assert.True(await featureManager.IsEnabledAsync(Features.DuplicateFlag));
}

[Fact]
public async Task DedupesFeatureNames()
{
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ $"FeatureManagement:{Features.DuplicateFlag}", "False" }
})
.AddInMemoryCollection(new Dictionary<string, string>
{
{ $"FeatureManagement:{Features.DuplicateFlag}", "True" }
})
.Build();

IServiceCollection services = new ServiceCollection();

services
.AddSingleton(configuration)
.AddFeatureManagement();

ServiceProvider serviceProvider = services.BuildServiceProvider();

IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();

var featureNames = await featureManager.GetFeatureNamesAsync().ToListAsync();
Assert.Equal(featureNames.Distinct().Count(), featureNames.Count);
Assert.True(await featureManager.IsEnabledAsync(Features.DuplicateFlag));
}
}

public class FeatureManagementMultipleSchemasTest
{
[Fact]
public async Task DedupesFeatureNames()
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.MultipleSchemas.json")
.Build();

IServiceCollection services = new ServiceCollection();

services
.AddSingleton(configuration)
.AddFeatureManagement();

ServiceProvider serviceProvider = services.BuildServiceProvider();

IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();

var featureNames = await featureManager.GetFeatureNamesAsync().ToListAsync();
Assert.Equal(featureNames.Distinct().Count(), featureNames.Count);
}

[Fact]
public async Task PrioritizesMicrosoftSchemaConfiguration()
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.MultipleSchemas.json")
.Build();

IServiceCollection services = new ServiceCollection();

services
.AddSingleton(configuration)
.AddFeatureManagement();

ServiceProvider serviceProvider = services.BuildServiceProvider();

IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();

Assert.True(await featureManager.IsEnabledAsync(Features.DuplicateFlag));
}
}

public class FeatureManagementFeatureFilterGeneralTest
Expand Down
5 changes: 4 additions & 1 deletion tests/Tests.FeatureManagement/Tests.FeatureManagement.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net8.0;net9.0</TargetFrameworks>
Expand Down Expand Up @@ -43,6 +43,9 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.MultipleSchemas.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="DotnetFeatureManagementSchema.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
22 changes: 22 additions & 0 deletions tests/Tests.FeatureManagement/appsettings.MultipleSchemas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",

// MS schema will be prioritized, feature should be enabled
"feature_management": {
"feature_flags": [
{
"id": "DuplicateFlag",
"enabled": true
}
]
},

"FeatureManagement": {
"DuplicateFlag": false
}
}