|
| 1 | +#include <userver/utest/utest.hpp> |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | + |
| 5 | +#include <components/component_list_test.hpp> |
| 6 | +#include <userver/cache/caching_component_base.hpp> |
| 7 | +#include <userver/components/component_base.hpp> |
| 8 | +#include <userver/components/component_context.hpp> |
| 9 | +#include <userver/components/minimal_component_list.hpp> |
| 10 | +#include <userver/components/run.hpp> |
| 11 | +#include <userver/components/statistics_storage.hpp> |
| 12 | +#include <userver/engine/deadline.hpp> |
| 13 | +#include <userver/engine/sleep.hpp> |
| 14 | +#include <userver/testsuite/testsuite_support.hpp> |
| 15 | +#include <userver/utest/utest.hpp> |
| 16 | +#include <userver/utils/statistics/testing.hpp> |
| 17 | + |
| 18 | +USERVER_NAMESPACE_BEGIN |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +const auto kStaticConfig = tests::MergeYaml(tests::kMinimalStaticConfig, R"( |
| 23 | +components_manager: |
| 24 | + components: |
| 25 | + testsuite-support: |
| 26 | + toy-cache: |
| 27 | + update-types: only-full |
| 28 | + update-interval: 1h |
| 29 | + metrics-checker: |
| 30 | +)"); |
| 31 | + |
| 32 | +class ToyCache final : public components::CachingComponentBase<int> { |
| 33 | +public: |
| 34 | + [[maybe_unused]] static constexpr std::string_view kName = "toy-cache"; |
| 35 | + |
| 36 | + ToyCache(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 37 | + : CachingComponentBase(config, context) |
| 38 | + { |
| 39 | + StartPeriodicUpdates(); |
| 40 | + } |
| 41 | + |
| 42 | + ~ToyCache() override { StopPeriodicUpdates(); } |
| 43 | + |
| 44 | + void Update( |
| 45 | + cache::UpdateType /*type*/, |
| 46 | + const std::chrono::system_clock::time_point& /*last_update*/, |
| 47 | + const std::chrono::system_clock::time_point& /*now*/, |
| 48 | + cache::UpdateStatisticsScope& stats_scope |
| 49 | + ) override { |
| 50 | + // Simulate a 3-second update. |
| 51 | + engine::SleepFor(std::chrono::seconds(3)); |
| 52 | + |
| 53 | + // Complete successfully with size 10. |
| 54 | + Set(10); |
| 55 | + stats_scope.Finish(10); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +class MetricsChecker final : public components::ComponentBase { |
| 60 | +public: |
| 61 | + [[maybe_unused]] static constexpr std::string_view kName = "metrics-checker"; |
| 62 | + |
| 63 | + MetricsChecker(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 64 | + : ComponentBase(config, context) |
| 65 | + { |
| 66 | + auto& stats_storage = context.FindComponent<components::StatisticsStorage>(); |
| 67 | + const auto& storage = stats_storage.GetStorage(); |
| 68 | + |
| 69 | + bool cache_age_seen = false; |
| 70 | + bool cache_size_seen = false; |
| 71 | + |
| 72 | + const auto deadline = engine::Deadline::FromDuration(utest::kMaxTestWaitTime); |
| 73 | + |
| 74 | + // Check metrics periodically every 10 milliseconds. |
| 75 | + while (!deadline.IsReached()) { |
| 76 | + engine::SleepFor(std::chrono::milliseconds(10)); |
| 77 | + |
| 78 | + const utils::statistics::Snapshot snapshot{storage}; |
| 79 | + |
| 80 | + // Check cache age metric. |
| 81 | + const auto age_metric = snapshot.SingleMetricOptional( |
| 82 | + "cache.any.time.time-from-last-update-start-ms", |
| 83 | + {{"cache_name", "toy-cache"}} |
| 84 | + ); |
| 85 | + if (age_metric) { |
| 86 | + const auto age_ms = age_metric->AsInt(); |
| 87 | + EXPECT_GE(age_ms, 0) << "Cache age should be non-negative"; |
| 88 | + EXPECT_LT(age_ms, 10000) << "Cache age should be less than 10 seconds"; |
| 89 | + cache_age_seen = true; |
| 90 | + } |
| 91 | + |
| 92 | + // Check cache size metric. |
| 93 | + const auto size_metric = |
| 94 | + snapshot.SingleMetricOptional("cache.current-documents-count", {{"cache_name", "toy-cache"}}); |
| 95 | + if (size_metric) { |
| 96 | + const auto size = size_metric->AsInt(); |
| 97 | + EXPECT_EQ(size, 10) << "Cache size should be 10"; |
| 98 | + cache_size_seen = true; |
| 99 | + } |
| 100 | + |
| 101 | + // If both metrics are set, we're done. |
| 102 | + if (cache_age_seen && cache_size_seen) { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Verify that both metrics were eventually seen. |
| 108 | + EXPECT_TRUE(cache_age_seen) << "Cache age metric should be written after initialization"; |
| 109 | + EXPECT_TRUE(cache_size_seen) << "Cache size metric should be written after initialization"; |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +class CacheMetricsInitializationTest : public ComponentList {}; |
| 114 | + |
| 115 | +} // namespace |
| 116 | + |
| 117 | +TEST_F(CacheMetricsInitializationTest, MetricsNotWrittenBeforeInitialization) { |
| 118 | + components::RunOnce( |
| 119 | + components::InMemoryConfig{kStaticConfig}, |
| 120 | + components::MinimalComponentList() |
| 121 | + .Append<components::TestsuiteSupport>() |
| 122 | + .Append<ToyCache>() |
| 123 | + .Append<MetricsChecker>() |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +USERVER_NAMESPACE_END |
0 commit comments