|
3 | 3 | import logging |
4 | 4 | import os |
5 | 5 | import tempfile |
6 | | -from threading import Event |
| 6 | +from threading import Event, Timer |
7 | 7 | from typing import List |
8 | 8 |
|
9 | 9 | import pytest |
@@ -880,3 +880,80 @@ def test_does_not_warn_when_payload_filter_key_is_not_configured(caplog): |
880 | 880 | FDv2(Config(sdk_key="dummy"), DataSystemConfig(initializers=None, synchronizers=None)) |
881 | 881 |
|
882 | 882 | assert not any("Payload filtering" in record.message for record in caplog.records) |
| 883 | + |
| 884 | + |
| 885 | +class _BlockingInitializer(Initializer): |
| 886 | + """An initializer whose fetch waits until stop is called, then returns data.""" |
| 887 | + |
| 888 | + def __init__(self): |
| 889 | + self.fetching = Event() |
| 890 | + self.stopped = Event() |
| 891 | + |
| 892 | + @property |
| 893 | + def name(self) -> str: |
| 894 | + return "blocking" |
| 895 | + |
| 896 | + def fetch(self, ss: SelectorStore) -> BasisResult: |
| 897 | + self.fetching.set() |
| 898 | + self.stopped.wait(5) |
| 899 | + builder = ChangeSetBuilder() |
| 900 | + builder.start(IntentCode.TRANSFER_FULL) |
| 901 | + change_set = builder.finish(Selector(state="blocking-state", version=1)) |
| 902 | + return _Success(value=Basis(change_set=change_set, persist=False, environment_id=None)) |
| 903 | + |
| 904 | + def stop(self): |
| 905 | + self.stopped.set() |
| 906 | + |
| 907 | + |
| 908 | +def test_stop_stops_the_initializer_in_progress_and_ignores_its_result(): |
| 909 | + initializer = _BlockingInitializer() |
| 910 | + td = TestDataV2.data_source() |
| 911 | + fdv2 = FDv2( |
| 912 | + Config(sdk_key="dummy"), |
| 913 | + DataSystemConfig(initializers=[_InitializerBuilder(initializer)], synchronizers=[td.builder]), |
| 914 | + ) |
| 915 | + set_on_ready = Event() |
| 916 | + fdv2.start(set_on_ready) |
| 917 | + assert initializer.fetching.wait(5), "The initializer did not start" |
| 918 | + |
| 919 | + fdv2.stop() |
| 920 | + |
| 921 | + assert initializer.stopped.is_set() |
| 922 | + # The initializer returned data after stop. The data system does not apply it. |
| 923 | + assert not set_on_ready.is_set() |
| 924 | + assert not fdv2.store.initialized |
| 925 | + |
| 926 | + |
| 927 | +class _InitializerWithoutStop: |
| 928 | + """An initializer that matches the protocol but does not inherit from it, so it has no stop.""" |
| 929 | + |
| 930 | + def __init__(self): |
| 931 | + self.fetching = Event() |
| 932 | + self.release = Event() |
| 933 | + |
| 934 | + @property |
| 935 | + def name(self) -> str: |
| 936 | + return "without-stop" |
| 937 | + |
| 938 | + def fetch(self, ss: SelectorStore) -> BasisResult: |
| 939 | + self.fetching.set() |
| 940 | + self.release.wait(5) |
| 941 | + return _Fail(error="released") |
| 942 | + |
| 943 | + |
| 944 | +def test_stop_accepts_an_initializer_in_progress_without_a_stop_method(caplog): |
| 945 | + initializer = _InitializerWithoutStop() |
| 946 | + fdv2 = FDv2( |
| 947 | + Config(sdk_key="dummy"), |
| 948 | + DataSystemConfig(initializers=[_InitializerBuilder(initializer)], synchronizers=None), |
| 949 | + ) |
| 950 | + fdv2.start(Event()) |
| 951 | + assert initializer.fetching.wait(5), "The initializer did not start" |
| 952 | + |
| 953 | + # The initializer cannot be stopped, so release it shortly after stop begins. |
| 954 | + release = Timer(0.2, initializer.release.set) |
| 955 | + release.start() |
| 956 | + fdv2.stop() |
| 957 | + release.join() |
| 958 | + |
| 959 | + assert not any(record.levelno >= logging.ERROR for record in caplog.records) |
0 commit comments