Skip to content

Commit 0b4477d

Browse files
fix: When rollout has no rule, should return null (#235)
1 parent 824961c commit 0b4477d

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

OptimizelySDK.Tests/DecisionServiceTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,24 @@ public void TestGetVariationForFeatureExperimentGivenMutexGroupAndUserNotBuckete
569569
#region GetVariationForFeatureRollout Tests
570570

571571
// Should return null when rollout doesn't exist for the feature.
572+
[Test]
573+
public void TestGetVariationForFeatureRolloutWhenNoRuleInRollouts()
574+
{
575+
var projectConfig = DatafileProjectConfig.Create(TestData.EmptyRolloutDatafile, new NoOpLogger(), new NoOpErrorHandler());
576+
577+
Assert.IsNotNull(projectConfig);
578+
var featureFlag = projectConfig.FeatureKeyMap["empty_rollout"];
579+
var rollout = projectConfig.GetRolloutFromId(featureFlag.RolloutId);
580+
581+
Assert.AreEqual(rollout.Experiments.Count, 0);
582+
583+
var decisionService = new DecisionService(new Bucketer(new NoOpLogger()), new NoOpErrorHandler(), null, new NoOpLogger());
584+
585+
var variation = decisionService.GetVariationForFeatureRollout(featureFlag, "userId1", null, projectConfig);
586+
587+
Assert.IsNull(variation);
588+
}
589+
572590
[Test]
573591
public void TestGetVariationForFeatureRolloutWhenRolloutIsNotInDataFile()
574592
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"version": "4",
3+
"rollouts": [
4+
{
5+
"experiments": [
6+
],
7+
"id": "18309384009"
8+
}
9+
],
10+
"typedAudiences": [],
11+
"anonymizeIP": true,
12+
"projectId": "18326250003",
13+
"variables": [],
14+
"featureFlags": [
15+
{
16+
"experimentIds": [],
17+
"rolloutId": "18309384009",
18+
"variables": [
19+
{
20+
"defaultValue": "",
21+
"type": "string",
22+
"id": "18323951833",
23+
"key": "var_1"
24+
}
25+
],
26+
"id": "18244658520",
27+
"key": "empty_rollout"
28+
}
29+
],
30+
"experiments": [],
31+
"audiences": [
32+
{
33+
"conditions": "[\"or\", {\"match\": \"exact\", \"name\": \"$opt_dummy_attribute\", \"type\": \"custom_attribute\", \"value\": \"$opt_dummy_value\"}]",
34+
"id": "$opt_dummy_audience",
35+
"name": "Optimizely-Generated Audience for Backwards Compatibility"
36+
}
37+
],
38+
"groups": [],
39+
"attributes": [],
40+
"botFiltering": false,
41+
"accountId": "8272261422",
42+
"events": [],
43+
"revision": "2"
44+
}

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<ItemGroup>
120120
<EmbeddedResource Include="TestData.json" />
121121
<EmbeddedResource Include="simple_ab_experiments.json" />
122+
<EmbeddedResource Include="EmptyRolloutRule.json" />
122123
</ItemGroup>
123124
<ItemGroup>
124125
<ProjectReference Include="..\OptimizelySDK\OptimizelySDK.csproj">

OptimizelySDK.Tests/Utils/TestData.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class TestData
2727
private static string simpleABExperimentsDatafile = null;
2828
private static string unsupportedVersionDatafile = null;
2929
private static string typedAudienceDatafile = null;
30+
private static string emptyRolloutDatafile = null;
3031

3132
public static string Datafile
3233
{
@@ -52,6 +53,12 @@ public static string UnsupportedVersionDatafile
5253
}
5354
}
5455

56+
public static string EmptyRolloutDatafile {
57+
get {
58+
return emptyRolloutDatafile ?? (emptyRolloutDatafile = LoadJsonData("EmptyRolloutRule.json"));
59+
}
60+
}
61+
5562
public static string TypedAudienceDatafile
5663
{
5764
get

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ public virtual FeatureDecision GetVariationForFeatureRollout(FeatureFlag feature
389389
return null;
390390
}
391391

392+
if (rollout.Experiments == null || rollout.Experiments.Count == 0) {
393+
return null;
394+
}
395+
392396
Variation variation = null;
393397
var rolloutRulesLength = rollout.Experiments.Count;
394398

0 commit comments

Comments
 (0)