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