|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Generator |
| 3 | +import pytest |
| 4 | +from common import CommonScenario, ResultCode, temp_dir_common |
| 5 | +from testing_utils import ScenarioResult, LogContainer |
| 6 | + |
| 7 | +pytestmark = pytest.mark.parametrize("version", ["rust"], scope="class") |
| 8 | + |
| 9 | + |
| 10 | +class PersistencyScenario(CommonScenario): |
| 11 | + """ |
| 12 | + Common base implementation for persistency tests. |
| 13 | + """ |
| 14 | + |
| 15 | + def instance_id(self) -> int: |
| 16 | + return 2 |
| 17 | + |
| 18 | + @pytest.fixture(scope="class") |
| 19 | + def temp_dir( |
| 20 | + self, tmp_path_factory: pytest.TempPathFactory, version: str |
| 21 | + ) -> Generator[Path, None, None]: |
| 22 | + yield from temp_dir_common(tmp_path_factory, self.__class__.__name__, version) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.PartiallyVerifies([]) |
| 26 | +@pytest.mark.FullyVerifies(["comp_req__persistency__persist_data_store_com"]) |
| 27 | +@pytest.mark.Description( |
| 28 | + "Verifies that disabling flush on exit but manually flushing ensures data is persisted correctly." |
| 29 | +) |
| 30 | +@pytest.mark.TestType("requirements-based") |
| 31 | +@pytest.mark.DerivationTechnique("requirements-based") |
| 32 | +class TestExplicitFlush(PersistencyScenario): |
| 33 | + NUM_VALUES = 5 |
| 34 | + |
| 35 | + @pytest.fixture(scope="class") |
| 36 | + def scenario_name(self) -> str: |
| 37 | + return "cit.persistency.explicit_flush" |
| 38 | + |
| 39 | + @pytest.fixture(scope="class") |
| 40 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 41 | + return { |
| 42 | + "kvs_parameters": { |
| 43 | + "instance_id": self.instance_id(), |
| 44 | + "dir": str(temp_dir), |
| 45 | + "flush_on_exit": False, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + def test_data_stored(self, results: ScenarioResult, logs_info_level: LogContainer): |
| 50 | + assert results.return_code == ResultCode.SUCCESS |
| 51 | + |
| 52 | + for i in range(self.NUM_VALUES): |
| 53 | + log = logs_info_level.find_log("key", value=f"test_number_{i}") |
| 54 | + assert log is not None |
| 55 | + assert log.value == f"Ok(F64({12.3 * i}))" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.PartiallyVerifies([]) |
| 59 | +@pytest.mark.FullyVerifies(["comp_req__persistency__persist_data_store_com"]) |
| 60 | +@pytest.mark.Description( |
| 61 | + "Verifies that data is automatically flushed and persisted when the KVS instance is dropped, with flush on exit enabled." |
| 62 | +) |
| 63 | +@pytest.mark.TestType("requirements-based") |
| 64 | +@pytest.mark.DerivationTechnique("requirements-based") |
| 65 | +class TestFlushOnExitEnabled(PersistencyScenario): |
| 66 | + NUM_VALUES = 5 |
| 67 | + |
| 68 | + @pytest.fixture(scope="class") |
| 69 | + def scenario_name(self) -> str: |
| 70 | + return "cit.persistency.flush_on_exit" |
| 71 | + |
| 72 | + @pytest.fixture(scope="class") |
| 73 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 74 | + return { |
| 75 | + "kvs_parameters": { |
| 76 | + "instance_id": self.instance_id(), |
| 77 | + "dir": str(temp_dir), |
| 78 | + "flush_on_exit": True, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + def test_data_stored(self, results: ScenarioResult, logs_info_level: LogContainer): |
| 83 | + assert results.return_code == ResultCode.SUCCESS |
| 84 | + |
| 85 | + for i in range(self.NUM_VALUES): |
| 86 | + log = logs_info_level.find_log("key", value=f"test_number_{i}") |
| 87 | + assert log is not None |
| 88 | + assert log.value == f"Ok(F64({12.3 * i}))" |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.PartiallyVerifies([]) |
| 92 | +@pytest.mark.FullyVerifies(["comp_req__persistency__persist_data_store_com"]) |
| 93 | +@pytest.mark.Description( |
| 94 | + "Checks that disabling flush on exit causes data to be dropped and not persisted after the KVS instance is dropped." |
| 95 | +) |
| 96 | +@pytest.mark.TestType("requirements-based") |
| 97 | +@pytest.mark.DerivationTechnique("requirements-based") |
| 98 | +class TestFlushOnExitDisabled(PersistencyScenario): |
| 99 | + NUM_VALUES = 5 |
| 100 | + |
| 101 | + @pytest.fixture(scope="class") |
| 102 | + def scenario_name(self) -> str: |
| 103 | + return "cit.persistency.flush_on_exit" |
| 104 | + |
| 105 | + @pytest.fixture(scope="class") |
| 106 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 107 | + return { |
| 108 | + "kvs_parameters": { |
| 109 | + "instance_id": self.instance_id(), |
| 110 | + "dir": str(temp_dir), |
| 111 | + "flush_on_exit": False, |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + def test_data_dropped(self, results: ScenarioResult, logs_info_level: LogContainer): |
| 116 | + assert results.return_code == ResultCode.SUCCESS |
| 117 | + |
| 118 | + for i in range(self.NUM_VALUES): |
| 119 | + log = logs_info_level.find_log("key", value=f"test_number_{i}") |
| 120 | + assert log is not None |
| 121 | + assert log.value == "Err(KeyNotFound)" |
0 commit comments