Skip to content
Closed
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
7 changes: 6 additions & 1 deletion src/Microsoft.FeatureManagement/FeatureManagementError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public enum FeatureManagementError
/// <summary>
/// A feature filter configured for the feature being evaluated is an ambiguous reference to multiple registered feature filters.
/// </summary>
AmbiguousFeatureFilter
AmbiguousFeatureFilter,

/// <summary>
/// The specified feature does not exist.
/// </summary>
MissingFeature
}
}
6 changes: 6 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureManagementOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public class FeatureManagementOptions
/// If missing feature filters are not ignored an exception will be thrown when attempting to evaluate a feature that depends on a missing feature filter.
/// </summary>
public bool IgnoreMissingFeatureFilters { get; set; }

/// <summary>
/// Controls the behavior of feature evaluation when the specified feature does not exist.
/// If missing features are not ignored, an exception will be thrown when attempting to evaluate them.
/// </summary>
public bool IgnoreMissingFeatures { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want this defaults to true.

}
}
4 changes: 4 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private async Task<bool> IsEnabledAsync<TContext>(string feature, TContext appCo
}
}
}
else if (!_options.IgnoreMissingFeatures)
{
throw new FeatureManagementException(FeatureManagementError.MissingFeature, $"The feature declaration for specified feature '{feature}' was not found.");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should log a warning even if IgnoreMissingFeatures is true. Suggest change below

else
{
    string errorMessage = $"The feature declaration for specified feature '{feature}' was not found.";
    if (!_options.IgnoreMissingFeatures)
    {
        throw new FeatureManagementException(FeatureManagementError.MissingFeature, errorMessage);
    }
    else
    {
        _logger.LogWarning(errorMessage);
    }
}


foreach (ISessionManager sessionManager in _sessionManagers)
{
Expand Down
62 changes: 62 additions & 0 deletions tests/Tests.FeatureManagement/FeatureManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I may miss it as it's not very easy to tell. What we are using in the reflection namespace?

using System.Threading.Tasks;
using Xunit;

Expand All @@ -25,6 +26,7 @@ public class FeatureManagement
private const string OffFeature = "OffFeature";
private const string ConditionalFeature = "ConditionalFeature";
private const string ContextualFeature = "ContextualFeature";
private const string MissingFeature = "MissingFeature";

[Fact]
public async Task ReadsConfiguration()
Expand All @@ -33,6 +35,12 @@ public async Task ReadsConfiguration()

var services = new ServiceCollection();

services
.Configure<FeatureManagementOptions>(options =>
{
options.IgnoreMissingFeatures = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

});

services
.AddSingleton(config)
.AddFeatureManagement()
Expand Down Expand Up @@ -483,5 +491,59 @@ public async Task SwallowsExceptionForMissingFeatureFilter()

Assert.False(isEnabled);
}

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

var services = new ServiceCollection();

services
.Configure<FeatureManagementOptions>(options =>
{
options.IgnoreMissingFeatures = false;
});

services
.AddSingleton(config)
.AddFeatureManagement()
.AddFeatureFilter<TestFilter>();

ServiceProvider serviceProvider = services.BuildServiceProvider();

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

FeatureManagementException e = await Assert.ThrowsAsync<FeatureManagementException>(async () => await featureManager.IsEnabledAsync(MissingFeature));

Assert.Equal(FeatureManagementError.MissingFeature, e.Error);
}

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

var services = new ServiceCollection();

services
.Configure<FeatureManagementOptions>(options =>
{
options.IgnoreMissingFeatures = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this so we can test the default behavior and ensure no regressions.

});

services
.AddSingleton(config)
.AddFeatureManagement()
.AddFeatureFilter<TestFilter>();

ServiceProvider serviceProvider = services.BuildServiceProvider();

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

var isEnabled = await featureManager.IsEnabledAsync(ConditionalFeature);

Assert.False(isEnabled);
}
}
}