Skip to content

Commit c7533d6

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 42d9387 commit c7533d6

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;
@@ -43,6 +44,8 @@
4344
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV3;
4445
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV4;
4546
import static com.optimizely.ab.config.ValidProjectConfigV4.ATTRIBUTE_HOUSE_KEY;
47+
import static com.optimizely.ab.config.ValidProjectConfigV4.ATTRIBUTE_NATIONALITY_KEY;
48+
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE;
4649
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE;
4750
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_MULTI_VARIATE_FEATURE;
4851
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_SINGLE_VARIABLE_STRING;
@@ -425,7 +428,53 @@ public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficI
425428
verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
426429
}
427430

428-
//========= white list tests ==========/
431+
/**
432+
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
433+
* returns the variation of "Everyone Else" rule
434+
* when the user passes targeting for a rule, but was failed the traffic allocation for that rule,
435+
* and is bucketed successfully into the "Everyone Else" rule.
436+
* Fallback bucketing should not evaluate any other audiences.
437+
* Even though the user would satisfy a later rollout rule, they are never evaluated for it or bucketed into it.
438+
*/
439+
@Test
440+
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse() {
441+
Bucketer mockBucketer = mock(Bucketer.class);
442+
Rollout rollout = ROLLOUT_2;
443+
Experiment englishCitizensRule = rollout.getExperiments().get(2);
444+
Variation englishCitizenVariation = englishCitizensRule.getVariations().get(0);
445+
Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
446+
Variation expectedVariation = everyoneElseRule.getVariations().get(0);
447+
when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
448+
when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
449+
when(mockBucketer.bucket(eq(englishCitizensRule), anyString())).thenReturn(englishCitizenVariation);
450+
451+
DecisionService decisionService = new DecisionService(
452+
mockBucketer,
453+
mockErrorHandler,
454+
v4ProjectConfig,
455+
null
456+
);
457+
458+
assertEquals(expectedVariation,
459+
decisionService.getVariationForFeatureInRollout(
460+
FEATURE_FLAG_MULTI_VARIATE_FEATURE,
461+
genericUserId,
462+
ProjectConfigTestUtils.createMapOfObjects(
463+
ProjectConfigTestUtils.createListOfObjects(
464+
ATTRIBUTE_HOUSE_KEY, ATTRIBUTE_NATIONALITY_KEY
465+
),
466+
ProjectConfigTestUtils.createListOfObjects(
467+
AUDIENCE_GRYFFINDOR_VALUE, AUDIENCE_ENGLISH_CITIZENS_VALUE
468+
)
469+
)
470+
)
471+
);
472+
473+
// verify user is only bucketed once for everyone else rule
474+
verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
475+
}
476+
477+
//========= white list tests ==========/
429478

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

0 commit comments

Comments
 (0)