Skip to content

Commit 8c5e506

Browse files
committed
add test to ensure no other rules are bucketed when user fails first traffic allocation rule in fallback bucketing strategy
1 parent 0b10a33 commit 8c5e506

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

core-api/src/test/java/com/optimizely/ab/bucketing/DecisionServiceTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.optimizely.ab.config.Experiment;
2121
import com.optimizely.ab.config.FeatureFlag;
2222
import com.optimizely.ab.config.ProjectConfig;
23+
import com.optimizely.ab.config.ProjectConfigTestUtils;
2324
import com.optimizely.ab.config.Rollout;
2425
import com.optimizely.ab.config.TrafficAllocation;
2526
import com.optimizely.ab.config.ValidProjectConfigV4;
@@ -44,6 +45,8 @@
4445
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV3;
4546
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV4;
4647
import static com.optimizely.ab.config.ValidProjectConfigV4.ATTRIBUTE_HOUSE_KEY;
48+
import static com.optimizely.ab.config.ValidProjectConfigV4.ATTRIBUTE_NATIONALITY_KEY;
49+
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE;
4750
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE;
4851
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_MULTI_VARIATE_FEATURE;
4952
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_SINGLE_VARIABLE_STRING;
@@ -562,7 +565,53 @@ public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficI
562565
verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
563566
}
564567

565-
//========= white list tests ==========/
568+
/**
569+
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
570+
* returns the variation of "Everyone Else" rule
571+
* when the user passes targeting for a rule, but was failed the traffic allocation for that rule,
572+
* and is bucketed successfully into the "Everyone Else" rule.
573+
* Fallback bucketing should not evaluate any other audiences.
574+
* Even though the user would satisfy a later rollout rule, they are never evaluated for it or bucketed into it.
575+
*/
576+
@Test
577+
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse() {
578+
Bucketer mockBucketer = mock(Bucketer.class);
579+
Rollout rollout = ROLLOUT_2;
580+
Experiment englishCitizensRule = rollout.getExperiments().get(2);
581+
Variation englishCitizenVariation = englishCitizensRule.getVariations().get(0);
582+
Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
583+
Variation expectedVariation = everyoneElseRule.getVariations().get(0);
584+
when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
585+
when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
586+
when(mockBucketer.bucket(eq(englishCitizensRule), anyString())).thenReturn(englishCitizenVariation);
587+
588+
DecisionService decisionService = new DecisionService(
589+
mockBucketer,
590+
mockErrorHandler,
591+
v4ProjectConfig,
592+
null
593+
);
594+
595+
assertEquals(expectedVariation,
596+
decisionService.getVariationForFeatureInRollout(
597+
FEATURE_FLAG_MULTI_VARIATE_FEATURE,
598+
genericUserId,
599+
ProjectConfigTestUtils.createMapOfObjects(
600+
ProjectConfigTestUtils.createListOfObjects(
601+
ATTRIBUTE_HOUSE_KEY, ATTRIBUTE_NATIONALITY_KEY
602+
),
603+
ProjectConfigTestUtils.createListOfObjects(
604+
AUDIENCE_GRYFFINDOR_VALUE, AUDIENCE_ENGLISH_CITIZENS_VALUE
605+
)
606+
)
607+
)
608+
);
609+
610+
// verify user is only bucketed once for everyone else rule
611+
verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
612+
}
613+
614+
//========= white list tests ==========/
566615

567616
/**
568617
* Test {@link DecisionService#getWhitelistedVariation(Experiment, String)} correctly returns a whitelisted variation.

0 commit comments

Comments
 (0)