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 <type_traits>
10#include <unordered_set>
11
12#include <userver/cache/update_type.hpp>
13#include <userver/components/component_fwd.hpp>
14#include <userver/components/state.hpp>
15#include <userver/utils/assert.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace cache {
21struct Config;
22} // namespace cache
23
24namespace components {
26} // namespace components
27
28namespace testsuite {
29
30namespace impl {
31enum class PeriodicUpdatesMode { kDefault, kEnabled, kDisabled };
32} // namespace impl
33
34class CacheResetRegistration;
35
36/// @brief Testsuite interface for caches and cache-like components.
37///
38/// If a component stores transient state that may be carried between tests,
39/// or stores caches that may become stale, then it should register its resetter
40/// here. Example:
41///
42/// @snippet testsuite/cache_control_test.cpp sample
43///
44/// Testsuite will then call this hook in the beginning of each test.
45/// You can also reset a specific cache in testsuite explicitly as follows:
46///
47/// @code
48/// service_client.invalidate_caches(names=['your-cache-name'])
49/// @endcode
50///
51/// CacheControl is normally acquired through testsuite::FindCacheControl.
52///
53/// All methods are coro-safe.
54class CacheControl final {
55 public:
56 /// @brief Reset all the registered caches.
57 ///
58 /// @a update_type is used by caches derived from
59 /// @a component::CachingComponentBase.
61 cache::UpdateType update_type,
62 const std::unordered_set<std::string>& force_incremental_names,
63 const std::unordered_set<std::string>& exclude_names);
64
65 /// @brief Reset caches with the specified @a names.
66 ///
67 /// @a update_type is used by caches derived from
68 /// @a component::CachingComponentBase.
70 cache::UpdateType update_type,
71 std::unordered_set<std::string> reset_only_names,
72 const std::unordered_set<std::string>& force_incremental_names);
73
74 CacheControl(CacheControl&&) = delete;
75 CacheControl& operator=(CacheControl&&) = delete;
76
77 /// @cond
78 // For internal use only.
79 struct UnitTests {
80 explicit UnitTests() = default;
81 };
82
83 enum class ExecPolicy {
84 kSequential,
85 kConcurrent,
86 };
87
88 CacheControl(impl::PeriodicUpdatesMode, UnitTests);
89 CacheControl(impl::PeriodicUpdatesMode, ExecPolicy, components::State);
90 ~CacheControl();
91
92 // For internal use only.
93 bool IsPeriodicUpdateEnabled(const cache::Config& cache_config,
94 const std::string& cache_name) const;
95
96 // For internal use only.
97 CacheResetRegistration RegisterPeriodicCache(cache::CacheUpdateTrait& cache);
98
99 // For internal use only. Use testsuite::RegisterCache instead
100 template <typename Component>
101 CacheResetRegistration RegisterCache(Component* self, std::string_view name,
102 void (Component::*reset_method)());
103
104 struct CacheInfo final {
105 std::string name;
106 std::function<void(cache::UpdateType)> reset;
107 bool needs_span{true};
108 };
109 struct CacheInfoNode;
110 using CacheInfoIterator = CacheInfoNode*;
111
112 // For internal use only.
113 CacheInfoIterator DoRegisterCache(CacheInfo&& info);
114 /// @endcond
115 private:
116 friend class CacheResetRegistration;
117
118 class CacheResetJob;
119
120 void DoResetCaches(
121 cache::UpdateType update_type,
122 std::unordered_set<std::string>* reset_only_names,
123 const std::unordered_set<std::string>& force_incremental_names,
124 const std::unordered_set<std::string>* exclude_names);
125
126 void DoResetCachesConcurrently(
127 cache::UpdateType update_type,
128 std::unordered_set<std::string>* reset_only_names,
129 const std::unordered_set<std::string>& force_incremental_names,
130 const std::unordered_set<std::string>* exclude_names);
131
132 void UnregisterCache(CacheInfoIterator) noexcept;
133
134 static void DoResetSingleCache(
135 const CacheInfo& info, cache::UpdateType update_type,
136 const std::unordered_set<std::string>& force_incremental_names);
137
138 struct Impl;
139 std::unique_ptr<Impl> impl_;
140};
141
142/// @brief RAII helper for testsuite registration. Must be kept alive to keep
143/// supporting cache resetting.
144/// @warning Make sure to always place CacheResetRegistration after the rest of
145/// the component's fields.
146/// @see testsuite::CacheControl
147class [[nodiscard]] CacheResetRegistration final {
148 public:
149 CacheResetRegistration() noexcept;
150
151 CacheResetRegistration(CacheResetRegistration&&) noexcept;
152 CacheResetRegistration& operator=(CacheResetRegistration&&) noexcept;
153 ~CacheResetRegistration();
154
155 /// Unregister the cache component explicitly.
156 /// `Unregister` is called in the destructor automatically.
157 void Unregister() noexcept;
158
159 /// @cond
160 // For internal use only.
161 CacheResetRegistration(CacheControl&, CacheControl::CacheInfoIterator);
162 /// @endcond
163
164 private:
165 CacheControl* cache_control_{nullptr};
166 CacheControl::CacheInfoIterator cache_info_iterator_{};
167};
168
169/// The method for acquiring testsuite::CacheControl in the component system.
170///
171/// @see testsuite::RegisterCache
172CacheControl& FindCacheControl(const components::ComponentContext& context);
173
174/// @brief The method for registering a cache from component constructor. The
175/// returned handle must be kept alive to keep supporting cache resetting.
176///
177/// @warning The function should be called in the component's constructor
178/// *after* all FindComponent calls. This ensures that reset will first be
179/// called for dependencies, then for dependent components.
180template <typename Component>
181CacheResetRegistration RegisterCache(
182 const components::ComponentConfig& config,
183 const components::ComponentContext& context, Component* self,
184 void (Component::*reset_method)()) {
185 auto& cc = testsuite::FindCacheControl(context);
186 return cc.RegisterCache(self, components::GetCurrentComponentName(config),
187 reset_method);
188}
189
190template <typename Component>
191CacheResetRegistration CacheControl::RegisterCache(
192 Component* self, std::string_view name, void (Component::*reset_method)()) {
193 static_assert(std::is_base_of_v<components::RawComponentBase, Component>,
194 "CacheControl can only be used with components");
195 UASSERT(self);
196 UASSERT(reset_method);
197
198 CacheInfo info;
199 info.name = std::string{name};
200 info.reset = [self, reset_method]([[maybe_unused]] cache::UpdateType) {
201 (self->*reset_method)();
202 };
203 info.needs_span = true;
204
205 auto iter = DoRegisterCache(std::move(info));
206 return CacheResetRegistration(*this, std::move(iter));
207}
208
209} // namespace testsuite
210
211USERVER_NAMESPACE_END