22 def userver_cache_control_hooks(self) -> dict[str, str]:
25 def pytest_plugin_registered(self, plugin, manager):
26 if not isinstance(plugin, types.ModuleType):
28 uhooks = getattr(plugin,
'USERVER_CACHE_CONTROL_HOOKS',
None)
31 if not isinstance(uhooks, dict):
33 f
'USERVER_CACHE_CONTROL_HOOKS must be dictionary: {{cache_name: fixture_name}}, got {uhooks} instead',
35 for cache_name, fixture_name
in uhooks.items():
36 if cache_name
in self.
_hooks:
38 f
'USERVER_CACHE_CONTROL_HOOKS: hook already registered for cache {cache_name}',
40 self.
_hooks[cache_name] = fixture_name
44 def __init__(self) -> None:
49 def invalidate_all(self) -> None:
52 def invalidate(self, caches: Iterable[str]) ->
None:
57 def should_update_all_caches(self) -> bool:
61 def caches_to_update(self) -> frozenset[str]:
66 def has_caches_to_update(self) -> bool:
68 return caches
is None or bool(caches)
70 def on_caches_updated(self, caches: Iterable[str]) ->
None:
74 def on_all_caches_updated(self) -> None:
77 def assign_copy(self, other:
'InvalidationState') ->
None:
187 _userver_cache_control_context,
188 _userver_cache_fixtures,
191 """Userver cache control handler.
193 To install per cache handler use USERVER_CACHE_CONTROL_HOOKS variable
194 in your pytest plugin:
197 USERVER_CACHE_CONTROL_HOOKS = {
198 'my-cache-name': 'my_cache_cc',
202 def my_cache_cc(my_cache_context):
203 def cache_control(request, state):
204 new_state = my_cache_context.get_state()
205 if state == new_state:
206 # Cache is already up to date, no need to update
209 # Request incremental update, if you cache supports it
210 request.incremental()
215 @ingroup userver_testsuite_fixtures
218 caches_disabled = set()
220 def userver_cache_control_disabled(caches: Sequence[str] =
None, *, reason: str):
221 if caches
is not None:
222 caches_disabled.update(caches)
226 for mark
in request.node.iter_markers(
'userver_cache_control_disabled'):
227 enabled = userver_cache_control_disabled(*mark.args, **mark.kwargs)
230 context=_userver_cache_control_context,
231 fixtures=_userver_cache_fixtures,
233 caches_disabled=caches_disabled,
237@pytest.fixture(scope='session')