forked from thales-e-security/envoy-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
89 lines (72 loc) · 2.7 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <cstdint>
#include "envoy/common/time.h"
#include "envoy/common/token_bucket.h"
#include "envoy/event/timer.h"
#include "common/common/logger.h"
#include "test/test_common/test_time.h"
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace Envoy {
/**
* This action allows us to save a reference parameter to a pointer target.
*/
ACTION_P(SaveArgAddress, target) { *target = &arg0; }
/**
* Matcher that matches on whether the pointee of both lhs and rhs are equal.
*/
MATCHER_P(PointeesEq, rhs, "") {
*result_listener << testing::PrintToString(*arg) + " != " + testing::PrintToString(*rhs);
return *arg == *rhs;
}
/**
* Simple mock that just lets us make sure a method gets called or not called form a lambda.
*/
class ReadyWatcher {
public:
ReadyWatcher();
~ReadyWatcher();
MOCK_METHOD0(ready, void());
};
// TODO(jmarantz): get rid of this and use SimulatedTimeSystem in its place.
class MockTimeSystem : public Event::TestTimeSystem {
public:
MockTimeSystem();
~MockTimeSystem();
// TODO(#4160): Eliminate all uses of MockTimeSystem, replacing with SimulatedTimeSystem,
// where timer callbacks are triggered by the advancement of time. This implementation
// matches recent behavior, where real-time timers were created directly in libevent
// by dispatcher_impl.cc.
Event::SchedulerPtr createScheduler(Event::Scheduler& base_scheduler) override {
return real_time_.createScheduler(base_scheduler);
}
void sleep(const Duration& duration) override { real_time_.sleep(duration); }
Thread::CondVar::WaitStatus
waitFor(Thread::MutexBasicLockable& mutex, Thread::CondVar& condvar,
const Duration& duration) noexcept EXCLUSIVE_LOCKS_REQUIRED(mutex) override {
return real_time_.waitFor(mutex, condvar, duration); // NO_CHECK_FORMAT(real_time)
}
MOCK_METHOD0(systemTime, SystemTime());
MOCK_METHOD0(monotonicTime, MonotonicTime());
Event::TestRealTimeSystem real_time_; // NO_CHECK_FORMAT(real_time)
};
// Captures absl::string_view parameters into temp strings, for use
// with gmock's SaveArg<n>. Providing an absl::string_view compiles,
// but fails because by the time you examine the saved value, its
// backing store will go out of scope.
class StringViewSaver {
public:
void operator=(absl::string_view view) { value_ = std::string(view); }
const std::string& value() const { return value_; }
operator std::string() const { return value_; }
private:
std::string value_;
};
inline bool operator==(const char* str, const StringViewSaver& saver) {
return saver.value() == str;
}
inline bool operator==(const StringViewSaver& saver, const char* str) {
return saver.value() == str;
}
} // namespace Envoy