userver: userver/testsuite/component_control.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
component_control.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/testsuite/component_control.hpp
4/// @brief @copybrief testsuite::ComponentControl
5
6#include <functional>
7#include <list>
8#include <type_traits>
9
10#include <userver/components/loggable_component_base.hpp>
11#include <userver/engine/mutex.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace testsuite {
16
17/// @brief Component control interface for testsuite
18/// @details All methods are coro-safe.
19class ComponentControl final {
20 public:
21 void InvalidateComponents();
22
23 private:
24 friend class ComponentInvalidatorHolder;
25
26 using Callback = std::function<void()>;
27 using InvalidatorList = std::list<Callback>;
28 using InvalidatorHandle = InvalidatorList::iterator;
29
30 InvalidatorHandle RegisterComponentInvalidator(Callback&& callback);
31
32 void UnregisterComponentInvalidator(InvalidatorHandle handle) noexcept;
33
34 engine::Mutex mutex_;
35 InvalidatorList invalidators_;
36};
37
38/// RAII helper for testsuite registration
39class ComponentInvalidatorHolder final {
40 public:
41 template <class T>
42 ComponentInvalidatorHolder(ComponentControl& component_control, T& component,
43 void (T::*invalidate_method)())
44 : ComponentInvalidatorHolder(component_control,
45 [&component, invalidate_method] {
46 (component.*invalidate_method)();
47 }) {
48 static_assert(
49 std::is_base_of_v<components::LoggableComponentBase, T>,
50 "ComponentInvalidatorHolder can only be used with components");
51 }
52
53 ~ComponentInvalidatorHolder();
54
55 ComponentInvalidatorHolder(const ComponentInvalidatorHolder&) = delete;
56 ComponentInvalidatorHolder(ComponentInvalidatorHolder&&) = delete;
57 ComponentInvalidatorHolder& operator=(const ComponentInvalidatorHolder&) =
58 delete;
59 ComponentInvalidatorHolder& operator=(ComponentInvalidatorHolder&&) = delete;
60
61 private:
62 ComponentInvalidatorHolder(ComponentControl& component_control,
63 std::function<void()>&& callback);
64
65 ComponentControl& component_control_;
66 ComponentControl::InvalidatorHandle invalidator_handle_;
67};
68
69} // namespace testsuite
70
71USERVER_NAMESPACE_END