/proc/self/cwd/test/mocks/buffer/mocks.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <string> |
4 | | |
5 | | #include "envoy/buffer/buffer.h" |
6 | | |
7 | | #include "source/common/buffer/buffer_impl.h" |
8 | | #include "source/common/buffer/watermark_buffer.h" |
9 | | #include "source/common/network/io_socket_error_impl.h" |
10 | | |
11 | | #include "test/test_common/printers.h" |
12 | | #include "test/test_common/utility.h" |
13 | | |
14 | | #include "gmock/gmock.h" |
15 | | |
16 | | namespace Envoy { |
17 | | |
18 | | // A template class to allow code reuse between MockBuffer and MockWatermarkBuffer |
19 | | template <class BaseClass> class MockBufferBase : public BaseClass { |
20 | | public: |
21 | | MockBufferBase(); |
22 | | MockBufferBase(std::function<void()> below_low, std::function<void()> above_high, |
23 | | std::function<void()> above_overflow); |
24 | | |
25 | | MOCK_METHOD(void, move, (Buffer::Instance & rhs)); |
26 | | MOCK_METHOD(void, move, (Buffer::Instance & rhs, uint64_t length)); |
27 | | MOCK_METHOD(void, move, |
28 | | (Buffer::Instance & rhs, uint64_t length, bool reset_drain_trackers_and_accounting)); |
29 | | MOCK_METHOD(void, drain, (uint64_t size)); |
30 | | |
31 | 3.90k | void baseMove(Buffer::Instance& rhs) { BaseClass::move(rhs); } Unexecuted instantiation: Envoy::MockBufferBase<Envoy::Buffer::OwnedImpl>::baseMove(Envoy::Buffer::Instance&) Envoy::MockBufferBase<Envoy::Buffer::WatermarkBuffer>::baseMove(Envoy::Buffer::Instance&) Line | Count | Source | 31 | 3.90k | void baseMove(Buffer::Instance& rhs) { BaseClass::move(rhs); } |
|
32 | | void baseDrain(uint64_t size) { BaseClass::drain(size); } |
33 | | |
34 | 6.01k | void trackDrains(uint64_t size) { |
35 | 6.01k | bytes_drained_ += size; |
36 | 6.01k | BaseClass::drain(size); |
37 | 6.01k | } |
38 | | |
39 | 10.1k | uint64_t bytesDrained() const { return bytes_drained_; } |
40 | | |
41 | | private: |
42 | | uint64_t bytes_drained_{0}; |
43 | | }; |
44 | | |
45 | | template <> |
46 | | MockBufferBase<Buffer::WatermarkBuffer>::MockBufferBase(std::function<void()> below_low, |
47 | | std::function<void()> above_high, |
48 | | std::function<void()> above_overflow); |
49 | | template <> MockBufferBase<Buffer::WatermarkBuffer>::MockBufferBase(); |
50 | | |
51 | | template <> |
52 | | MockBufferBase<Buffer::OwnedImpl>::MockBufferBase(std::function<void()> below_low, |
53 | | std::function<void()> above_high, |
54 | | std::function<void()> above_overflow); |
55 | | template <> MockBufferBase<Buffer::OwnedImpl>::MockBufferBase(); |
56 | | |
57 | | class MockBuffer : public MockBufferBase<Buffer::OwnedImpl> { |
58 | | public: |
59 | 0 | MockBuffer() { |
60 | 0 | ON_CALL(*this, move(testing::_)).WillByDefault(testing::Invoke(this, &MockBuffer::baseMove)); |
61 | 0 | } |
62 | | }; |
63 | | |
64 | | class MockWatermarkBuffer : public MockBufferBase<Buffer::WatermarkBuffer> { |
65 | | public: |
66 | | using BaseClass = MockBufferBase<Buffer::WatermarkBuffer>; |
67 | | |
68 | | MockWatermarkBuffer(std::function<void()> below_low, std::function<void()> above_high, |
69 | | std::function<void()> above_overflow) |
70 | 2.11k | : BaseClass(below_low, above_high, above_overflow) { |
71 | 2.11k | ON_CALL(*this, move(testing::_)) |
72 | 2.11k | .WillByDefault(testing::Invoke(this, &MockWatermarkBuffer::baseMove)); |
73 | 2.11k | } |
74 | | }; |
75 | | |
76 | | class MockBufferFactory : public Buffer::WatermarkFactory { |
77 | | public: |
78 | | MockBufferFactory(); |
79 | | ~MockBufferFactory() override; |
80 | | |
81 | | Buffer::InstancePtr createBuffer(std::function<void()> below_low, |
82 | | std::function<void()> above_high, |
83 | 384k | std::function<void()> above_overflow) override { |
84 | 384k | auto buffer = Buffer::InstancePtr{createBuffer_(below_low, above_high, above_overflow)}; |
85 | 384k | ASSERT(buffer != nullptr); |
86 | 384k | return buffer; |
87 | 384k | } |
88 | | |
89 | | MOCK_METHOD(Buffer::Instance*, createBuffer_, |
90 | | (std::function<void()> below_low, std::function<void()> above_high, |
91 | | std::function<void()> above_overflow)); |
92 | | |
93 | | MOCK_METHOD(Buffer::BufferMemoryAccountSharedPtr, createAccount, (Http::StreamResetHandler&)); |
94 | | MOCK_METHOD(uint64_t, resetAccountsGivenPressure, (float)); |
95 | | }; |
96 | | |
97 | | MATCHER_P(BufferEqual, rhs, testing::PrintToString(*rhs)) { |
98 | | return TestUtility::buffersEqual(arg, *rhs); |
99 | | } |
100 | | |
101 | | MATCHER_P(BufferStringEqual, rhs, rhs) { |
102 | | *result_listener << "\"" << arg.toString() << "\""; |
103 | | |
104 | | Buffer::OwnedImpl buffer(rhs); |
105 | | return TestUtility::buffersEqual(arg, buffer); |
106 | | } |
107 | | |
108 | | MATCHER_P(BufferStringContains, rhs, |
109 | | std::string(negation ? "doesn't contain" : "contains") + " \"" + rhs + "\"") { |
110 | | *result_listener << "\"" << arg.toString() << "\""; |
111 | | return arg.toString().find(rhs) != std::string::npos; |
112 | | } |
113 | | |
114 | | ACTION_P(AddBufferToString, target_string) { |
115 | | auto bufferToString = [](const Buffer::OwnedImpl& buf) -> std::string { return buf.toString(); }; |
116 | | target_string->append(bufferToString(arg0)); |
117 | | arg0.drain(arg0.length()); |
118 | | } |
119 | | |
120 | | ACTION_P(AddBufferToStringWithoutDraining, target_string) { |
121 | | target_string->append(arg0.toString()); |
122 | | } |
123 | | |
124 | | MATCHER_P(RawSliceVectorEqual, rhs, testing::PrintToString(rhs)) { |
125 | | return TestUtility::rawSlicesEqual(arg, rhs.data(), rhs.size()); |
126 | | } |
127 | | |
128 | | } // namespace Envoy |