/proc/self/cwd/test/mocks/event/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 | | |
8 | | #include "envoy/common/scope_tracker.h" |
9 | | #include "envoy/common/time.h" |
10 | | #include "envoy/event/deferred_deletable.h" |
11 | | #include "envoy/event/dispatcher.h" |
12 | | #include "envoy/event/file_event.h" |
13 | | #include "envoy/event/scaled_range_timer_manager.h" |
14 | | #include "envoy/event/signal.h" |
15 | | #include "envoy/network/connection.h" |
16 | | #include "envoy/network/connection_handler.h" |
17 | | #include "envoy/network/dns.h" |
18 | | #include "envoy/network/listener.h" |
19 | | #include "envoy/network/transport_socket.h" |
20 | | #include "envoy/ssl/context.h" |
21 | | |
22 | | #include "source/common/common/scope_tracker.h" |
23 | | |
24 | | #include "test/mocks/buffer/mocks.h" |
25 | | #include "test/test_common/test_time.h" |
26 | | |
27 | | #include "gmock/gmock.h" |
28 | | |
29 | | namespace Envoy { |
30 | | namespace Event { |
31 | | |
32 | | class MockDispatcher : public Dispatcher { |
33 | | public: |
34 | | MockDispatcher(); |
35 | | MockDispatcher(const std::string& name); |
36 | | ~MockDispatcher() override; |
37 | | |
38 | | // Dispatcher |
39 | 0 | const std::string& name() override { return name_; } |
40 | 5.01M | TimeSource& timeSource() override { return *time_system_; } |
41 | 0 | GlobalTimeSystem& globalTimeSystem() { |
42 | 0 | return *(dynamic_cast<GlobalTimeSystem*>(time_system_.get())); |
43 | 0 | } |
44 | | Network::ServerConnectionPtr |
45 | | createServerConnection(Network::ConnectionSocketPtr&& socket, |
46 | | Network::TransportSocketPtr&& transport_socket, |
47 | 0 | StreamInfo::StreamInfo&) override { |
48 | | // The caller expects both the socket and the transport socket to be moved. |
49 | 0 | socket.reset(); |
50 | 0 | transport_socket.reset(); |
51 | 0 | return Network::ServerConnectionPtr{createServerConnection_()}; |
52 | 0 | } |
53 | | |
54 | | Network::ClientConnectionPtr |
55 | | createClientConnection(Network::Address::InstanceConstSharedPtr address, |
56 | | Network::Address::InstanceConstSharedPtr source_address, |
57 | | Network::TransportSocketPtr&& transport_socket, |
58 | | const Network::ConnectionSocket::OptionsSharedPtr& options, |
59 | 22.4k | const Network::TransportSocketOptionsConstSharedPtr&) override { |
60 | 22.4k | return Network::ClientConnectionPtr{ |
61 | 22.4k | createClientConnection_(address, source_address, transport_socket, options)}; |
62 | 22.4k | } |
63 | | |
64 | | FileEventPtr createFileEvent(os_fd_t fd, FileReadyCb cb, FileTriggerType trigger, |
65 | 0 | uint32_t events) override { |
66 | 0 | return FileEventPtr{createFileEvent_(fd, cb, trigger, events)}; |
67 | 0 | } |
68 | | |
69 | 0 | Filesystem::WatcherPtr createFilesystemWatcher() override { |
70 | 0 | return Filesystem::WatcherPtr{createFilesystemWatcher_()}; |
71 | 0 | } |
72 | | |
73 | 12.6k | Event::TimerPtr createTimer(Event::TimerCb cb) override { |
74 | 12.6k | auto timer = Event::TimerPtr{createTimer_(cb)}; |
75 | | // Assert that the timer is not null to avoid confusing test failures down the line. |
76 | 12.6k | ASSERT(timer != nullptr); |
77 | 12.6k | return timer; |
78 | 12.6k | } |
79 | | |
80 | 0 | Event::TimerPtr createScaledTimer(ScaledTimerMinimum minimum, Event::TimerCb cb) override { |
81 | 0 | auto timer = Event::TimerPtr{createScaledTimer_(minimum, cb)}; |
82 | | // Assert that the timer is not null to avoid confusing test failures down the line. |
83 | 0 | ASSERT(timer != nullptr); |
84 | 0 | return timer; |
85 | 0 | } |
86 | | |
87 | 2.32k | Event::TimerPtr createScaledTimer(ScaledTimerType timer_type, Event::TimerCb cb) override { |
88 | 2.32k | auto timer = Event::TimerPtr{createScaledTypedTimer_(timer_type, cb)}; |
89 | | // Assert that the timer is not null to avoid confusing test failures down the line. |
90 | 2.32k | ASSERT(timer != nullptr); |
91 | 2.32k | return timer; |
92 | 2.32k | } |
93 | | |
94 | 64 | Event::SchedulableCallbackPtr createSchedulableCallback(std::function<void()> cb) override { |
95 | 64 | auto schedulable_cb = Event::SchedulableCallbackPtr{createSchedulableCallback_(cb)}; |
96 | 64 | if (!allow_null_callback_) { |
97 | | // Assert that schedulable_cb is not null to avoid confusing test failures down the line. |
98 | 64 | ASSERT(schedulable_cb != nullptr); |
99 | 64 | } |
100 | 64 | return schedulable_cb; |
101 | 64 | } |
102 | | |
103 | 278k | void deferredDelete(DeferredDeletablePtr&& to_delete) override { |
104 | 278k | if (to_delete) { |
105 | 278k | to_delete->deleteIsPending(); |
106 | 278k | } |
107 | 278k | deferredDelete_(to_delete.get()); |
108 | 278k | if (to_delete) { |
109 | 278k | to_delete_.push_back(std::move(to_delete)); |
110 | 278k | } |
111 | 278k | } |
112 | | |
113 | 0 | SignalEventPtr listenForSignal(signal_t signal_num, SignalCb cb) override { |
114 | 0 | return SignalEventPtr{listenForSignal_(signal_num, cb)}; |
115 | 0 | } |
116 | | |
117 | | // Event::Dispatcher |
118 | | MOCK_METHOD(void, registerWatchdog, |
119 | | (const Server::WatchDogSharedPtr&, std::chrono::milliseconds)); |
120 | | MOCK_METHOD(void, initializeStats, (Stats::Scope&, const absl::optional<std::string>&)); |
121 | | MOCK_METHOD(void, clearDeferredDeleteList, ()); |
122 | | MOCK_METHOD(Network::ServerConnection*, createServerConnection_, ()); |
123 | | MOCK_METHOD(Network::ClientConnection*, createClientConnection_, |
124 | | (Network::Address::InstanceConstSharedPtr address, |
125 | | Network::Address::InstanceConstSharedPtr source_address, |
126 | | Network::TransportSocketPtr& transport_socket, |
127 | | const Network::ConnectionSocket::OptionsSharedPtr& options)); |
128 | | MOCK_METHOD(FileEvent*, createFileEvent_, |
129 | | (os_fd_t fd, FileReadyCb cb, FileTriggerType trigger, uint32_t events)); |
130 | | MOCK_METHOD(Filesystem::Watcher*, createFilesystemWatcher_, ()); |
131 | | MOCK_METHOD(Timer*, createTimer_, (Event::TimerCb cb)); |
132 | | MOCK_METHOD(Timer*, createScaledTimer_, (ScaledTimerMinimum minimum, Event::TimerCb cb)); |
133 | | MOCK_METHOD(Timer*, createScaledTypedTimer_, (ScaledTimerType timer_type, Event::TimerCb cb)); |
134 | | MOCK_METHOD(SchedulableCallback*, createSchedulableCallback_, (std::function<void()> cb)); |
135 | | MOCK_METHOD(void, deferredDelete_, (DeferredDeletable * to_delete)); |
136 | | MOCK_METHOD(void, exit, ()); |
137 | | MOCK_METHOD(SignalEvent*, listenForSignal_, (signal_t signal_num, SignalCb cb)); |
138 | | MOCK_METHOD(void, post, (PostCb callback)); |
139 | | MOCK_METHOD(void, deleteInDispatcherThread, (DispatcherThreadDeletableConstPtr deletable)); |
140 | | MOCK_METHOD(void, run, (RunType type)); |
141 | | MOCK_METHOD(void, pushTrackedObject, (const ScopeTrackedObject* object)); |
142 | | MOCK_METHOD(void, popTrackedObject, (const ScopeTrackedObject* expected_object)); |
143 | | MOCK_METHOD(bool, trackedObjectStackIsEmpty, (), (const)); |
144 | | MOCK_METHOD(bool, isThreadSafe, (), (const)); |
145 | 567k | Buffer::WatermarkFactory& getWatermarkFactory() override { return buffer_factory_; } |
146 | | MOCK_METHOD(Thread::ThreadId, getCurrentThreadId, ()); |
147 | | MOCK_METHOD(MonotonicTime, approximateMonotonicTime, (), (const)); |
148 | | MOCK_METHOD(void, updateApproximateMonotonicTime, ()); |
149 | | MOCK_METHOD(void, shutdown, ()); |
150 | | |
151 | | std::unique_ptr<TimeSource> time_system_; |
152 | | std::list<DeferredDeletablePtr> to_delete_; |
153 | | testing::NiceMock<MockBufferFactory> buffer_factory_; |
154 | | bool allow_null_callback_{}; |
155 | | |
156 | | private: |
157 | | const std::string name_; |
158 | | }; |
159 | | |
160 | | class MockTimer : public Timer { |
161 | | public: |
162 | | MockTimer(); |
163 | | |
164 | | // Ownership of each MockTimer instance is transferred to the (caller of) dispatcher's |
165 | | // createTimer_(), so to avoid destructing it twice, the MockTimer must have been dynamically |
166 | | // allocated and must not be deleted by it's creator. |
167 | | MockTimer(MockDispatcher* dispatcher); |
168 | | ~MockTimer() override; |
169 | | |
170 | 0 | void invokeCallback() { |
171 | 0 | EXPECT_TRUE(enabled_); |
172 | 0 | enabled_ = false; |
173 | 0 | if (scope_ == nullptr) { |
174 | 0 | callback_(); |
175 | 0 | return; |
176 | 0 | } |
177 | 0 | ScopeTrackerScopeState scope(scope_, *dispatcher_); |
178 | 0 | scope_ = nullptr; |
179 | 0 | callback_(); |
180 | 0 | } |
181 | | |
182 | | // Timer |
183 | | MOCK_METHOD(void, disableTimer, ()); |
184 | | MOCK_METHOD(void, enableTimer, (std::chrono::milliseconds, const ScopeTrackedObject* scope)); |
185 | | MOCK_METHOD(void, enableHRTimer, (std::chrono::microseconds, const ScopeTrackedObject* scope)); |
186 | | MOCK_METHOD(bool, enabled, ()); |
187 | | |
188 | | MockDispatcher* dispatcher_{}; |
189 | | const ScopeTrackedObject* scope_{}; |
190 | | bool enabled_{}; |
191 | | |
192 | | Event::TimerCb callback_; |
193 | | |
194 | | // If not nullptr, will be set on dtor. This can help to verify that the timer was destroyed. |
195 | | bool* timer_destroyed_{}; |
196 | | }; |
197 | | |
198 | | class MockScaledRangeTimerManager : public ScaledRangeTimerManager { |
199 | | public: |
200 | 0 | TimerPtr createTimer(ScaledTimerMinimum minimum, TimerCb callback) override { |
201 | 0 | return TimerPtr{createTimer_(minimum, std::move(callback))}; |
202 | 0 | } |
203 | 0 | TimerPtr createTimer(ScaledTimerType timer_type, TimerCb callback) override { |
204 | 0 | return TimerPtr{createTypedTimer_(timer_type, std::move(callback))}; |
205 | 0 | } |
206 | | MOCK_METHOD(Timer*, createTimer_, (ScaledTimerMinimum, TimerCb)); |
207 | | MOCK_METHOD(Timer*, createTypedTimer_, (ScaledTimerType, TimerCb)); |
208 | | MOCK_METHOD(void, setScaleFactor, (UnitFloat), (override)); |
209 | | }; |
210 | | |
211 | | class MockSchedulableCallback : public SchedulableCallback { |
212 | | public: |
213 | | MockSchedulableCallback(MockDispatcher* dispatcher, |
214 | | testing::MockFunction<void()>* destroy_cb = nullptr); |
215 | | MockSchedulableCallback(MockDispatcher* dispatcher, std::function<void()> callback, |
216 | | testing::MockFunction<void()>* destroy_cb = nullptr); |
217 | | ~MockSchedulableCallback() override; |
218 | | |
219 | 0 | void invokeCallback() { |
220 | 0 | EXPECT_TRUE(enabled_); |
221 | 0 | enabled_ = false; |
222 | 0 | callback_(); |
223 | 0 | } |
224 | | |
225 | | // SchedulableCallback |
226 | | MOCK_METHOD(void, scheduleCallbackCurrentIteration, ()); |
227 | | MOCK_METHOD(void, scheduleCallbackNextIteration, ()); |
228 | | MOCK_METHOD(void, cancel, ()); |
229 | | MOCK_METHOD(bool, enabled, ()); |
230 | | |
231 | | MockDispatcher* dispatcher_{}; |
232 | | bool enabled_{}; |
233 | | |
234 | | private: |
235 | | std::function<void()> callback_; |
236 | | testing::MockFunction<void()>* destroy_cb_{nullptr}; |
237 | | }; |
238 | | |
239 | | class MockSignalEvent : public SignalEvent { |
240 | | public: |
241 | | MockSignalEvent(MockDispatcher* dispatcher); |
242 | | ~MockSignalEvent() override; |
243 | | |
244 | | SignalCb callback_; |
245 | | }; |
246 | | |
247 | | class MockFileEvent : public FileEvent { |
248 | | public: |
249 | | MockFileEvent(); |
250 | | ~MockFileEvent() override; |
251 | | |
252 | | MOCK_METHOD(void, activate, (uint32_t events)); |
253 | | MOCK_METHOD(void, setEnabled, (uint32_t events)); |
254 | | MOCK_METHOD(void, registerEventIfEmulatedEdge, (uint32_t event)); |
255 | | MOCK_METHOD(void, unregisterEventIfEmulatedEdge, (uint32_t event)); |
256 | | }; |
257 | | |
258 | | } // namespace Event |
259 | | } // namespace Envoy |