/proc/self/cwd/test/mocks/common.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstdint> |
4 | | |
5 | | #include "envoy/common/backoff_strategy.h" |
6 | | #include "envoy/common/conn_pool.h" |
7 | | #include "envoy/common/key_value_store.h" |
8 | | #include "envoy/common/random_generator.h" |
9 | | #include "envoy/common/scope_tracker.h" |
10 | | #include "envoy/common/time.h" |
11 | | |
12 | | #include "source/common/common/logger.h" |
13 | | |
14 | | #include "test/test_common/test_time.h" |
15 | | |
16 | | #include "absl/strings/string_view.h" |
17 | | #include "gmock/gmock.h" |
18 | | #include "gtest/gtest.h" |
19 | | |
20 | | namespace Envoy { |
21 | | /** |
22 | | * This action allows us to save a reference parameter to a pointer target. |
23 | | */ |
24 | 22.3k | ACTION_P(SaveArgAddress, target) { *target = &arg0; } |
25 | | |
26 | | /** |
27 | | * Matcher that matches on whether the pointee of both lhs and rhs are equal. |
28 | | */ |
29 | | MATCHER_P(PointeesEq, rhs, "") { |
30 | | *result_listener << testing::PrintToString(*arg) + " != " + testing::PrintToString(*rhs); |
31 | | return *arg == *rhs; |
32 | | } |
33 | | |
34 | | /** |
35 | | * Simple mock that just lets us make sure a method gets called or not called form a lambda. |
36 | | */ |
37 | | class ReadyWatcher { |
38 | | public: |
39 | | ReadyWatcher(); |
40 | | ~ReadyWatcher(); |
41 | | |
42 | | MOCK_METHOD(void, ready, ()); |
43 | | }; |
44 | | |
45 | | // TODO(jmarantz): get rid of this and use SimulatedTimeSystem in its place. |
46 | | class MockTimeSystem : public Event::TestTimeSystem { |
47 | | public: |
48 | | MockTimeSystem(); |
49 | | ~MockTimeSystem() override; |
50 | | |
51 | | // TODO(#4160): Eliminate all uses of MockTimeSystem, replacing with SimulatedTimeSystem, |
52 | | // where timer callbacks are triggered by the advancement of time. This implementation |
53 | | // matches recent behavior, where real-time timers were created directly in libevent |
54 | | // by dispatcher_impl.cc. |
55 | | Event::SchedulerPtr createScheduler(Event::Scheduler& base_scheduler, |
56 | 0 | Event::CallbackScheduler& cb_scheduler) override { |
57 | 0 | return real_time_.createScheduler(base_scheduler, cb_scheduler); |
58 | 0 | } |
59 | 0 | void advanceTimeWaitImpl(const Duration& duration) override { |
60 | 0 | real_time_.advanceTimeWaitImpl(duration); |
61 | 0 | } |
62 | 0 | void advanceTimeAsyncImpl(const Duration& duration) override { |
63 | 0 | real_time_.advanceTimeAsyncImpl(duration); |
64 | 0 | } |
65 | | MOCK_METHOD(SystemTime, systemTime, ()); |
66 | | MOCK_METHOD(MonotonicTime, monotonicTime, ()); |
67 | | |
68 | | Event::TestRealTimeSystem real_time_; // NO_CHECK_FORMAT(real_time) |
69 | | }; |
70 | | |
71 | | class MockBackOffStrategy : public BackOffStrategy { |
72 | | public: |
73 | | ~MockBackOffStrategy() override = default; |
74 | | |
75 | | MOCK_METHOD(uint64_t, nextBackOffMs, ()); |
76 | | MOCK_METHOD(void, reset, ()); |
77 | | MOCK_METHOD(void, reset, (uint64_t base_interval)); |
78 | | MOCK_METHOD(bool, isOverTimeLimit, (uint64_t interval_ms), (const)); |
79 | | }; |
80 | | |
81 | | // Captures absl::string_view parameters into temp strings, for use |
82 | | // with gmock's SaveArg<n>. Providing an absl::string_view compiles, |
83 | | // but fails because by the time you examine the saved value, its |
84 | | // backing store will go out of scope. |
85 | | class StringViewSaver { |
86 | | public: |
87 | 0 | void operator=(absl::string_view view) { value_ = std::string(view); } |
88 | 0 | const std::string& value() const { return value_; } |
89 | 0 | operator std::string() const { return value_; } |
90 | | |
91 | | private: |
92 | | std::string value_; |
93 | | }; |
94 | | |
95 | 0 | inline bool operator==(const char* str, const StringViewSaver& saver) { |
96 | 0 | return saver.value() == str; |
97 | 0 | } |
98 | | |
99 | 0 | inline bool operator==(const StringViewSaver& saver, const char* str) { |
100 | 0 | return saver.value() == str; |
101 | 0 | } |
102 | | |
103 | | class MockScopeTrackedObject : public ScopeTrackedObject { |
104 | | public: |
105 | | MOCK_METHOD(void, dumpState, (std::ostream&, int), (const)); |
106 | | MOCK_METHOD(ExecutionContext*, executionContext, (), (const)); |
107 | | }; |
108 | | |
109 | | namespace ConnectionPool { |
110 | | |
111 | | class MockCancellable : public Cancellable { |
112 | | public: |
113 | | MockCancellable(); |
114 | | ~MockCancellable() override; |
115 | | |
116 | | // ConnectionPool::Cancellable |
117 | | MOCK_METHOD(void, cancel, (CancelPolicy cancel_policy)); |
118 | | }; |
119 | | } // namespace ConnectionPool |
120 | | |
121 | | namespace Random { |
122 | | class MockRandomGenerator : public RandomGenerator { |
123 | | public: |
124 | | MockRandomGenerator(); |
125 | | MockRandomGenerator(uint64_t value); |
126 | | ~MockRandomGenerator() override; |
127 | | |
128 | | MOCK_METHOD(uint64_t, random, ()); |
129 | | MOCK_METHOD(std::string, uuid, ()); |
130 | | |
131 | | uint64_t value_; |
132 | | const std::string uuid_{"a121e9e1-feae-4136-9e0e-6fac343d56c9"}; |
133 | | }; |
134 | | |
135 | | } // namespace Random |
136 | | |
137 | | class MockKeyValueStore : public KeyValueStore { |
138 | | public: |
139 | | MOCK_METHOD(void, addOrUpdate, |
140 | | (absl::string_view, absl::string_view, absl::optional<std::chrono::seconds> ttl)); |
141 | | MOCK_METHOD(void, remove, (absl::string_view)); |
142 | | MOCK_METHOD(absl::optional<absl::string_view>, get, (absl::string_view)); |
143 | | MOCK_METHOD(void, flush, ()); |
144 | | MOCK_METHOD(void, iterate, (ConstIterateCb), (const)); |
145 | | }; |
146 | | |
147 | | class MockKeyValueStoreFactory : public KeyValueStoreFactory { |
148 | | public: |
149 | | MockKeyValueStoreFactory(); |
150 | | MOCK_METHOD(KeyValueStorePtr, createStore, |
151 | | (const Protobuf::Message&, ProtobufMessage::ValidationVisitor&, Event::Dispatcher&, |
152 | | Filesystem::Instance&)); |
153 | | MOCK_METHOD(ProtobufTypes::MessagePtr, createEmptyConfigProto, ()); |
154 | 0 | std::string category() const override { return "envoy.common.key_value"; } |
155 | 0 | std::string name() const override { return "mock_key_value_store_factory"; } |
156 | | }; |
157 | | |
158 | | struct MockLogSink : Logger::SinkDelegate { |
159 | 0 | MockLogSink(Logger::DelegatingLogSinkSharedPtr log_sink) : Logger::SinkDelegate(log_sink) { |
160 | 0 | setDelegate(); |
161 | 0 | } |
162 | 0 | ~MockLogSink() override { restoreDelegate(); } |
163 | | |
164 | | MOCK_METHOD(void, log, (absl::string_view, const spdlog::details::log_msg&)); |
165 | | MOCK_METHOD(void, logWithStableName, |
166 | | (absl::string_view, absl::string_view, absl::string_view, absl::string_view)); |
167 | 0 | void flush() override {} |
168 | | }; |
169 | | |
170 | | } // namespace Envoy |