userver: testsuite::CacheControl Class Reference
Loading...
Searching...
No Matches
testsuite::CacheControl Class Referencefinal

#include <userver/testsuite/cache_control.hpp>

Detailed Description

Testsuite interface for caches and cache-like components.

If a component stores transient state that may be carried between tests, or stores caches that may become stale, then it should register its resetter here. Example:

class MyCache final : public components::LoggableComponentBase {
public:
static constexpr std::string_view kName = "my-cache";
MyCache(const components::ComponentConfig& config,
: components::LoggableComponentBase(config, context) {
{
auto cache = cached_token_.Lock();
*cache = FetchToken();
}
// ...
// reset_registration_ must be set at the end of the constructor.
reset_registration_ = testsuite::FindCacheControl(context).RegisterCache(
this, kName, &MyCache::ResetCache);
}
std::string GetToken() {
auto cache = cached_token_.Lock();
if (auto& opt_token = *cache; opt_token) return *opt_token;
auto new_token = FetchToken();
*cache = new_token;
return new_token;
}
void ReportServiceRejectedToken() { ResetCache(); }
private:
std::string FetchToken() const;
void ResetCache() {
auto cache = cached_token_.Lock();
cache->reset();
}
// Subscriptions must be the last fields.
testsuite::CacheResetRegistration reset_registration_;
};

Testsuite will then call this hook in the beginning of each test. You can also reset a specific cache in testsuite explicitly as follows:

service_client.invalidate_caches(names=['your-cache-name'])

CacheControl is normally acquired through testsuite::FindCacheControl.

All methods are coro-safe.

Definition at line 59 of file cache_control.hpp.

Public Member Functions

template<typename Component >
CacheResetRegistration RegisterCache (Component *self, std::string_view name, void(Component::*reset_method)())
 Register a cache reset function. The returned handle must be kept alive to keep supporting cache resetting.
 
void ResetAllCaches (cache::UpdateType update_type, const std::unordered_set< std::string > &force_incremental_names)
 Reset all the registered caches.
 
void ResetCaches (cache::UpdateType update_type, std::unordered_set< std::string > reset_only_names, const std::unordered_set< std::string > &force_incremental_names)
 Reset caches with the specified names.
 

Member Function Documentation

◆ RegisterCache()

template<typename Component >
CacheResetRegistration testsuite::CacheControl::RegisterCache ( Component * self,
std::string_view name,
void(Component::*)() reset_method )

Register a cache reset function. The returned handle must be kept alive to keep supporting cache resetting.

Warning
The function should be called in the component's constructor after all FindComponent calls. This ensures that reset will first be called for dependencies, then for dependent components.

Definition at line 155 of file cache_control.hpp.

◆ ResetAllCaches()

void testsuite::CacheControl::ResetAllCaches ( cache::UpdateType update_type,
const std::unordered_set< std::string > & force_incremental_names )

Reset all the registered caches.

update_type is used by caches derived from component::CachingComponentBase.

◆ ResetCaches()

void testsuite::CacheControl::ResetCaches ( cache::UpdateType update_type,
std::unordered_set< std::string > reset_only_names,
const std::unordered_set< std::string > & force_incremental_names )

Reset caches with the specified names.

update_type is used by caches derived from component::CachingComponentBase.


The documentation for this class was generated from the following file: