userver: /data/code/service_template/third_party/userver/testsuite/pytest_plugins/pytest_userver/plugins/caches.py Source File
Loading...
Searching...
No Matches
caches.py
1"""
2Fixtures for controlling userver caches.
3"""
4import copy
5import typing
6
7import pytest
8
9
11 def __init__(self):
12 # None means that we should update all caches.
13 # We invalidate all caches at the start of each test.
14 self._invalidated_caches: typing.Optional[typing.Set[str]] = None
15
16 def invalidate_all(self) -> None:
17 self._invalidated_caches = None
18
19 def invalidate(self, caches: typing.Iterable[str]) -> None:
20 if self._invalidated_caches is not None:
21 self._invalidated_caches.update(caches)
22
23 @property
24 def should_update_all_caches(self) -> bool:
25 return self._invalidated_caches is None
26
27 @property
28 def caches_to_update(self) -> typing.FrozenSet[str]:
29 assert self._invalidated_caches is not None
30 return frozenset(self._invalidated_caches)
31
32 @property
33 def has_caches_to_update(self) -> bool:
34 caches = self._invalidated_caches
35 return caches is None or bool(caches)
36
37 def on_caches_updated(self, caches: typing.Iterable[str]) -> None:
38 if self._invalidated_caches is not None:
39 self._invalidated_caches.difference_update(caches)
40
41 def on_all_caches_updated(self) -> None:
42 self._invalidated_caches = set()
43
44 def assign_copy(self, other: 'InvalidationState') -> None:
45 # pylint: disable=protected-access
46 self._invalidated_caches = copy.deepcopy(other._invalidated_caches)
47
48
49@pytest.fixture
50def cache_invalidation_state() -> InvalidationState:
51 """
52 A fixture for notifying the service of changes in cache data sources.
53
54 Intended to be used by other fixtures that represent those data sources,
55 not by tests directly.
56
57 @ingroup userver_testsuite_fixtures
58 """
59 return InvalidationState()