diff --git a/UnleashClient/strategies/FlexibleRolloutStrategy.py b/UnleashClient/strategies/FlexibleRolloutStrategy.py index a78c9fe9..26c475b9 100644 --- a/UnleashClient/strategies/FlexibleRolloutStrategy.py +++ b/UnleashClient/strategies/FlexibleRolloutStrategy.py @@ -16,7 +16,7 @@ def apply(self, context: dict = None) -> bool: """ percentage = int(self.parameters['rollout']) activation_group = self.parameters['groupId'] - stickiness = self.parameters['stickiness'] + stickiness = self.parameters['stickiness'] if "stickiness" in self.parameters else "default" if stickiness == 'default': if 'userId' in context.keys(): @@ -25,10 +25,9 @@ def apply(self, context: dict = None) -> bool: calculated_percentage = normalized_hash(context['sessionId'], activation_group) else: calculated_percentage = self.random_hash() - elif stickiness in context.keys(): - calculated_percentage = normalized_hash(context[stickiness], activation_group) - else: - # This also handles the stickiness == random scenario. + elif stickiness == "random": calculated_percentage = self.random_hash() + else: + calculated_percentage = normalized_hash(context[stickiness], activation_group) return percentage > 0 and calculated_percentage <= percentage diff --git a/UnleashClient/variants/Variants.py b/UnleashClient/variants/Variants.py index eef7fa17..ca6d6ee3 100644 --- a/UnleashClient/variants/Variants.py +++ b/UnleashClient/variants/Variants.py @@ -36,7 +36,7 @@ def _apply_overrides(self, context: dict) -> dict: @staticmethod def _get_seed(context: dict, stickiness_selector: str = "default") -> str: """Grabs seed value from context.""" - seed = str(random.random() * 10000) + seed = "" if stickiness_selector == "default": if 'userId' in context: @@ -45,7 +45,11 @@ def _get_seed(context: dict, stickiness_selector: str = "default") -> str: seed = context['sessionId'] elif 'remoteAddress' in context: seed = context['remoteAddress'] - elif stickiness_selector in context.keys(): + else: + seed = str(random.random() * 10000) + elif stickiness_selector == 'random': + seed = str(random.random() * 10000) + else: seed = context[stickiness_selector] return seed diff --git a/tests/specification_tests/test_10_flexible_rollout.py b/tests/specification_tests/test_10_flexible_rollout.py index ff44c81d..50774b7e 100644 --- a/tests/specification_tests/test_10_flexible_rollout.py +++ b/tests/specification_tests/test_10_flexible_rollout.py @@ -197,7 +197,7 @@ def test_feature_55sessionid_disabled(unleash_client): @responses.activate def test_feature_55_nouserdisabled(unleash_client): """ - Feature.flexibleRollout.userId.55 should be disabled + Feature.flexibleRollout.userId.55 should be disabled if no userId in context """ # Set up API responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202)