18 def userver_cache_control_hooks(self) -> typing.Dict[str, str]:
21 def pytest_plugin_registered(self, plugin, manager):
22 if not isinstance(plugin, types.ModuleType):
24 uhooks = getattr(plugin,
'USERVER_CACHE_CONTROL_HOOKS',
None)
27 if not isinstance(uhooks, dict):
29 f
'USERVER_CACHE_CONTROL_HOOKS must be dictionary: {{cache_name: fixture_name}}, got {uhooks} instead',
31 for cache_name, fixture_name
in uhooks.items():
32 if cache_name
in self.
_hooks:
34 f
'USERVER_CACHE_CONTROL_HOOKS: hook already registered for cache {cache_name}',
36 self.
_hooks[cache_name] = fixture_name
45 def invalidate_all(self) -> None:
48 def invalidate(self, caches: typing.Iterable[str]) ->
None:
53 def should_update_all_caches(self) -> bool:
57 def caches_to_update(self) -> typing.FrozenSet[str]:
62 def has_caches_to_update(self) -> bool:
64 return caches
is None or bool(caches)
66 def on_caches_updated(self, caches: typing.Iterable[str]) ->
None:
70 def on_all_caches_updated(self) -> None:
73 def assign_copy(self, other:
'InvalidationState') ->
None:
186 _userver_cache_control_context,
187 _userver_cache_fixtures,
190 """Userver cache control handler.
192 To install per cache handler use USERVER_CACHE_CONTROL_HOOKS variable
193 in your pytest plugin:
196 USERVER_CACHE_CONTROL_HOOKS = {
197 'my-cache-name': 'my_cache_cc',
201 def my_cache_cc(my_cache_context):
202 def cache_control(request, state):
203 new_state = my_cache_context.get_state()
204 if state == new_state:
205 # Cache is already up to date, no need to update
208 # Request incremental update, if you cache supports it
209 request.incremental()
214 @ingroup userver_testsuite_fixtures
217 caches_disabled = set()
219 def userver_cache_control_disabled(
220 caches: typing.Sequence[str] =
None,
224 if caches
is not None:
225 caches_disabled.update(caches)
229 for mark
in request.node.iter_markers(
'userver_cache_control_disabled'):
230 enabled = userver_cache_control_disabled(*mark.args, **mark.kwargs)
233 context=_userver_cache_control_context,
234 fixtures=_userver_cache_fixtures,
236 caches_disabled=caches_disabled,
240@pytest.fixture(scope='session')