|
| 1 | +#include <userver/components/component_context.hpp> |
| 2 | +#include <userver/components/minimal_server_component_list.hpp> |
| 3 | +#include <userver/engine/sleep.hpp> |
| 4 | +#include <userver/server/handlers/ping.hpp> |
| 5 | +#include <userver/utest/using_namespace_userver.hpp> |
| 6 | +#include <userver/utils/daemon_run.hpp> |
| 7 | + |
| 8 | +class MonitorHandler final : public server::handlers::HttpHandlerBase { |
| 9 | +public: |
| 10 | + MonitorHandler(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 11 | + : server::handlers::HttpHandlerBase(config, context, true) {} |
| 12 | + |
| 13 | + static constexpr std::string_view kName = "handler-monitor"; |
| 14 | + |
| 15 | + std::string HandleRequestThrow(const server::http::HttpRequest&, server::request::RequestContext&) const override { |
| 16 | + called_ = true; |
| 17 | + return {}; |
| 18 | + } |
| 19 | + |
| 20 | + bool WasCalled() const { return called_; } |
| 21 | + |
| 22 | +private: |
| 23 | + mutable std::atomic<bool> called_{false}; |
| 24 | +}; |
| 25 | + |
| 26 | +class Brake final : public components::ComponentBase { |
| 27 | +public: |
| 28 | + Brake(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 29 | + : components::ComponentBase(config, context) { |
| 30 | + auto& monitor_handler = context.FindComponent<MonitorHandler>(); |
| 31 | + |
| 32 | + for (;;) { |
| 33 | + engine::SleepFor(std::chrono::milliseconds(100)); |
| 34 | + |
| 35 | + if (monitor_handler.WasCalled()) { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static constexpr std::string_view kName = "brake"; |
| 42 | + |
| 43 | +private: |
| 44 | +}; |
| 45 | + |
| 46 | +template <> |
| 47 | +inline constexpr bool components::kHasValidate<Brake> = false; |
| 48 | + |
| 49 | +template <> |
| 50 | +inline constexpr auto components::kConfigFileMode<Brake> = ConfigFileMode::kNotRequired; |
| 51 | + |
| 52 | +int main(int argc, char* argv[]) { |
| 53 | + const auto component_list = |
| 54 | + components::MinimalServerComponentList() |
| 55 | + .Append<server::handlers::Ping>() |
| 56 | + .Append<Brake>() |
| 57 | + .Append<MonitorHandler>(); |
| 58 | + return utils::DaemonMain(argc, argv, component_list); |
| 59 | +} |
0 commit comments