diff --git a/shared/rollouts/__init__.py b/shared/rollouts/__init__.py index 116c9a115..ae909816a 100644 --- a/shared/rollouts/__init__.py +++ b/shared/rollouts/__init__.py @@ -93,9 +93,7 @@ def __init__(self, name, proportion=None, salt=None): args["salt"] = salt # so it is hashable - args = tuple(sorted(args.items())) - - self._fetch_and_set_from_db(args) + self.args = tuple(sorted(args.items())) def check_value(self, identifier, default=False): """ @@ -104,7 +102,12 @@ def check_value(self, identifier, default=False): feature variants via Django Admin. """ # Will only run and refresh values from the database every ~5 minutes due to TTL cache - self._fetch_and_set_from_db() + self._fetch_and_set_from_db(self.args) + + if ( + self.args + ): # to create a default when `check_value()` is run for the first time + self.args = None return self._check_value(identifier, default) diff --git a/tests/unit/test_rollouts.py b/tests/unit/test_rollouts.py index 857e9bd0d..7da35d851 100644 --- a/tests/unit/test_rollouts.py +++ b/tests/unit/test_rollouts.py @@ -24,10 +24,13 @@ def test_buckets(self): "complex", 0.5, ) + # # To make the math simpler, let's pretend our hash function can only # # return 200 different values. with patch.object(Feature, "HASHSPACE", 200): + complex_feature.check_value("garbage") # to force fetch values from db + # Because the top-level feature proportion is 0.5, we are only using the # first 50% of our 200 hash values as our test population: [0..100] # Each feature variant has a proportion of 1/3, so our three buckets