Skip to content

Commit

Permalink
update error log and test
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Aug 1, 2024
1 parent 646dd5a commit 9864e46
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/amplitude_experiment/local/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ def _required_cohorts_in_storage(self, flag_configs: List) -> None:
for flag in flag_configs:
flag_cohort_ids = get_all_cohort_ids_from_flag(flag)
missing_cohorts = flag_cohort_ids - stored_cohort_ids
if self.config.cohort_sync_config and missing_cohorts:
self.logger.warning(f"Evaluating flag {flag['key']} with cohorts {flag_cohort_ids} without "
f"cohort syncing configured")
if missing_cohorts:
message = (
f"Evaluating flag {flag['key']} dependent on cohorts {flag_cohort_ids} "
f"without {missing_cohorts} in storage"
if self.config.cohort_sync_config
else f"Evaluating flag {flag['key']} dependent on cohorts {flag_cohort_ids} without "
f"cohort syncing configured"
)
self.logger.warning(message)

def _enrich_user_with_cohorts(self, user: User, flag_configs: Dict) -> User:
grouped_cohort_ids = get_grouped_cohort_ids_from_flags(list(flag_configs.values()))
Expand Down
6 changes: 3 additions & 3 deletions tests/local/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ def test_evaluate_with_group_cohort(self):
expected_off_variant = Variant(key='off')
self.assertEqual(expected_off_variant, non_targeted_variant)

def test_evaluation_cohorts_not_in_storage(self):
def test_evaluation_cohorts_not_in_storage_with_sync_config(self):
with mock.patch.object(self._local_evaluation_client.cohort_storage, 'put_cohort', return_value=None):
self._local_evaluation_client.cohort_storage.get_cohort_ids = mock.Mock(return_value=set())
targeted_user = User(user_id='12345')

with self.assertLogs(self._local_evaluation_client.logger, level='WARNING') as log:
self._local_evaluation_client.evaluate_v2(targeted_user, {'sdk-local-evaluation-user-cohort-ci-test'})
log_message = (
"WARNING:Amplitude:Evaluating flag sdk-local-evaluation-user-cohort-ci-test with cohorts "
"{.*} without cohort syncing configured"
"WARNING:Amplitude:Evaluating flag sdk-local-evaluation-user-cohort-ci-test dependent on cohorts "
"{.*} without {.*} in storage"
)
self.assertTrue(any(re.match(log_message, message) for message in log.output))

Expand Down

0 comments on commit 9864e46

Please sign in to comment.