/proc/self/cwd/test/mocks/stats/mocks.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <chrono> |
4 | | #include <cstdint> |
5 | | #include <functional> |
6 | | #include <list> |
7 | | #include <string> |
8 | | |
9 | | #include "envoy/stats/histogram.h" |
10 | | #include "envoy/stats/sink.h" |
11 | | #include "envoy/stats/stats.h" |
12 | | #include "envoy/stats/stats_matcher.h" |
13 | | #include "envoy/stats/store.h" |
14 | | #include "envoy/stats/timespan.h" |
15 | | #include "envoy/thread_local/thread_local.h" |
16 | | #include "envoy/upstream/cluster_manager.h" |
17 | | |
18 | | #include "source/common/stats/histogram_impl.h" |
19 | | #include "source/common/stats/isolated_store_impl.h" |
20 | | #include "source/common/stats/symbol_table.h" |
21 | | #include "source/common/stats/timespan_impl.h" |
22 | | |
23 | | #include "test/common/stats/stat_test_utility.h" |
24 | | |
25 | | #include "gmock/gmock.h" |
26 | | |
27 | | namespace Envoy { |
28 | | namespace Stats { |
29 | | |
30 | | template <class BaseClass> class MockMetric : public BaseClass { |
31 | | public: |
32 | 854k | MockMetric() : name_(*this), tag_pool_(*symbol_table_) {} Envoy::Stats::MockMetric<Envoy::Stats::Counter>::MockMetric() Line | Count | Source | 32 | 427k | MockMetric() : name_(*this), tag_pool_(*symbol_table_) {} |
Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::MockMetric() Line | Count | Source | 32 | 427k | MockMetric() : name_(*this), tag_pool_(*symbol_table_) {} |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::MockMetric() Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::MockMetric() Line | Count | Source | 32 | 1 | MockMetric() : name_(*this), tag_pool_(*symbol_table_) {} |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::MockMetric() |
33 | 853k | ~MockMetric() override = default; Envoy::Stats::MockMetric<Envoy::Stats::Counter>::~MockMetric() Line | Count | Source | 33 | 426k | ~MockMetric() override = default; |
Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::~MockMetric() Line | Count | Source | 33 | 426k | ~MockMetric() override = default; |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::~MockMetric() Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::~MockMetric() Line | Count | Source | 33 | 1 | ~MockMetric() override = default; |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::~MockMetric() |
34 | | |
35 | | // This bit of C++ subterfuge allows us to support the wealth of tests that |
36 | | // do metric->name_ = "foo" even though names are more complex now. Note |
37 | | // that the statName is only populated if there is a symbol table. |
38 | | class MetricName { |
39 | | public: |
40 | 854k | explicit MetricName(MockMetric& mock_metric) : mock_metric_(mock_metric) {} Envoy::Stats::MockMetric<Envoy::Stats::Counter>::MetricName::MetricName(Envoy::Stats::MockMetric<Envoy::Stats::Counter>&) Line | Count | Source | 40 | 427k | explicit MetricName(MockMetric& mock_metric) : mock_metric_(mock_metric) {} |
Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::MetricName::MetricName(Envoy::Stats::MockMetric<Envoy::Stats::Gauge>&) Line | Count | Source | 40 | 427k | explicit MetricName(MockMetric& mock_metric) : mock_metric_(mock_metric) {} |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::MetricName::MetricName(Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>&) Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::MetricName::MetricName(Envoy::Stats::MockMetric<Envoy::Stats::Histogram>&) Line | Count | Source | 40 | 1 | explicit MetricName(MockMetric& mock_metric) : mock_metric_(mock_metric) {} |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::MetricName::MetricName(Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>&) |
41 | 853k | ~MetricName() { |
42 | 853k | if (stat_name_storage_ != nullptr) { |
43 | 0 | stat_name_storage_->free(mock_metric_.symbolTable()); |
44 | 0 | } |
45 | 853k | } Envoy::Stats::MockMetric<Envoy::Stats::Counter>::MetricName::~MetricName() Line | Count | Source | 41 | 426k | ~MetricName() { | 42 | 426k | if (stat_name_storage_ != nullptr) { | 43 | 0 | stat_name_storage_->free(mock_metric_.symbolTable()); | 44 | 0 | } | 45 | 426k | } |
Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::MetricName::~MetricName() Line | Count | Source | 41 | 426k | ~MetricName() { | 42 | 426k | if (stat_name_storage_ != nullptr) { | 43 | 0 | stat_name_storage_->free(mock_metric_.symbolTable()); | 44 | 0 | } | 45 | 426k | } |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::MetricName::~MetricName() Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::MetricName::~MetricName() Line | Count | Source | 41 | 1 | ~MetricName() { | 42 | 1 | if (stat_name_storage_ != nullptr) { | 43 | 0 | stat_name_storage_->free(mock_metric_.symbolTable()); | 44 | 0 | } | 45 | 1 | } |
Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::MetricName::~MetricName() |
46 | | |
47 | 0 | void operator=(absl::string_view str) { |
48 | 0 | name_ = std::string(str); |
49 | 0 | stat_name_storage_ = std::make_unique<StatNameStorage>(str, mock_metric_.symbolTable()); |
50 | 0 | } |
51 | | |
52 | 0 | std::string name() const { return name_; } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::MetricName::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::MetricName::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::MetricName::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::MetricName::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::MetricName::name() const |
53 | 0 | StatName statName() const { return stat_name_storage_->statName(); } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::MetricName::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::MetricName::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::MetricName::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::MetricName::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::MetricName::statName() const |
54 | | |
55 | | private: |
56 | | MockMetric& mock_metric_; |
57 | | std::string name_; |
58 | | std::unique_ptr<StatNameStorage> stat_name_storage_; |
59 | | }; |
60 | | |
61 | 0 | SymbolTable& symbolTable() override { return *symbol_table_; } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::symbolTable() Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::symbolTable() Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::symbolTable() Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::symbolTable() Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::symbolTable() |
62 | 0 | const SymbolTable& constSymbolTable() const override { return *symbol_table_; } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::constSymbolTable() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::constSymbolTable() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::constSymbolTable() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::constSymbolTable() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::constSymbolTable() const |
63 | | |
64 | | // Note: cannot be mocked because it is accessed as a Property in a gmock EXPECT_CALL. This |
65 | | // creates a deadlock in gmock and is an unintended use of mock functions. |
66 | 0 | std::string name() const override { return name_.name(); } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::name() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::name() const |
67 | 0 | StatName statName() const override { return name_.statName(); } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::statName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::statName() const |
68 | 0 | TagVector tags() const override { return tags_; } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::tags() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::tags() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::tags() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::tags() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::tags() const |
69 | | void setTagExtractedName(absl::string_view name) { |
70 | | tag_extracted_name_ = std::string(name); |
71 | | tag_extracted_stat_name_ = |
72 | | std::make_unique<StatNameManagedStorage>(tagExtractedName(), *symbol_table_); |
73 | | } |
74 | 0 | std::string tagExtractedName() const override { |
75 | 0 | return tag_extracted_name_.empty() ? name() : tag_extracted_name_; |
76 | 0 | } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::tagExtractedName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::tagExtractedName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::tagExtractedName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::tagExtractedName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::tagExtractedName() const |
77 | 0 | StatName tagExtractedStatName() const override { return tag_extracted_stat_name_->statName(); } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::tagExtractedStatName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::tagExtractedStatName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::tagExtractedStatName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::tagExtractedStatName() const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::tagExtractedStatName() const |
78 | 0 | void iterateTagStatNames(const Metric::TagStatNameIterFn& fn) const override { |
79 | 0 | ASSERT((tag_names_and_values_.size() % 2) == 0); |
80 | 0 | for (size_t i = 0; i < tag_names_and_values_.size(); i += 2) { |
81 | 0 | if (!fn(tag_names_and_values_[i], tag_names_and_values_[i + 1])) { |
82 | 0 | return; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Counter>::iterateTagStatNames(std::__1::function<bool (Envoy::Stats::StatName, Envoy::Stats::StatName)> const&) const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Gauge>::iterateTagStatNames(std::__1::function<bool (Envoy::Stats::StatName, Envoy::Stats::StatName)> const&) const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::TextReadout>::iterateTagStatNames(std::__1::function<bool (Envoy::Stats::StatName, Envoy::Stats::StatName)> const&) const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::Histogram>::iterateTagStatNames(std::__1::function<bool (Envoy::Stats::StatName, Envoy::Stats::StatName)> const&) const Unexecuted instantiation: Envoy::Stats::MockMetric<Envoy::Stats::ParentHistogram>::iterateTagStatNames(std::__1::function<bool (Envoy::Stats::StatName, Envoy::Stats::StatName)> const&) const |
86 | | |
87 | | TestUtil::TestSymbolTable symbol_table_; // Must outlive name_. |
88 | | MetricName name_; |
89 | | |
90 | | void setTags(const TagVector& tags) { |
91 | | tag_pool_.clear(); |
92 | | tag_names_and_values_.clear(); |
93 | | tags_ = tags; |
94 | | for (const Tag& tag : tags) { |
95 | | tag_names_and_values_.push_back(tag_pool_.add(tag.name_)); |
96 | | tag_names_and_values_.push_back(tag_pool_.add(tag.value_)); |
97 | | } |
98 | | } |
99 | | |
100 | | void setTags(const Stats::StatNameTagVector& tags) { |
101 | | tag_pool_.clear(); |
102 | | tag_names_and_values_.clear(); |
103 | | tags_.clear(); |
104 | | for (const StatNameTag& tag : tags) { |
105 | | tag_names_and_values_.push_back(tag.first); |
106 | | tag_names_and_values_.push_back(tag.second); |
107 | | tags_.push_back(Tag{symbol_table_->toString(tag.first), symbol_table_->toString(tag.second)}); |
108 | | } |
109 | | } |
110 | | |
111 | | void addTag(const Tag& tag) { |
112 | | tags_.emplace_back(tag); |
113 | | tag_names_and_values_.push_back(tag_pool_.add(tag.name_)); |
114 | | tag_names_and_values_.push_back(tag_pool_.add(tag.value_)); |
115 | | } |
116 | | |
117 | | private: |
118 | | TagVector tags_; |
119 | | StatNameVec tag_names_and_values_; |
120 | | std::string tag_extracted_name_; |
121 | | StatNamePool tag_pool_; |
122 | | std::unique_ptr<StatNameManagedStorage> tag_extracted_stat_name_; |
123 | | }; |
124 | | |
125 | | template <class BaseClass> class MockStatWithRefcount : public MockMetric<BaseClass> { |
126 | | public: |
127 | | // RefcountInterface |
128 | 0 | void incRefCount() override { refcount_helper_.incRefCount(); } Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Counter>::incRefCount() Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Gauge>::incRefCount() |
129 | 0 | bool decRefCount() override { return refcount_helper_.decRefCount(); } Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Counter>::decRefCount() Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Gauge>::decRefCount() |
130 | 0 | uint32_t use_count() const override { return refcount_helper_.use_count(); } Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Counter>::use_count() const Unexecuted instantiation: Envoy::Stats::MockStatWithRefcount<Envoy::Stats::Gauge>::use_count() const |
131 | | |
132 | | RefcountHelper refcount_helper_; |
133 | | }; |
134 | | |
135 | | class MockCounter : public MockStatWithRefcount<Counter> { |
136 | | public: |
137 | | MockCounter(); |
138 | | ~MockCounter() override; |
139 | | |
140 | | MOCK_METHOD(void, add, (uint64_t amount)); |
141 | | MOCK_METHOD(void, inc, ()); |
142 | | MOCK_METHOD(uint64_t, latch, ()); |
143 | | MOCK_METHOD(void, reset, ()); |
144 | | MOCK_METHOD(bool, used, (), (const)); |
145 | | MOCK_METHOD(bool, hidden, (), (const)); |
146 | | MOCK_METHOD(uint64_t, value, (), (const)); |
147 | | |
148 | | bool used_; |
149 | | bool hidden_; |
150 | | uint64_t value_; |
151 | | uint64_t latch_; |
152 | | }; |
153 | | |
154 | | class MockGauge : public MockStatWithRefcount<Gauge> { |
155 | | public: |
156 | | MockGauge(); |
157 | | ~MockGauge() override; |
158 | | |
159 | | MOCK_METHOD(void, add, (uint64_t amount)); |
160 | | MOCK_METHOD(void, dec, ()); |
161 | | MOCK_METHOD(void, inc, ()); |
162 | | MOCK_METHOD(void, set, (uint64_t value)); |
163 | | MOCK_METHOD(void, setParentValue, (uint64_t parent_value)); |
164 | | MOCK_METHOD(void, sub, (uint64_t amount)); |
165 | | MOCK_METHOD(void, mergeImportMode, (ImportMode)); |
166 | | MOCK_METHOD(bool, used, (), (const)); |
167 | | MOCK_METHOD(bool, hidden, (), (const)); |
168 | | MOCK_METHOD(uint64_t, value, (), (const)); |
169 | | MOCK_METHOD(absl::optional<bool>, cachedShouldImport, (), (const)); |
170 | | MOCK_METHOD(ImportMode, importMode, (), (const)); |
171 | | |
172 | | bool used_; |
173 | | bool hidden_; |
174 | | uint64_t value_; |
175 | | ImportMode import_mode_; |
176 | | }; |
177 | | |
178 | | class MockHistogram : public MockMetric<Histogram> { |
179 | | public: |
180 | | MockHistogram(); |
181 | | ~MockHistogram() override; |
182 | | |
183 | | MOCK_METHOD(bool, used, (), (const)); |
184 | | MOCK_METHOD(bool, hidden, (), (const)); |
185 | | MOCK_METHOD(Histogram::Unit, unit, (), (const)); |
186 | | MOCK_METHOD(void, recordValue, (uint64_t value)); |
187 | | |
188 | | // RefcountInterface |
189 | 0 | void incRefCount() override { refcount_helper_.incRefCount(); } |
190 | 0 | bool decRefCount() override { return refcount_helper_.decRefCount(); } |
191 | 0 | uint32_t use_count() const override { return refcount_helper_.use_count(); } |
192 | | |
193 | | Unit unit_{Histogram::Unit::Unspecified}; |
194 | | Store* store_{}; |
195 | | |
196 | | private: |
197 | | RefcountHelper refcount_helper_; |
198 | | }; |
199 | | |
200 | | class MockParentHistogram : public MockMetric<ParentHistogram> { |
201 | | public: |
202 | | MockParentHistogram(); |
203 | | ~MockParentHistogram() override; |
204 | | |
205 | 0 | void merge() override {} |
206 | 0 | std::string quantileSummary() const override { return ""; }; |
207 | 0 | std::string bucketSummary() const override { return ""; }; |
208 | | MOCK_METHOD(bool, used, (), (const)); |
209 | | MOCK_METHOD(bool, hidden, (), (const)); |
210 | | MOCK_METHOD(Histogram::Unit, unit, (), (const)); |
211 | | MOCK_METHOD(void, recordValue, (uint64_t value)); |
212 | | MOCK_METHOD(const HistogramStatistics&, cumulativeStatistics, (), (const)); |
213 | | MOCK_METHOD(const HistogramStatistics&, intervalStatistics, (), (const)); |
214 | | MOCK_METHOD(std::vector<Bucket>, detailedTotalBuckets, (), (const)); |
215 | | MOCK_METHOD(std::vector<Bucket>, detailedIntervalBuckets, (), (const)); |
216 | | |
217 | | // RefcountInterface |
218 | 0 | void incRefCount() override { refcount_helper_.incRefCount(); } |
219 | 0 | bool decRefCount() override { return refcount_helper_.decRefCount(); } |
220 | 0 | uint32_t use_count() const override { return refcount_helper_.use_count(); } |
221 | | |
222 | | bool used_; |
223 | | bool hidden_; |
224 | | Unit unit_{Histogram::Unit::Unspecified}; |
225 | | Store* store_{}; |
226 | | std::shared_ptr<HistogramStatistics> histogram_stats_ = |
227 | | std::make_shared<HistogramStatisticsImpl>(); |
228 | | |
229 | | private: |
230 | | RefcountHelper refcount_helper_; |
231 | | }; |
232 | | |
233 | | class MockTextReadout : public MockMetric<TextReadout> { |
234 | | public: |
235 | | MockTextReadout(); |
236 | | ~MockTextReadout() override; |
237 | | |
238 | | MOCK_METHOD(void, set, (absl::string_view value), (override)); |
239 | | MOCK_METHOD(bool, used, (), (const, override)); |
240 | | MOCK_METHOD(bool, hidden, (), (const)); |
241 | | MOCK_METHOD(std::string, value, (), (const, override)); |
242 | | |
243 | | bool used_; |
244 | | bool hidden_; |
245 | | std::string value_; |
246 | | }; |
247 | | |
248 | | class MockMetricSnapshot : public MetricSnapshot { |
249 | | public: |
250 | | MockMetricSnapshot(); |
251 | | ~MockMetricSnapshot() override; |
252 | | |
253 | | MOCK_METHOD(const std::vector<CounterSnapshot>&, counters, ()); |
254 | | MOCK_METHOD(const std::vector<std::reference_wrapper<const Gauge>>&, gauges, ()); |
255 | | MOCK_METHOD(const std::vector<std::reference_wrapper<const ParentHistogram>>&, histograms, ()); |
256 | | MOCK_METHOD(const std::vector<std::reference_wrapper<const TextReadout>>&, textReadouts, ()); |
257 | | MOCK_METHOD(const std::vector<Stats::PrimitiveCounterSnapshot>&, hostCounters, ()); |
258 | | MOCK_METHOD(const std::vector<Stats::PrimitiveGaugeSnapshot>&, hostGauges, ()); |
259 | | MOCK_METHOD(SystemTime, snapshotTime, (), (const)); |
260 | | |
261 | | std::vector<CounterSnapshot> counters_; |
262 | | std::vector<std::reference_wrapper<const Gauge>> gauges_; |
263 | | std::vector<std::reference_wrapper<const ParentHistogram>> histograms_; |
264 | | std::vector<std::reference_wrapper<const TextReadout>> text_readouts_; |
265 | | std::vector<Stats::PrimitiveCounterSnapshot> host_counters_; |
266 | | std::vector<Stats::PrimitiveGaugeSnapshot> host_gauges_; |
267 | | SystemTime snapshot_time_; |
268 | | }; |
269 | | |
270 | | class MockSink : public Sink { |
271 | | public: |
272 | | MockSink(); |
273 | | ~MockSink() override; |
274 | | |
275 | | MOCK_METHOD(void, flush, (MetricSnapshot & snapshot)); |
276 | | MOCK_METHOD(void, onHistogramComplete, (const Histogram& histogram, uint64_t value)); |
277 | | }; |
278 | | |
279 | | class MockSinkPredicates : public SinkPredicates { |
280 | | public: |
281 | | MockSinkPredicates(); |
282 | | ~MockSinkPredicates() override; |
283 | | MOCK_METHOD(bool, includeCounter, (const Counter&)); |
284 | | MOCK_METHOD(bool, includeGauge, (const Gauge&)); |
285 | | MOCK_METHOD(bool, includeTextReadout, (const TextReadout&)); |
286 | | MOCK_METHOD(bool, includeHistogram, (const Histogram&)); |
287 | | }; |
288 | | |
289 | | class MockStore; |
290 | | |
291 | | class MockScope : public TestUtil::TestScope { |
292 | | public: |
293 | | MockScope(StatName prefix, MockStore& store); |
294 | | |
295 | 0 | ScopeSharedPtr createScope(const std::string& name) override { |
296 | 0 | return ScopeSharedPtr(createScope_(name)); |
297 | 0 | } |
298 | 0 | ScopeSharedPtr scopeFromStatName(StatName name) override { |
299 | 0 | return createScope_(symbolTable().toString(name)); |
300 | 0 | } |
301 | | |
302 | | MOCK_METHOD(ScopeSharedPtr, createScope_, (const std::string& name)); |
303 | | MOCK_METHOD(CounterOptConstRef, findCounter, (StatName), (const)); |
304 | | MOCK_METHOD(GaugeOptConstRef, findGauge, (StatName), (const)); |
305 | | MOCK_METHOD(HistogramOptConstRef, findHistogram, (StatName), (const)); |
306 | | MOCK_METHOD(TextReadoutOptConstRef, findTextReadout, (StatName), (const)); |
307 | | |
308 | | // Override the lowest level of stat creation based on StatName to redirect |
309 | | // back to the old string-based mechanisms still on the MockStore object |
310 | | // to allow tests to inject EXPECT_CALL hooks for those. |
311 | | Counter& counterFromStatNameWithTags(const StatName& name, StatNameTagVectorOptConstRef) override; |
312 | | Gauge& gaugeFromStatNameWithTags(const StatName& name, StatNameTagVectorOptConstRef, |
313 | | Gauge::ImportMode import_mode) override; |
314 | | Histogram& histogramFromStatNameWithTags(const StatName& name, StatNameTagVectorOptConstRef, |
315 | | Histogram::Unit unit) override; |
316 | | TextReadout& textReadoutFromStatNameWithTags(const StatName& name, |
317 | | StatNameTagVectorOptConstRef) override; |
318 | | |
319 | | MockStore& mock_store_; |
320 | | }; |
321 | | |
322 | | class MockStore : public TestUtil::TestStore { |
323 | | public: |
324 | | MockStore(); |
325 | | ~MockStore() override; |
326 | | |
327 | | // Store |
328 | | MOCK_METHOD(void, forEachCounter, (SizeFn, StatFn<Counter>), (const)); |
329 | | MOCK_METHOD(void, forEachGauge, (SizeFn, StatFn<Gauge>), (const)); |
330 | | MOCK_METHOD(void, forEachTextReadout, (SizeFn, StatFn<TextReadout>), (const)); |
331 | | MOCK_METHOD(void, forEachHistogram, (SizeFn, StatFn<ParentHistogram>), (const)); |
332 | | MOCK_METHOD(void, forEachSinkedHistogram, (SizeFn, StatFn<ParentHistogram>), (const)); |
333 | | MOCK_METHOD(Counter&, counter, (const std::string&)); |
334 | | MOCK_METHOD(Gauge&, gauge, (const std::string&, Gauge::ImportMode)); |
335 | | MOCK_METHOD(Histogram&, histogram, (const std::string&, Histogram::Unit)); |
336 | | MOCK_METHOD(void, deliverHistogramToSinks, (const Histogram& histogram, uint64_t value)); |
337 | | MOCK_METHOD(TextReadout&, textReadout, (const std::string&)); |
338 | | |
339 | 0 | MockScope& mockScope() { |
340 | 0 | MockScope* scope = dynamic_cast<MockScope*>(rootScope().get()); |
341 | 0 | ASSERT(scope != nullptr); |
342 | 0 | return *scope; |
343 | 0 | } |
344 | | |
345 | | ScopeSharedPtr makeScope(StatName name) override; |
346 | | |
347 | | TestUtil::TestSymbolTable symbol_table_; |
348 | | testing::NiceMock<MockCounter> counter_; |
349 | | testing::NiceMock<MockGauge> gauge_; |
350 | | std::vector<std::unique_ptr<MockHistogram>> histograms_; |
351 | | }; |
352 | | |
353 | | /** |
354 | | * With IsolatedStoreImpl it's hard to test timing stats. |
355 | | * MockIsolatedStatsStore mocks only deliverHistogramToSinks for better testing. |
356 | | */ |
357 | | class MockIsolatedStatsStore : public TestUtil::TestStore { |
358 | | public: |
359 | | MockIsolatedStatsStore(); |
360 | | ~MockIsolatedStatsStore() override; |
361 | | |
362 | | MOCK_METHOD(void, deliverHistogramToSinks, (const Histogram& histogram, uint64_t value)); |
363 | | MOCK_METHOD(const TagVector&, fixedTags, ()); |
364 | | }; |
365 | | |
366 | | class MockStatsMatcher : public StatsMatcher { |
367 | | public: |
368 | | MockStatsMatcher(); |
369 | | ~MockStatsMatcher() override; |
370 | | MOCK_METHOD(bool, rejects, (StatName name), (const)); |
371 | | MOCK_METHOD(StatsMatcher::FastResult, fastRejects, (StatName name), (const)); |
372 | | MOCK_METHOD(bool, slowRejects, (FastResult, StatName name), (const)); |
373 | 0 | bool acceptsAll() const override { return accepts_all_; } |
374 | 0 | bool rejectsAll() const override { return rejects_all_; } |
375 | | |
376 | | bool accepts_all_{false}; |
377 | | bool rejects_all_{false}; |
378 | | }; |
379 | | |
380 | | } // namespace Stats |
381 | | } // namespace Envoy |