|
| 1 | +#include <algorithm> |
| 2 | +#include <optional> |
| 3 | +#include <ranges> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include <fmt/format.h> |
| 8 | +#include <gtest/gtest.h> |
| 9 | + |
| 10 | +#include <userver/components/component_base.hpp> |
| 11 | +#include <userver/components/component_context.hpp> |
| 12 | +#include <userver/components/component_list.hpp> |
| 13 | +#include <userver/components/run.hpp> |
| 14 | +#include <userver/components/statistics_storage.hpp> |
| 15 | +#include <userver/engine/exception.hpp> |
| 16 | +#include <userver/engine/sleep.hpp> |
| 17 | +#include <userver/engine/task/cancel.hpp> |
| 18 | +#include <userver/fs/blocking/read.hpp> |
| 19 | +#include <userver/fs/blocking/temp_directory.hpp> |
| 20 | +#include <userver/logging/component.hpp> |
| 21 | +#include <userver/logging/log.hpp> |
| 22 | +#include <userver/os_signals/component.hpp> |
| 23 | +#include <userver/utest/log_capture_fixture.hpp> |
| 24 | +#include <userver/utest/utest.hpp> |
| 25 | + |
| 26 | +#include <components/component_list_test.hpp> |
| 27 | + |
| 28 | +USERVER_NAMESPACE_BEGIN |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +// Sleeps in its constructor until the sibling component fails and cancels component loading. |
| 33 | +class SlowStartingComponent final : public components::ComponentBase { |
| 34 | +public: |
| 35 | + static constexpr std::string_view kName = "slow-component"; |
| 36 | + |
| 37 | + SlowStartingComponent(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 38 | + : ComponentBase(config, context) |
| 39 | + { |
| 40 | + engine::InterruptibleSleepFor(utest::kMaxTestWaitTime); |
| 41 | + if (engine::current_task::ShouldCancel()) { |
| 42 | + throw engine::WaitInterruptedException(engine::current_task::CancellationReason()); |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +// Yields several times so the slow component starts waiting, then throws. |
| 48 | +class FailingComponent final : public components::ComponentBase { |
| 49 | +public: |
| 50 | + static constexpr std::string_view kName = "failing-component"; |
| 51 | + |
| 52 | + FailingComponent(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 53 | + : ComponentBase(config, context) |
| 54 | + { |
| 55 | + for (int i = 0; i < 10; ++i) { |
| 56 | + engine::Yield(); |
| 57 | + } |
| 58 | + throw std::runtime_error("FailingComponent constructor intentionally throws"); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +constexpr std::string_view kCannotStartComponentLogTextPrefix = "Cannot start component"; |
| 63 | +constexpr std::string_view kOnLoadingCancelledLogTextPrefix = "Call OnLoadingCancelled() for component"; |
| 64 | + |
| 65 | +std::size_t CountCannotStartComponentLogs( |
| 66 | + const std::vector<utest::LogRecord>& records, |
| 67 | + std::optional<std::string_view> level = std::nullopt, |
| 68 | + std::optional<std::string_view> component_name = std::nullopt |
| 69 | +) { |
| 70 | + return std::ranges::count_if(records, [&](const utest::LogRecord& record) { |
| 71 | + return record.GetText().starts_with(kCannotStartComponentLogTextPrefix) && |
| 72 | + (!level || record.GetTagOptional("level") == *level) && |
| 73 | + (!component_name || record.GetTagOptional("component_name") == *component_name); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +std::size_t CountOnLoadingCancelledLogs( |
| 78 | + const std::vector<utest::LogRecord>& records, |
| 79 | + std::optional<std::string_view> level = std::nullopt, |
| 80 | + std::optional<std::string_view> component_name = std::nullopt |
| 81 | +) { |
| 82 | + return std::ranges::count_if(records, [&](const utest::LogRecord& record) { |
| 83 | + return record.GetText().starts_with(kOnLoadingCancelledLogTextPrefix) && |
| 84 | + !record.GetTagOptional("component_name") && (!level || record.GetTagOptional("level") == *level) && |
| 85 | + (!component_name || |
| 86 | + record.GetText() == fmt::format("Call OnLoadingCancelled() for component '{}'", *component_name)); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace |
| 91 | + |
| 92 | +using ComponentStartFailureLog = ComponentList; |
| 93 | + |
| 94 | +TEST_F(ComponentStartFailureLog, RootCauseAndCancelledComponentLogs) { |
| 95 | + const auto temp_root = fs::blocking::TempDirectory::Create(); |
| 96 | + const std::string logs_path = temp_root.GetPath() + "/log.txt"; |
| 97 | + |
| 98 | + const std::string config = tests::MergeYaml( |
| 99 | + tests::kMinimalStaticConfig, |
| 100 | + fmt::format( |
| 101 | + R"( |
| 102 | + components_manager: |
| 103 | + components: |
| 104 | + slow-component: {{}} |
| 105 | + failing-component: {{}} |
| 106 | + statistics-storage: {{}} |
| 107 | + logging: |
| 108 | + loggers: |
| 109 | + default: |
| 110 | + file_path: {} |
| 111 | + format: tskv |
| 112 | + level: debug |
| 113 | + )", |
| 114 | + logs_path |
| 115 | + ) |
| 116 | + ); |
| 117 | + |
| 118 | + auto component_list = |
| 119 | + components::ComponentList() |
| 120 | + .Append<os_signals::ProcessorComponent>() |
| 121 | + .Append<components::StatisticsStorage>() |
| 122 | + .Append<components::Logging>() |
| 123 | + .Append<SlowStartingComponent>() |
| 124 | + .Append<FailingComponent>(); |
| 125 | + |
| 126 | + UEXPECT_THROW_MSG( |
| 127 | + components::RunOnce(components::InMemoryConfig{config}, component_list), |
| 128 | + std::runtime_error, |
| 129 | + "FailingComponent constructor intentionally throws" |
| 130 | + ); |
| 131 | + |
| 132 | + logging::LogFlush(); |
| 133 | + |
| 134 | + const auto records = utest::ParseTskvLogRecords(fs::blocking::ReadFileContents(logs_path)); |
| 135 | + |
| 136 | + ASSERT_EQ(CountCannotStartComponentLogs(records, "ERROR", FailingComponent::kName), 1) << records; |
| 137 | + ASSERT_EQ(CountCannotStartComponentLogs(records, "WARNING", SlowStartingComponent::kName), 1) << records; |
| 138 | + ASSERT_EQ(CountCannotStartComponentLogs(records), 2) << records; |
| 139 | + |
| 140 | + ASSERT_EQ(CountOnLoadingCancelledLogs(records, "DEBUG", SlowStartingComponent::kName), 1) << records; |
| 141 | + ASSERT_EQ(CountOnLoadingCancelledLogs(records, "DEBUG", FailingComponent::kName), 1) << records; |
| 142 | + ASSERT_EQ(CountOnLoadingCancelledLogs(records, "DEBUG"), std::ranges::distance(component_list)) << records; |
| 143 | +} |
| 144 | + |
| 145 | +USERVER_NAMESPACE_END |
0 commit comments