Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions optimizely/decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,15 @@ 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:
# CMAB experiments are excluded from UPS because UPS maintains decisions
# across the experiment lifetime without considering TTL or user attributes,
# which contradicts CMAB's dynamic decision-making nature.
if experiment.cmab:
message = f'Skipping UPS lookup for CMAB experiment "{experiment.key}". ' \
'CMAB decisions are excluded from UserProfileService.'
self.logger.debug(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 ' \
Expand Down Expand Up @@ -529,7 +537,13 @@ 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:
# CMAB experiments are excluded from UPS to preserve dynamic decision-making.
if experiment.cmab:
message = f'Skipping UPS save for CMAB experiment "{experiment.key}". ' \
'CMAB decisions are excluded from UserProfileService.'
self.logger.debug(message)
decide_reasons.append(message)
elif user_profile_tracker is not None and not ignore_user_profile:
try:
user_profile_tracker.update_user_profile(experiment, variation)
except:
Expand Down
217 changes: 217 additions & 0 deletions tests/test_decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,223 @@ 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.
CMAB should not use UPS for sticky bucketing because UPS maintains
decisions without considering TTL or user attributes."""

# 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',
[], # No audience IDs
{},
[
entities.Variation('111151', 'variation_1'),
entities.Variation('111152', 'variation_2')
],
[
{'entityId': '111151', 'endOfRange': 5000},
{'entityId': '111152', 'endOfRange': 10000}
],
cmab={'trafficAllocation': 5000}
)

# Create a user profile tracker with a stored variation
user_profile_service = user_profile.UserProfileService()
user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service)

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_get_stored, \
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-123'
},
[] # reasons list
)

# Call get_variation with user_profile_tracker
variation_result = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
user_profile_tracker
)
variation = variation_result['variation']
reasons = variation_result['reasons']

# Verify UPS lookup was NOT called for CMAB experiment
mock_get_stored.assert_not_called()

# Verify the UPS skip reason is in the decision reasons
self.assertIn(
'Skipping UPS lookup for CMAB experiment "cmab_experiment". '
'CMAB decisions are excluded from UserProfileService.',
reasons
)

# Verify we still get a valid variation from CMAB service
self.assertEqual(entities.Variation('111151', 'variation_1'), variation)

# Verify logger debug was called with UPS skip message
mock_logger.debug.assert_any_call(
'Skipping UPS lookup for CMAB experiment "cmab_experiment". '
'CMAB decisions are excluded from UserProfileService.'
)

def test_get_variation_cmab_experiment_skips_ups_save(self):
"""Test that get_variation skips UPS save for CMAB experiments.
CMAB decisions should not be saved to UPS to preserve dynamic
decision-making."""

# 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',
[], # No audience IDs
{},
[
entities.Variation('111151', 'variation_1'),
entities.Variation('111152', 'variation_2')
],
[
{'entityId': '111151', 'endOfRange': 5000},
{'entityId': '111152', 'endOfRange': 10000}
],
cmab={'trafficAllocation': 5000}
)

# Create a user profile tracker
user_profile_service = user_profile.UserProfileService()
user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service)

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(user_profile_tracker, 'update_user_profile') as mock_update_profile, \
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-123'
},
[] # reasons list
)

# Call get_variation with user_profile_tracker
variation_result = self.decision_service.get_variation(
self.project_config,
cmab_experiment,
user,
user_profile_tracker
)
variation = variation_result['variation']
reasons = variation_result['reasons']

# Verify UPS save was NOT called for CMAB experiment
mock_update_profile.assert_not_called()

# Verify the UPS save skip reason is in the decision reasons
self.assertIn(
'Skipping UPS save for CMAB experiment "cmab_experiment". '
'CMAB decisions are excluded from UserProfileService.',
reasons
)

# Verify we still get a valid variation
self.assertEqual(entities.Variation('111151', 'variation_1'), variation)

# Verify logger debug was called with UPS save skip message
mock_logger.debug.assert_any_call(
'Skipping UPS save for CMAB experiment "cmab_experiment". '
'CMAB decisions are excluded from UserProfileService.'
)

def test_get_variation_non_cmab_experiment_uses_ups_normally(self):
"""Test that non-CMAB experiments still use UPS for lookup and save normally."""

# Create a user context
user = optimizely_user_context.OptimizelyUserContext(
optimizely_client=None,
logger=None,
user_id="test_user",
user_attributes={}
)

experiment = self.project_config.get_experiment_from_key('test_experiment')

# Create a user profile tracker with a stored variation
user_profile_service = user_profile.UserProfileService()
user_profile_tracker = user_profile.UserProfileTracker(user.user_id, user_profile_service)

stored_variation = self.project_config.get_variation_from_id('test_experiment', '111129')

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_get_stored, \
mock.patch.object(self.decision_service,
'logger') as mock_logger:

# Call get_variation with user_profile_tracker
variation_result = self.decision_service.get_variation(
self.project_config,
experiment,
user,
user_profile_tracker
)
variation = variation_result['variation']
reasons = variation_result['reasons']

# Verify UPS lookup WAS called for non-CMAB experiment
mock_get_stored.assert_called_once()

# Verify we get the stored variation back
self.assertEqual(stored_variation, variation)

# Verify UPS skip messages are NOT in reasons
for reason in reasons:
self.assertNotIn('excluded from UserProfileService', reason)


class FeatureFlagDecisionTests(base.BaseTest):
def setUp(self):
Expand Down
Loading