|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Ludovic Henry <git@ludovic.dev> |
| 3 | +Date: Sun, 20 Sep 2026 22:48:16 +0000 |
| 4 | +Subject: [PATCH] tests: wait for background specs syncs instead of assuming a |
| 5 | + 1ms interval |
| 6 | + |
| 7 | +`StatsigOptions` rejects a `specs_sync_interval_ms` below `MIN_SYNC_INTERVAL` |
| 8 | +(1000) and drops it back to the default, logging "Invalid |
| 9 | +'specs_sync_interval_ms', value must be greater than 1000, received Some(1)". |
| 10 | +Both fixtures in this file still ask for 1, so the six polling tests never get |
| 11 | +the near-instant background sync they were written against and fail on every |
| 12 | +platform -- reproduced here against the published |
| 13 | +manylinux_2_17_x86_64 wheel, where 6 of the 7 tests in this file fail. |
| 14 | + |
| 15 | +Use the smallest accepted interval -- the same 1000 upstream already switched |
| 16 | +`test_observability_client.py` to -- and wait for the condition each test is |
| 17 | +really about rather than for a fixed duration. The waits return as soon as the |
| 18 | +sync lands, so fast hardware pays nothing. |
| 19 | + |
| 20 | +Upstream-Status: To upstream [not yet submitted; the release is cut from a non-public tree, so this needs a maintainer discussion rather than a drive-by PR] |
| 21 | +--- |
| 22 | + statsig-pyo3/tests/test_data_store.py | 46 +++++++++++++++++---------- |
| 23 | + 1 file changed, 30 insertions(+), 16 deletions(-) |
| 24 | + |
| 25 | +diff --git a/statsig-pyo3/tests/test_data_store.py b/statsig-pyo3/tests/test_data_store.py |
| 26 | +index 9df3ac7..708c81b 100644 |
| 27 | +--- a/statsig-pyo3/tests/test_data_store.py |
| 28 | ++++ b/statsig-pyo3/tests/test_data_store.py |
| 29 | +@@ -17,6 +17,12 @@ from utils import get_test_data_resource, get_test_data_resource_bytes |
| 30 | + |
| 31 | + known_lcut = 1767981029384 |
| 32 | + |
| 33 | ++# StatsigOptions rejects a specs_sync_interval_ms below this and falls back to the |
| 34 | ++# default, so the polling tests below have to wait for a real sync to land. |
| 35 | ++MIN_SYNC_INTERVAL_MS = 1000 |
| 36 | ++SYNC_WAIT_ATTEMPTS = 200 |
| 37 | ++SYNC_WAIT_INTERVAL_S = 0.05 |
| 38 | ++ |
| 39 | + dcs_content = get_test_data_resource("eval_proj_dcs.json") |
| 40 | + json_data = json.loads(dcs_content) |
| 41 | + eval_proj_protobuf = get_test_data_resource_bytes("eval_proj_dcs.pb.br") |
| 42 | +@@ -169,7 +175,7 @@ def statsig_setup(httpserver: HTTPServer): |
| 43 | + specs_url=httpserver.url_for("/v2/download_config_specs"), |
| 44 | + log_event_url=httpserver.url_for("/v1/log_event"), |
| 45 | + data_store=data_store, |
| 46 | +- specs_sync_interval_ms=1, |
| 47 | ++ specs_sync_interval_ms=MIN_SYNC_INTERVAL_MS, |
| 48 | + ) |
| 49 | + |
| 50 | + statsig = Statsig("secret-key", options) |
| 51 | +@@ -193,7 +199,7 @@ def statsig_bytes_setup(httpserver: HTTPServer): |
| 52 | + specs_url=httpserver.url_for("/v2/download_config_specs"), |
| 53 | + log_event_url=httpserver.url_for("/v1/log_event"), |
| 54 | + data_store=data_store, |
| 55 | +- specs_sync_interval_ms=1, |
| 56 | ++ specs_sync_interval_ms=MIN_SYNC_INTERVAL_MS, |
| 57 | + ) |
| 58 | + |
| 59 | + statsig = Statsig("secret-key", options) |
| 60 | +@@ -223,10 +229,10 @@ def test_data_store_usage_get(statsig_setup): |
| 61 | + assert gate.value == True |
| 62 | + assert gate.details.lcut == known_lcut |
| 63 | + |
| 64 | +- for _ in range(100): |
| 65 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 66 | + if data_store.get_called_count >= 2: |
| 67 | + break |
| 68 | +- sleep(0.05) |
| 69 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 70 | + |
| 71 | + assert data_store.get_called_count > 1 |
| 72 | + |
| 73 | +@@ -240,9 +246,13 @@ def test_data_store_usage_set(statsig_setup): |
| 74 | + |
| 75 | + assert data_store.init_called |
| 76 | + assert gate.details.reason == "Adapter(DataStore):Recognized" |
| 77 | +- sleep(1) |
| 78 | + |
| 79 | +- gate_after = statsig.get_feature_gate(user, "test_public") |
| 80 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 81 | ++ gate_after = statsig.get_feature_gate(user, "test_public") |
| 82 | ++ if gate_after.details.lcut == known_lcut + 10: |
| 83 | ++ break |
| 84 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 85 | ++ |
| 86 | + statsig.flush_events().wait() |
| 87 | + |
| 88 | + assert gate_after.value == True |
| 89 | +@@ -265,10 +275,10 @@ def test_data_store_usage_get_bytes(statsig_bytes_setup): |
| 90 | + assert gate.value == True |
| 91 | + assert gate.details.lcut == known_lcut |
| 92 | + |
| 93 | +- for _ in range(5): |
| 94 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 95 | + if data_store.set_bytes_called_count > 0: |
| 96 | + break |
| 97 | +- sleep(0.05) |
| 98 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 99 | + |
| 100 | + assert data_store.get_bytes_called_count >= 1 |
| 101 | + assert data_store.get_called_count == 0 |
| 102 | +@@ -283,10 +293,10 @@ def test_data_store_usage_get_bytes_request_has_since_time_after_initial_poll(st |
| 103 | + gate = statsig.get_feature_gate(user, "test_public") |
| 104 | + assert gate.details.reason == "Adapter(DataStore):Recognized" |
| 105 | + |
| 106 | +- for _ in range(100): |
| 107 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 108 | + if data_store.get_bytes_called_count >= 2: |
| 109 | + break |
| 110 | +- sleep(0.05) |
| 111 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 112 | + |
| 113 | + assert data_store.get_bytes_called_count >= 2 |
| 114 | + |
| 115 | +@@ -308,10 +318,10 @@ def test_data_store_usage_get_bytes_request_checksum_match_returns_no_update(sta |
| 116 | + gate = statsig.get_feature_gate(user, "test_public") |
| 117 | + assert gate.details.reason == "Adapter(DataStore):Recognized" |
| 118 | + |
| 119 | +- for _ in range(100): |
| 120 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 121 | + if data_store.get_bytes_called_count >= 2: |
| 122 | + break |
| 123 | +- sleep(0.05) |
| 124 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 125 | + |
| 126 | + assert data_store.get_bytes_called_count >= 2 |
| 127 | + assert data_store.returned_no_update |
| 128 | +@@ -325,10 +335,10 @@ def test_data_store_usage_get_bytes_request_checksum_match_returns_no_update(sta |
| 129 | + assert request_with_checksum.since_time == data_store.stored_time |
| 130 | + |
| 131 | + # no second write should occur when server responds with {"has_updates": false} |
| 132 | +- for _ in range(100): |
| 133 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 134 | + if data_store.get_bytes_called_count > 2: |
| 135 | + break |
| 136 | +- sleep(0.05) |
| 137 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +@@ -341,9 +351,13 @@ def test_data_store_usage_set_bytes(statsig_bytes_setup): |
| 142 | + |
| 143 | + assert data_store.init_called |
| 144 | + assert gate.details.reason == "Adapter(DataStore):Recognized" |
| 145 | +- sleep(1) |
| 146 | + |
| 147 | +- gate_after = statsig.get_feature_gate(user, "test_public") |
| 148 | ++ for _ in range(SYNC_WAIT_ATTEMPTS): |
| 149 | ++ gate_after = statsig.get_feature_gate(user, "test_public") |
| 150 | ++ if gate_after.details.lcut == known_lcut + 10: |
| 151 | ++ break |
| 152 | ++ sleep(SYNC_WAIT_INTERVAL_S) |
| 153 | ++ |
| 154 | + statsig.flush_events().wait() |
| 155 | + |
| 156 | + assert gate_after.value == True |
0 commit comments