Skip to content

Commit 173cc2f

Browse files
authored
move rulesets cb location (#435)
moving rulesets callback to after the store has finished parsing and updating, in case end user is expecting to evaluate right away using the new configs
1 parent b42e63c commit 173cc2f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

statsig/spec_store.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def get_target_app_for_sdk_key(self, sdk_key=None):
143143

144144
def get_default_environment(self):
145145
return self._default_environment
146+
146147
def get_session_replay_info(self):
147148
return self._session_replay_info
148149

@@ -170,9 +171,10 @@ def _process_specs(self, specs_json, source: DataSource) -> Tuple[bool, bool]:
170171
if specs_json.get("time", 0) < self.last_update_time(): # outdated lcut
171172
self._log_process("Failed to process specs, lcut is older than current lcut")
172173
return False, False
174+
175+
copy = None
173176
if callable(self._options.rules_updated_callback):
174177
copy = json.dumps(specs_json)
175-
self._options.rules_updated_callback(copy)
176178

177179
def get_parsed_specs(key: str):
178180
parsed = {}
@@ -267,6 +269,8 @@ def parse_target_value_map_from_spec(spec, parsed):
267269
sampling_rate = specs_json.get("diagnostics", {})
268270
self._diagnostics.set_sampling_rate(sampling_rate)
269271
self._log_process("Done processing specs")
272+
if callable(self._options.rules_updated_callback):
273+
self._options.rules_updated_callback(copy)
270274
return True, True
271275

272276
def _process_download_id_lists(self, server_id_lists):

tests/test_eval_callback.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import time
1+
import json
22
import os
3+
import time
34
import unittest
4-
import json
5-
6-
from typing import Optional, Union, Callable
7-
5+
from typing import Union
86
from unittest.mock import patch
7+
98
from gzip_helpers import GzipHelpers
109
from network_stub import NetworkStub
11-
from statsig import statsig, StatsigUser, StatsigOptions, StatsigEvent, StatsigEnvironmentTier, DynamicConfig, Layer, FeatureGate
10+
from statsig import statsig, StatsigUser, StatsigOptions, StatsigEnvironmentTier, DynamicConfig, Layer, \
11+
FeatureGate
1212

1313
with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../testdata/download_config_specs.json')) as r:
1414
CONFIG_SPECS_RESPONSE = r.read()
@@ -22,6 +22,7 @@ class TestEvalCallback(unittest.TestCase):
2222
_gateName = ""
2323
_configName = ""
2424
_layerName = ""
25+
_spec_match = False
2526

2627
@classmethod
2728
@patch('requests.request', side_effect=_network_stub.mock)
@@ -47,19 +48,24 @@ def log_event_callback(url: str, **kwargs):
4748
"regular_user_id", email="[email protected]", private_attributes={"test": 123})
4849
cls.random_user = StatsigUser("random")
4950
cls._logs = {}
51+
5052
def callback_func(config: Union[DynamicConfig, FeatureGate, Layer]):
5153
if isinstance(config, FeatureGate):
5254
cls._gateName = config.get_name()
5355
if isinstance(config, DynamicConfig):
5456
cls._configName = config.get_name()
5557
if isinstance(config, Layer):
5658
cls._layerName = config.get_name()
57-
59+
60+
def specs_callback(specs: str):
61+
cls._spec_match = json.loads(specs) == json.loads(CONFIG_SPECS_RESPONSE)
62+
5863
options = StatsigOptions(
5964
api=_network_stub.host,
6065
tier=StatsigEnvironmentTier.development,
6166
disable_diagnostics=True,
62-
evaluation_callback=callback_func)
67+
evaluation_callback=callback_func,
68+
rules_updated_callback=specs_callback, )
6369

6470
statsig.initialize("secret-key", options)
6571
cls.initTime = round(time.time() * 1000)
@@ -102,5 +108,9 @@ def test_c_experiment(self, mock_request):
102108
"a_layer"
103109
)
104110

111+
def test_d_spec_match(self, mock_request):
112+
self.assertTrue(self._spec_match)
113+
114+
105115
if __name__ == '__main__':
106116
unittest.main()

0 commit comments

Comments
 (0)