Skip to content

Commit d5079b3

Browse files
add testcase
1 parent 004c48b commit d5079b3

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

tests/Tests.FeatureManagement/FeatureManagement.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,5 +1262,72 @@ public async Task VariantsInvalidScenarios()
12621262
Assert.Equal(FeatureManagementError.InvalidConfigurationSetting, e.Error);
12631263
Assert.Contains(ConfigurationFields.PercentileAllocationFrom, e.Message);
12641264
}
1265+
1266+
[Fact]
1267+
public void VariantBasedInjection()
1268+
{
1269+
IConfiguration configuration = new ConfigurationBuilder()
1270+
.AddJsonFile("appsettings.json")
1271+
.Build();
1272+
1273+
IServiceCollection services = new ServiceCollection();
1274+
1275+
services.AddSingleton<IAlgorithm, AlgorithmBeta>();
1276+
services.AddSingleton<IAlgorithm, AlgorithmSigma>();
1277+
services.AddSingleton<IAlgorithm>(sp => new AlgorithmOmega("OMEGA"));
1278+
1279+
services.AddSingleton(configuration)
1280+
.AddFeatureManagement()
1281+
.AddFeatureFilter<TargetingFilter>()
1282+
.AddVariantServiceProvider<IAlgorithm>(Features.VariantImplementationFeature);
1283+
1284+
var targetingContextAccessor = new OnDemandTargetingContextAccessor();
1285+
1286+
services.AddSingleton<ITargetingContextAccessor>(targetingContextAccessor);
1287+
1288+
ServiceProvider serviceProvider = services.BuildServiceProvider();
1289+
1290+
IVariantFeatureManager featureManager = serviceProvider.GetRequiredService<IVariantFeatureManager>();
1291+
1292+
IVariantServiceProvider<IAlgorithm> featuredAlgorithm = serviceProvider.GetRequiredService<IVariantServiceProvider<IAlgorithm>>();
1293+
1294+
targetingContextAccessor.Current = new TargetingContext
1295+
{
1296+
UserId = "Guest"
1297+
};
1298+
1299+
IAlgorithm algorithm = featuredAlgorithm.GetService();
1300+
1301+
Assert.Null(algorithm);
1302+
1303+
targetingContextAccessor.Current = new TargetingContext
1304+
{
1305+
UserId = "UserSigma"
1306+
};
1307+
1308+
algorithm = featuredAlgorithm.GetService();
1309+
1310+
Assert.Null(algorithm);
1311+
1312+
targetingContextAccessor.Current = new TargetingContext
1313+
{
1314+
UserId = "UserBeta"
1315+
};
1316+
1317+
algorithm = featuredAlgorithm.GetService();
1318+
1319+
Assert.NotNull(algorithm);
1320+
Assert.Equal("Beta", algorithm.Name);
1321+
1322+
targetingContextAccessor.Current = new TargetingContext
1323+
{
1324+
UserId = "UserOmega"
1325+
};
1326+
1327+
algorithm = featuredAlgorithm.GetService();
1328+
1329+
Assert.NotNull(algorithm);
1330+
Assert.Equal("OMEGA", algorithm.Name);
1331+
}
12651332
}
12661333
}

tests/Tests.FeatureManagement/Features.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ static class Features
3030
public const string VariantFeatureBothConfigurations = "VariantFeatureBothConfigurations";
3131
public const string VariantFeatureInvalidStatusOverride = "VariantFeatureInvalidStatusOverride";
3232
public const string VariantFeatureInvalidFromTo = "VariantFeatureInvalidFromTo";
33+
public const string VariantImplementationFeature = "VariantImplementationFeature";
3334
}
3435
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.FeatureManagement;
2+
3+
namespace Tests.FeatureManagement
4+
{
5+
interface IAlgorithm
6+
{
7+
public string Name { get; }
8+
}
9+
10+
class AlgorithmBeta : IAlgorithm
11+
{
12+
public string Name { get; set; }
13+
14+
public AlgorithmBeta()
15+
{
16+
Name = "Beta";
17+
}
18+
}
19+
20+
class AlgorithmSigma : IAlgorithm
21+
{
22+
public string Name { get; set; }
23+
24+
public AlgorithmSigma()
25+
{
26+
Name = "Sigma";
27+
}
28+
}
29+
30+
[VariantServiceAlias("Omega")]
31+
class AlgorithmOmega : IAlgorithm
32+
{
33+
public string Name { get; set; }
34+
35+
public AlgorithmOmega(string name)
36+
{
37+
Name = name;
38+
}
39+
}
40+
}

tests/Tests.FeatureManagement/appsettings.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,54 @@
493493
"Name": "On"
494494
}
495495
]
496+
},
497+
"VariantImplementationFeature": {
498+
"EnabledFor": [
499+
{
500+
"Name": "Targeting",
501+
"Parameters": {
502+
"Audience": {
503+
"Users": [
504+
"UserOmega", "UserSigma", "UserBeta"
505+
]
506+
}
507+
}
508+
}
509+
],
510+
"Variants": [
511+
{
512+
"Name": "AlgorithmBeta"
513+
},
514+
{
515+
"Name": "Sigma",
516+
"ConfigurationValue": "AlgorithmSigma"
517+
},
518+
{
519+
"Name": "Omega"
520+
}
521+
],
522+
"Allocation": {
523+
"User": [
524+
{
525+
"Variant": "AlgorithmBeta",
526+
"Users": [
527+
"UserBeta"
528+
]
529+
},
530+
{
531+
"Variant": "Omega",
532+
"Users": [
533+
"UserOmega"
534+
]
535+
},
536+
{
537+
"Variant": "Sigma",
538+
"Users": [
539+
"UserSigma"
540+
]
541+
}
542+
]
543+
}
496544
}
497545
}
498546
}

0 commit comments

Comments
 (0)