forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwappableEventBaseTest.cpp
More file actions
188 lines (147 loc) · 4.07 KB
/
SwappableEventBaseTest.cpp
File metadata and controls
188 lines (147 loc) · 4.07 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2004-present Facebook. All Rights Reserved.
#include <folly/ExceptionWrapper.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "rsocket/internal/SwappableEventBase.h"
using SwappableEventBase = rsocket::SwappableEventBase;
namespace {
// helpers for defining new eventbases/"did this callback run" trackers
#define EB(name) auto& name = get_event_base()
#define MAKE_DID_EXEC(name) \
auto name = make_did_exec_tracker_impl(__LINE__, __FILE__, #name)
struct DidExecTracker {
const int line;
const std::string file;
const std::string name;
DidExecTracker(int line, std::string file, std::string name)
: line(line), file(file), name(name) {}
MOCK_METHOD0(mark, void());
};
struct DETMarkedOnce : public ::testing::CardinalityInterface {
explicit DETMarkedOnce(DidExecTracker const& det) : det(det) {}
DidExecTracker const& det;
int ConservativeLowerBound() const override { return 1; }
int ConservativeUpperBound() const override { return 1; }
bool IsSatisfiedByCallCount(int cc) const override { return cc == 1; }
bool IsSaturatedByCallCount(int cc) const override { return cc == 1; }
void DescribeTo(std::ostream* os) const override {
*os << "is called exactly once on ";
*os << "Tracker<" << det.file << ":" << det.line << ">";
}
};
::testing::Cardinality MarkedOnce(DidExecTracker const& det) {
return ::testing::Cardinality(new DETMarkedOnce(det));
}
class SwappableEbTest : public ::testing::Test {
public:
std::vector<std::unique_ptr<folly::EventBase>> ebs;
std::vector<std::shared_ptr<DidExecTracker>> did_exec_trackers;
void loop_ebs() {
{
::testing::InSequence s;
for(auto tracker : did_exec_trackers) {
EXPECT_CALL(*tracker, mark()).Times(MarkedOnce(*tracker));
}
}
for(auto& eb : ebs) {
ASSERT_TRUE(eb->loop());
}
// dtor verifies EXPECT_CALL
did_exec_trackers.clear();
}
std::shared_ptr<DidExecTracker> make_did_exec_tracker_impl(
int line,
std::string const& file,
std::string const& name
) {
did_exec_trackers.emplace_back(new DidExecTracker(line, file, name));
return did_exec_trackers.back();
}
folly::EventBase& get_event_base() {
ebs.emplace_back(new folly::EventBase());
return *ebs.back();
}
void TearDown() override {
// verify any trackers created after the last loop_ebs call
loop_ebs();
}
};
TEST_F(SwappableEbTest, MarkedOnceSanityCheck) {
MAKE_DID_EXEC(t1);
MAKE_DID_EXEC(t2);
{
::testing::InSequence s;
EXPECT_CALL(*t1, mark()).Times(MarkedOnce(*t1));
EXPECT_CALL(*t2, mark()).Times(MarkedOnce(*t2));
}
t1->mark();
t2->mark();
did_exec_trackers.clear();
}
TEST_F(SwappableEbTest, RunningInCorrectEb) {
EB(EbA);
SwappableEventBase seb(EbA);
MAKE_DID_EXEC(t1);
seb.runInEventBaseThread([&](folly::EventBase& eb) {
ASSERT_EQ(&eb, &EbA);
t1->mark();
});
loop_ebs();
}
TEST_F(SwappableEbTest, CanSwapEbs) {
EB(EbA);
EB(EbB);
SwappableEventBase seb(EbA);
seb.setEventBase(EbB);
MAKE_DID_EXEC(t1);
seb.runInEventBaseThread([&](folly::EventBase& eb) {
t1->mark();
ASSERT_EQ(&eb, &EbB);
});
loop_ebs();
}
TEST_F(SwappableEbTest, SkipsToLastEb) {
EB(EbA);
EB(EbB);
EB(EbC);
SwappableEventBase seb(EbA);
MAKE_DID_EXEC(t1);
seb.runInEventBaseThread([&](auto& eb) {
t1->mark();
ASSERT_EQ(&eb, &EbA);
});
loop_ebs();
seb.setEventBase(EbB);
MAKE_DID_EXEC(t2);
seb.runInEventBaseThread([&](auto& eb) {
t2->mark();
ASSERT_EQ(&eb, &EbC);
});
seb.setEventBase(EbC);
MAKE_DID_EXEC(t3);
seb.runInEventBaseThread([&](auto& eb) {
t3->mark();
ASSERT_EQ(&eb, &EbC);
});
loop_ebs();
}
TEST_F(SwappableEbTest, CanDestroySEB) {
EB(EbA);
EB(EbB);
auto seb = std::make_shared<SwappableEventBase>(EbA);
MAKE_DID_EXEC(t1);
seb->runInEventBaseThread([&](auto& eb) {
t1->mark();
ASSERT_EQ(&eb, &EbA);
});
loop_ebs();
seb->setEventBase(EbB);
MAKE_DID_EXEC(t2);
seb->runInEventBaseThread([&](auto& eb) {
t2->mark();
ASSERT_EQ(&eb, &EbA);
});
seb = nullptr;
loop_ebs();
}
} /* namespace */