userver: userver/testsuite/cache_control.hpp Source File
Loading...
Searching...
No Matches
cache_control.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/testsuite/cache_control.hpp
4/// @brief @copybrief testsuite::CacheControl
5
6#include <functional>
7#include <memory>
8#include <string>
9#include <string_view>
10#include <unordered_set>
11
12#include <userver/cache/update_type.hpp>
13#include <userver/components/component_fwd.hpp>
14#include <userver/utils/assert.hpp>
15#include <userver/utils/impl/internal_tag.hpp>
16#include <userver/utils/resource_scopes_fwd.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace cache {
22struct Config;
23} // namespace cache
24
25namespace components {
26class State;
27} // namespace components
28
29namespace testsuite {
30
31namespace impl {
32enum class PeriodicUpdatesMode { kDefault, kEnabled, kDisabled };
33
34using CacheReverseDependencies = std::unordered_set<std::string>;
35
36CacheReverseDependencies GetDefaultCacheReverseDependencies();
37
38} // namespace impl
39
40/// @brief Testsuite interface for caches and cache-like components.
41///
42/// If a component stores transient state that may be carried between tests,
43/// or stores caches that may become stale, then it should register its resetter
44/// here. Example:
45///
46/// @snippet core/src/testsuite/cache_control_test.cpp sample
47///
48/// Testsuite will then call this hook in the beginning of each test.
49/// You can also reset a specific cache in testsuite explicitly as follows:
50///
51/// @code
52/// service_client.invalidate_caches(names=['your-cache-name'])
53/// @endcode
54///
55/// CacheControl is normally acquired through testsuite::FindCacheControl.
56///
57/// All methods are coro-safe.
58class CacheControl final {
59public:
60 /// @brief Reset all the registered caches.
61 ///
62 /// @a update_type is used by caches derived from
63 /// @a component::CachingComponentBase.
65 cache::UpdateType update_type,
66 const std::unordered_set<std::string>& force_incremental_names,
67 const std::unordered_set<std::string>& exclude_names
68 );
69
70 /// @brief Reset caches with the specified @a names.
71 ///
72 /// Every registered resetter whose name is in @a reset_only_names is
73 /// invoked. Several resetters may share a name: for example a periodic
74 /// @ref cache::CacheUpdateTrait resetter and a later custom
75 /// @ref RegisterCacheResetter.
76 ///
77 /// @a update_type is used by caches derived from
78 /// @a component::CachingComponentBase.
80 cache::UpdateType update_type,
81 std::unordered_set<std::string> reset_only_names,
82 const std::unordered_set<std::string>& force_incremental_names
83 );
84
85 CacheControl(CacheControl&&) = delete;
86 CacheControl& operator=(CacheControl&&) = delete;
87
88 /// @cond
89 // For internal use only.
90 struct UnitTests {
91 explicit UnitTests() = default;
92 };
93
94 enum class ExecPolicy {
95 kSequential,
96 kConcurrent,
97 };
98
99 CacheControl(impl::PeriodicUpdatesMode, UnitTests);
100 CacheControl(
101 impl::PeriodicUpdatesMode,
102 ExecPolicy,
103 components::State,
104 impl::CacheReverseDependencies reverse_dependencies
105 );
106 ~CacheControl();
107
108 // For internal use only.
109 bool IsPeriodicUpdateEnabled(const cache::Config& cache_config, const std::string& cache_name) const;
110
111 // For internal use only.
112 void RegisterPeriodicCache(utils::ResourceScopeStorage& scopes, cache::CacheUpdateTrait& cache);
113
114 // For internal use only. Use testsuite::RegisterCacheResetter instead
115 void RegisterCache(
116 utils::impl::InternalTag,
117 utils::ResourceScopeStorage& scopes,
118 std::string_view name,
119 std::function<void(cache::UpdateType)> reset
120 );
121
122 /// @endcond
123private:
124 struct CacheInfo final {
125 std::string name;
126 std::function<void(cache::UpdateType)> reset;
127 bool needs_span{true};
128 };
129 struct CacheInfoNode;
130 using CacheInfoIterator = CacheInfoNode*;
131
132 CacheInfoIterator DoRegisterCache(CacheInfo&& info);
133
134 class CacheResetJob;
135
136 void DoResetCaches(
137 cache::UpdateType update_type,
138 const std::unordered_set<std::string>* reset_only_names,
139 const std::unordered_set<std::string>& force_incremental_names,
140 const std::unordered_set<std::string>* exclude_names
141 );
142
143 void DoResetCachesConcurrently(
144 cache::UpdateType update_type,
145 const std::unordered_set<std::string>* reset_only_names,
146 std::unordered_set<std::string>& names_left_to_encounter,
147 const std::unordered_set<std::string>& force_incremental_names,
148 const std::unordered_set<std::string>* exclude_names
149 );
150
151 void UnregisterCache(CacheInfoIterator) noexcept;
152
153 static void DoResetSingleCache(
154 const CacheInfo& info,
155 cache::UpdateType update_type,
156 const std::unordered_set<std::string>& force_incremental_names
157 );
158
159 struct Impl;
160 std::unique_ptr<Impl> impl_;
161};
162
163/// The method for acquiring testsuite::CacheControl in the component system.
164///
165/// @see testsuite::RegisterCacheResetter
166CacheControl& FindCacheControl(const components::ComponentContext& context);
167
168namespace impl {
169
170void DoRegisterCacheScope(const components::ComponentContext& context, std::function<void(cache::UpdateType)> reset);
171
172template <typename Component>
173std::function<void(cache::UpdateType)> BindCacheResetter(Component* self, void (Component::*reset_method)()) {
174 UASSERT(self);
175 UASSERT(reset_method);
176 return [self, reset_method]([[maybe_unused]] cache::UpdateType update_type) { (self->*reset_method)(); };
177}
178
179template <typename Component>
180std::function<void(cache::UpdateType)> BindCacheResetter(
181 Component* self,
182 void (Component::*reset_method)(cache::UpdateType)
183) {
184 UASSERT(self);
185 UASSERT(reset_method);
186 return [self, reset_method](cache::UpdateType update_type) { (self->*reset_method)(update_type); };
187}
188
189} // namespace impl
190
191/// @brief Registers a cache resetter bound to the component lifetime.
192///
193/// The resetter is registered after the component constructor finishes
194/// and is unregistered just before the destructor runs.
195///
196/// Several cache resetters for the same component are invoked sequentially
197/// in registration order.
198///
199/// Uses @ref components::ComponentContext::Scopes for the component automatically.
200/// See @ref resource_scopes_parameter "choosing scopes" for the lifetime rules.
201///
202/// Typical usage:
203/// @code
204/// testsuite::RegisterCacheResetter(context, this, &MyCache::ResetCache);
205/// @endcode
206///
207/// @warning The function should be called in the component's constructor
208/// *after* all FindComponent calls. This ensures that reset will first be
209/// called for dependencies, then for dependent components.
210template <typename Component>
212 const components::ComponentContext& context,
213 Component* self,
214 void (Component::*reset_method)()
215) {
216 impl::DoRegisterCacheScope(context, impl::BindCacheResetter(self, reset_method));
217}
218
219/// @overload The resetter additionally receives the requested
220/// @ref cache::UpdateType.
221///
222/// Use this when the hook must distinguish a full invalidation from an
223/// incremental one. Typical cases:
224/// - a cache that supports both update types but is not periodic, so it is
225/// not a @ref components::CachingComponentBase: values are pushed in,
226/// for example by a handler called from a sidecar;
227/// - an extra resetter on a @ref components::CachingComponentBase cache that
228/// must know the requested update type to adjust incoming data for testsuite.
229template <typename Component>
231 const components::ComponentContext& context,
232 Component* self,
233 void (Component::*reset_method)(cache::UpdateType)
234) {
235 impl::DoRegisterCacheScope(context, impl::BindCacheResetter(self, reset_method));
236}
237
238} // namespace testsuite
239
240USERVER_NAMESPACE_END