|
3 | 3 | from threading import Event |
4 | 4 | from typing import Any, Callable, Dict, List, Mapping, Optional |
5 | 5 |
|
| 6 | +import pytest |
| 7 | + |
| 8 | +from ldclient.client import Context |
6 | 9 | from ldclient.config import Config, DataSystemConfig |
7 | 10 | from ldclient.impl.datasystem import DataAvailability |
8 | 11 | from ldclient.impl.datasystem.fdv2 import FDv2 |
9 | 12 | from ldclient.integrations.test_datav2 import TestDataV2 |
10 | 13 | from ldclient.interfaces import DataStoreMode, FeatureStore, FlagChange |
| 14 | +from ldclient.testing.test_ldclient import make_client |
11 | 15 | from ldclient.versioned_data_kind import FEATURES, SEGMENTS, VersionedDataKind |
12 | 16 |
|
13 | 17 |
|
@@ -782,3 +786,65 @@ def init(self, all_data): |
782 | 786 | assert err is not None, "Commit should return error from persistent store" |
783 | 787 | assert isinstance(err, RuntimeError) |
784 | 788 | assert str(err) == "Simulated persistent store failure" |
| 789 | + |
| 790 | + |
| 791 | +class ThrowingInitializedStore(StubFeatureStore): |
| 792 | + """A persistent store whose ``initialized`` check raises, to simulate a |
| 793 | + store I/O error (for example a Redis connection failure) during the |
| 794 | + warm-start availability gate.""" |
| 795 | + |
| 796 | + @property |
| 797 | + def initialized(self) -> bool: |
| 798 | + raise RuntimeError("persistent store I/O error") |
| 799 | + |
| 800 | + |
| 801 | +def test_variation_does_not_throw_when_persistent_store_errors_during_warm_start(caplog): |
| 802 | + """A persistent-store error at the warm-start availability gate must not |
| 803 | + propagate out of the client. |
| 804 | +
|
| 805 | + While a synchronizer is configured but has not yet supplied a basis, the |
| 806 | + availability gate consults the persistent store's initialized state. If that |
| 807 | + query raises, evaluation must degrade to the default value with |
| 808 | + ``CLIENT_NOT_READY`` and ``all_flags_state()`` must return an invalid state, |
| 809 | + rather than raising. This is the sync counterpart to the async fix in #486. |
| 810 | + """ |
| 811 | + persistent_store = ThrowingInitializedStore() |
| 812 | + |
| 813 | + # A synchronizer is configured but the data system is never started, so no |
| 814 | + # basis arrives. This is the warm-start window in which the gate reads the |
| 815 | + # persistent store's initialized state. |
| 816 | + data_system_config = DataSystemConfig( |
| 817 | + data_store_mode=DataStoreMode.READ_ONLY, |
| 818 | + data_store=persistent_store, |
| 819 | + initializers=None, |
| 820 | + synchronizers=[TestDataV2.data_source().builder], |
| 821 | + ) |
| 822 | + fdv2 = FDv2(Config(sdk_key="dummy"), data_system_config) |
| 823 | + |
| 824 | + # The gate itself raises: this is the unguarded root the fix addresses. |
| 825 | + with pytest.raises(RuntimeError): |
| 826 | + _ = fdv2.data_availability |
| 827 | + |
| 828 | + # Drive the same failing gate through the client and confirm it degrades |
| 829 | + # instead of propagating the error. |
| 830 | + client = make_client() |
| 831 | + try: |
| 832 | + client._data_system = fdv2 |
| 833 | + context = Context.from_dict({"key": "user", "kind": "user"}) |
| 834 | + |
| 835 | + assert client.variation("flag-key", context, default="default-value") == "default-value" |
| 836 | + |
| 837 | + detail = client.variation_detail("flag-key", context, default="default-value") |
| 838 | + assert detail.value == "default-value" |
| 839 | + assert detail.reason == {"kind": "ERROR", "errorKind": "CLIENT_NOT_READY"} |
| 840 | + assert detail.is_default_value() is True |
| 841 | + |
| 842 | + assert client.all_flags_state(context).valid is False |
| 843 | + finally: |
| 844 | + client.close() |
| 845 | + |
| 846 | + assert any( |
| 847 | + "Error checking data availability" in record.message |
| 848 | + for record in caplog.records |
| 849 | + if record.levelname == "ERROR" |
| 850 | + ) |
0 commit comments