-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathservice.cpp
More file actions
118 lines (98 loc) · 3.92 KB
/
service.cpp
File metadata and controls
118 lines (98 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <userver/utest/using_namespace_userver.hpp>
#include <userver/alerts/component.hpp>
#include <userver/alerts/handler.hpp>
#include <userver/cache/caching_component_base.hpp>
#include <userver/components/common_component_list.hpp>
#include <userver/components/common_server_component_list.hpp>
#include <userver/components/component_context.hpp>
#include <userver/dump/common.hpp>
#include <userver/dump/common_containers.hpp>
#include <userver/server/handlers/http_handler_base.hpp>
#include <userver/server/handlers/ping.hpp>
#include <userver/tracing/manager_component.hpp>
#include <userver/utils/daemon_run.hpp>
namespace functional_tests {
using Data = std::unordered_map<int, int>;
class HandlerCacheState;
class AlertCache final : public components::CachingComponentBase<Data> {
public:
static constexpr std::string_view kName = "alert-cache";
AlertCache(const components::ComponentConfig& config,
const components::ComponentContext& context)
: CachingComponentBase(config, context) {
CacheUpdateTrait::StartPeriodicUpdates();
}
~AlertCache() override { CacheUpdateTrait::StopPeriodicUpdates(); }
void Update(cache::UpdateType type,
const std::chrono::system_clock::time_point& /*last_update*/,
const std::chrono::system_clock::time_point& /*now*/,
cache::UpdateStatisticsScope& stats_scope) override {
if (type == cache::UpdateType::kIncremental) {
stats_scope.FinishWithError();
} else {
stats_scope.FinishNoChanges();
}
}
};
class CacheSample final : public components::CachingComponentBase<Data> {
public:
friend HandlerCacheState;
static constexpr std::string_view kName = "sample-cache";
CacheSample(const components::ComponentConfig& config,
const components::ComponentContext& context)
: CachingComponentBase(config, context) {
CacheUpdateTrait::StartPeriodicUpdates();
}
~CacheSample() override { CacheUpdateTrait::StopPeriodicUpdates(); }
void Update(cache::UpdateType /*type*/,
const std::chrono::system_clock::time_point& /*last_update*/,
const std::chrono::system_clock::time_point& /*now*/,
cache::UpdateStatisticsScope& stats_scope) override {
static std::atomic<bool> set_empty{false};
if (set_empty.exchange(true)) {
try {
Set(Data{});
} catch (const cache::EmptyDataError& e) {
LOG_INFO() << "Trying to install the empty cache";
Set(Data{{42, 42}});
}
} else {
Set(Data{{1, 1}});
}
stats_scope.Finish(1);
}
};
class HandlerCacheState final : public server::handlers::HttpHandlerBase {
public:
static constexpr std::string_view kName = "handler-cache-state";
HandlerCacheState(const components::ComponentConfig& config,
const components::ComponentContext& context)
: server::handlers::HttpHandlerBase(config, context),
cache_(context.FindComponent<CacheSample>()) {}
std::string HandleRequestThrow(
const server::http::HttpRequest&,
server::request::RequestContext&) const override {
cache_.UpdateSyncDebug(cache::UpdateType::kIncremental);
cache_.UpdateSyncDebug(cache::UpdateType::kIncremental);
auto data = cache_.Get();
if (*data == Data{{42, 42}}) {
return "Magic numbers";
}
return "Not magic numbers";
};
private:
CacheSample& cache_;
};
} // namespace functional_tests
int main(int argc, const char* const argv[]) {
const auto component_list =
components::ComponentList()
.AppendComponentList(components::CommonComponentList())
.AppendComponentList(components::CommonServerComponentList())
.Append<functional_tests::CacheSample>()
.Append<functional_tests::HandlerCacheState>()
.Append<functional_tests::AlertCache>()
.Append<alerts::Handler>()
.Append<server::handlers::Ping>();
return utils::DaemonMain(argc, argv, component_list);
}