diff --git a/tests/e2e/budgets/test_spend_counter_reseed_e2e.py b/tests/e2e/budgets/test_spend_counter_reseed_e2e.py index 51f84665012..e964ebd5ea6 100644 --- a/tests/e2e/budgets/test_spend_counter_reseed_e2e.py +++ b/tests/e2e/budgets/test_spend_counter_reseed_e2e.py @@ -61,14 +61,18 @@ def _redis(): def _spend_counter(rds, key: str) -> float | None: """The shared spend counter for `key`, or None if it is cold. The counter key is - ``{cache namespace}:spend:key:{sha256(key)}``. With E2E_REDIS_NAMESPACE set (the - cluster-mode deploy, where a keyspace SCAN can't span shards) read it directly; - otherwise match by suffix so a local namespace need not be hard-coded.""" + the optional cache namespace plus ``spend:key:{sha256(key)}``. A cluster client + can't run a keyspace SCAN that spans shards, so on E2E_REDIS_CLUSTER (or when a + namespace is given) read the key directly - the namespaced key, then the bare + suffix. Otherwise match by suffix so a local namespace need not be hard-coded.""" digest = hashlib.sha256(key.encode()).hexdigest() suffix = f"spend:key:{digest}" namespace = os.getenv("E2E_REDIS_NAMESPACE") - if namespace: - for candidate in (f"{namespace}:{suffix}", suffix): + cluster = os.getenv("E2E_REDIS_CLUSTER", "false").lower() in ("1", "true", "yes") + if cluster or namespace: + candidates = [f"{namespace}:{suffix}"] if namespace else [] + candidates.append(suffix) + for candidate in candidates: raw = rds.get(candidate) if raw is not None: return float(raw)