Coverage Report

Created: 2023-11-12 09:30

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