diff --git a/optimizely/decision_service.py b/optimizely/decision_service.py index 28275ef..0580917 100644 --- a/optimizely/decision_service.py +++ b/optimizely/decision_service.py @@ -457,7 +457,17 @@ def get_variation( } # Check to see if user has a decision available for the given experiment - if user_profile_tracker is not None and not ignore_user_profile: + # Skip UPS lookup for CMAB experiments - CMAB decisions are dynamic and + # should not use sticky bucketing via UserProfileService + if experiment.cmab: + if user_profile_tracker is not None and not ignore_user_profile: + message = ( + f'Skipping user profile service lookup for CMAB experiment "{experiment.key}". ' + f'CMAB decisions should not use sticky bucketing.' + ) + self.logger.info(message) + decide_reasons.append(message) + elif user_profile_tracker is not None and not ignore_user_profile: variation = self.get_stored_variation(project_config, experiment, user_profile_tracker.get_user_profile()) if variation: message = f'Returning previously activated variation ID "{variation}" of experiment ' \ @@ -529,7 +539,9 @@ def get_variation( self.logger.info(message) decide_reasons.append(message) # Store this new decision and return the variation for the user - if user_profile_tracker is not None and not ignore_user_profile: + # Skip UPS save for CMAB experiments - CMAB decisions are dynamic + # and should not be persisted via UserProfileService + if user_profile_tracker is not None and not ignore_user_profile and not experiment.cmab: try: user_profile_tracker.update_user_profile(experiment, variation) except: diff --git a/tests/test_decision_service.py b/tests/test_decision_service.py index dbcb743..2669369 100644 --- a/tests/test_decision_service.py +++ b/tests/test_decision_service.py @@ -1074,6 +1074,211 @@ def test_get_variation_cmab_experiment_with_whitelisted_variation(self): mock_bucket.assert_not_called() mock_cmab_decision.assert_not_called() + def test_get_variation_cmab_experiment_skips_ups_lookup(self): + """Test that get_variation skips UPS lookup for CMAB experiments.""" + + # Create a user context + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a CMAB experiment + cmab_experiment = entities.Experiment( + '111150', + 'cmab_experiment', + 'Running', + '111150', + [], + {}, + [ + entities.Variation('111151', 'variation_1'), + entities.Variation('111152', 'variation_2') + ], + [ + {'entityId': '111151', 'endOfRange': 5000}, + {'entityId': '111152', 'endOfRange': 10000} + ], + cmab={'trafficAllocation': 5000} + ) + + # Set up a user profile tracker with a stored variation for this experiment + ups = user_profile.UserProfileService() + tracker = user_profile.UserProfileTracker("test_user", ups, self.decision_service.logger) + stored_profile = user_profile.UserProfile( + "test_user", + {"111150": {"variation_id": "111152"}} + ) + tracker.user_profile = stored_profile + + with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \ + mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \ + mock.patch.object(self.decision_service.bucketer, 'bucket_to_entity_id', + return_value=['$', []]), \ + mock.patch.object(self.decision_service, 'cmab_service') as mock_cmab_service, \ + mock.patch.object(self.project_config, 'get_variation_from_id', + return_value=entities.Variation('111151', 'variation_1')), \ + mock.patch.object(self.decision_service, 'get_stored_variation') as mock_stored_variation, \ + mock.patch.object(self.decision_service, 'logger') as mock_logger: + + # Configure CMAB service to return a decision + mock_cmab_service.get_decision.return_value = ( + { + 'variation_id': '111151', + 'cmab_uuid': 'test-cmab-uuid-456' + }, + [] + ) + + # Call get_variation with the CMAB experiment and a user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + cmab_experiment, + user, + tracker + ) + + # Verify UPS lookup was NOT called for CMAB experiment + mock_stored_variation.assert_not_called() + + # Verify the CMAB decision was used instead + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + self.assertEqual('test-cmab-uuid-456', variation_result['cmab_uuid']) + self.assertStrictFalse(variation_result['error']) + + # Verify the UPS exclusion reason is in the decision reasons + ups_skip_reasons = [r for r in variation_result['reasons'] + if 'Skipping user profile service lookup for CMAB experiment' in r] + self.assertEqual(len(ups_skip_reasons), 1) + + def test_get_variation_cmab_experiment_skips_ups_save(self): + """Test that get_variation skips UPS save for CMAB experiments.""" + + # Create a user context + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a CMAB experiment + cmab_experiment = entities.Experiment( + '111150', + 'cmab_experiment', + 'Running', + '111150', + [], + {}, + [ + entities.Variation('111151', 'variation_1'), + entities.Variation('111152', 'variation_2') + ], + [ + {'entityId': '111151', 'endOfRange': 5000}, + {'entityId': '111152', 'endOfRange': 10000} + ], + cmab={'trafficAllocation': 5000} + ) + + # Set up a user profile tracker + ups = user_profile.UserProfileService() + tracker = user_profile.UserProfileTracker("test_user", ups, self.decision_service.logger) + + with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \ + mock.patch('optimizely.helpers.audience.does_user_meet_audience_conditions', return_value=[True, []]), \ + mock.patch.object(self.decision_service.bucketer, 'bucket_to_entity_id', + return_value=['$', []]), \ + mock.patch.object(self.decision_service, 'cmab_service') as mock_cmab_service, \ + mock.patch.object(self.project_config, 'get_variation_from_id', + return_value=entities.Variation('111151', 'variation_1')), \ + mock.patch.object(tracker, 'update_user_profile') as mock_update_profile, \ + mock.patch.object(self.decision_service, 'logger'): + + # Configure CMAB service to return a decision + mock_cmab_service.get_decision.return_value = ( + { + 'variation_id': '111151', + 'cmab_uuid': 'test-cmab-uuid-789' + }, + [] + ) + + # Call get_variation with the CMAB experiment and a user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + cmab_experiment, + user, + tracker + ) + + # Verify the variation was assigned + self.assertEqual(entities.Variation('111151', 'variation_1'), variation_result['variation']) + + # Verify UPS save was NOT called for CMAB experiment + mock_update_profile.assert_not_called() + + def test_get_variation_non_cmab_experiment_still_uses_ups(self): + """Test that get_variation still uses UPS for non-CMAB experiments.""" + + # Create a user context + user = optimizely_user_context.OptimizelyUserContext( + optimizely_client=None, + logger=None, + user_id="test_user", + user_attributes={} + ) + + # Create a non-CMAB experiment (no cmab attribute) + experiment = entities.Experiment( + '111150', + 'regular_experiment', + 'Running', + '111150', + [], + {}, + [ + entities.Variation('111151', 'variation_1'), + ], + [ + {'entityId': '111151', 'endOfRange': 10000}, + ], + ) + + # Set up a user profile tracker with a stored variation + ups = user_profile.UserProfileService() + tracker = user_profile.UserProfileTracker("test_user", ups, self.decision_service.logger) + + stored_variation = entities.Variation('111151', 'variation_1') + + with mock.patch('optimizely.helpers.experiment.is_experiment_running', return_value=True), \ + mock.patch.object(self.decision_service, 'get_forced_variation', return_value=[None, []]), \ + mock.patch.object(self.decision_service, 'get_whitelisted_variation', return_value=[None, []]), \ + mock.patch.object(self.decision_service, 'get_stored_variation', + return_value=stored_variation) as mock_stored_variation, \ + mock.patch.object(self.decision_service, 'logger') as mock_logger: + + # Call get_variation with a non-CMAB experiment and a user profile tracker + variation_result = self.decision_service.get_variation( + self.project_config, + experiment, + user, + tracker + ) + + # Verify UPS lookup WAS called for non-CMAB experiment + mock_stored_variation.assert_called_once() + + # Verify the stored variation was returned + self.assertEqual(stored_variation, variation_result['variation']) + + # Verify there is no UPS exclusion reason + ups_skip_reasons = [r for r in variation_result['reasons'] + if 'Skipping user profile service lookup for CMAB experiment' in r] + self.assertEqual(len(ups_skip_reasons), 0) + class FeatureFlagDecisionTests(base.BaseTest): def setUp(self):