Skip to content

Commit

Permalink
Fix issues introduced by custom stickiness support! (#147)
Browse files Browse the repository at this point in the history
* Fix issues with stickiness when key is not available.

* Revert stickiness fallback.

* Revert the revert!

* Run PR tests on pull request, not on master. 🤦
  • Loading branch information
ivanklee86 committed Mar 26, 2021
1 parent 1dcd2c6 commit f19b4b0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions UnleashClient/strategies/FlexibleRolloutStrategy.py
Expand Up @@ -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():
Expand All @@ -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
8 changes: 6 additions & 2 deletions UnleashClient/variants/Variants.py
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/specification_tests/test_10_flexible_rollout.py
Expand Up @@ -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)
Expand Down

0 comments on commit f19b4b0

Please sign in to comment.