18 #include <gtest/gtest.h> 19 #include <gmock/gmock.h> 20 #include <gflags/gflags.h> 22 using namespace folly;
28 class ConnectionManagerTest;
32 using UniquePtr = std::unique_ptr<StrictMock<MockConnection>,
40 explicit MockConnection(ConnectionManagerTest *test)
43 .WillRepeatedly(
Return(
false));
47 .WillByDefault(
Invoke([
this] {
48 closeWhenIdle_ =
true;
54 void timeoutExpired()
noexcept override {
68 MOCK_METHOD2(drainConnections,
void(
double, std::chrono::milliseconds));
70 void setIdle(
bool idle) {
75 void closeWhenIdleImpl();
77 ConnectionManagerTest* test_{
nullptr};
79 bool closeWhenIdle_{
false};
85 ConnectionManagerTest() {
87 std::chrono::milliseconds(100),
92 void SetUp()
override {
96 for (
size_t i = 0;
i < n;
i++) {
98 cm_->addConnection(conns_.front().get());
102 void removeConn(MockConnection* connection) {
103 for (
auto& conn : conns_) {
104 if (conn.get() == connection) {
105 cm_->removeConnection(connection);
112 void testAddDuringCloseWhenIdle(
bool deactivate);
116 std::vector<MockConnection::UniquePtr> conns_;
119 void MockConnection::closeWhenIdleImpl() {
120 if (idle_ && closeWhenIdle_) {
121 test_->removeConn(
this);
125 TEST_F(ConnectionManagerTest, testShutdownSequence) {
129 cm_->onActivated(*conns_.front());
131 cm_->onDeactivated(*conns_.back());
132 for (
const auto& conn : conns_) {
135 cm_->initiateGracefulShutdown(std::chrono::milliseconds(50));
136 eventBase_.loopOnce();
137 for (
const auto& conn : conns_) {
144 TEST_F(ConnectionManagerTest, testRemoveDrainIterator) {
149 cm_->onActivated(*conns_.front());
150 for (
size_t i = 0;
i < conns_.size() - 1;
i++) {
153 auto conn65 = conns_[conns_.size() - 2].get();
154 auto conn66 = conns_[conns_.size() - 1].get();
155 eventBase_.runInLoop([&] {
157 cm_->onDeactivated(*conn65);
159 cm_->removeConnection(conn66);
161 cm_->onDeactivated(*conn65);
163 cm_->initiateGracefulShutdown(std::chrono::milliseconds(50));
166 eventBase_.loopOnce();
167 for (
size_t i = 0;
i < conns_.size() - 1;
i++) {
174 TEST_F(ConnectionManagerTest, testIdleGraceTimeout) {
181 for (
const auto& conn : conns_) {
183 .WillOnce(
Invoke([] { usleep(1000); }));
185 cm_->initiateGracefulShutdown(std::chrono::milliseconds(1));
186 eventBase_.loopOnce();
187 for (
const auto& conn : conns_) {
194 TEST_F(ConnectionManagerTest, testDropAll) {
197 for (
const auto& conn : conns_) {
199 .WillOnce(
Invoke([&] { cm_->removeConnection(conn.get()); }));
201 cm_->dropAllConnections();
204 TEST_F(ConnectionManagerTest, testDropPercent) {
208 const int numToAdd = 100 - conns_.size();
210 const int numToRemove = conns_.size() - 100;
211 for (
int i = 0;
i < numToRemove;
i++) {
212 removeConn(conns_.begin()->get());
214 EXPECT_EQ(100, cm_->getNumConnections());
218 int numToDrop = 100 * pct;
219 auto connIter = conns_.begin();
220 while (connIter != conns_.end() && numToDrop > 0) {
225 cm_->dropConnections(pct);
233 numToDrop = 80 * pct;
234 while (connIter != conns_.end() && numToDrop > 0) {
239 cm_->dropConnections(pct);
246 TEST_F(ConnectionManagerTest, testDrainPercent) {
248 double drain_percentage = .123;
250 for (
size_t i = 58 ;
i < conns_.size(); ++
i) {
254 cm_->drainConnections(drain_percentage, std::chrono::milliseconds(50));
256 for (
size_t i = 58;
i < conns_.size(); ++
i) {
263 TEST_F(ConnectionManagerTest, testDrainPctAfterAll) {
265 double drain_percentage = 0.1;
267 for (
const auto& conn : conns_) {
271 cm_->initiateGracefulShutdown(std::chrono::milliseconds(50));
272 cm_->drainConnections(drain_percentage, std::chrono::milliseconds(50));
273 eventBase_.loopOnce();
275 for (
const auto& conn : conns_) {
282 TEST_F(ConnectionManagerTest, testDrainAllAfterPct) {
284 double drain_pct = 0.8;
286 for (
auto i = conns_.size() -
static_cast<int>(conns_.size() * drain_pct);
287 i < conns_.size(); ++
i) {
291 cm_->drainConnections(drain_pct, std::chrono::milliseconds(50));
294 i < conns_.size() -
static_cast<size_t>(conns_.size() * drain_pct); ++
i) {
298 cm_->initiateGracefulShutdown(std::chrono::milliseconds(50));
299 eventBase_.loopOnce();
301 for (
const auto& conn : conns_) {
308 TEST_F(ConnectionManagerTest, testDropIdle) {
309 for (
const auto& conn : conns_) {
312 .WillRepeatedly(
Return(std::chrono::milliseconds(100)));
316 for (
size_t i = 0;
i < conns_.size() / 2;
i++) {
317 cm_->onDeactivated(*conns_[
i]);
320 cm_->onActivated(*conns_[0]);
322 cm_->removeConnection(conns_[1].
get());
327 for (
size_t i = 2;
i < conns_.size() / 2;
i++) {
329 .WillOnce(
Invoke([
this, i] { cm_->removeConnection(conns_[i].
get()); }));
332 cm_->dropIdleConnections(conns_.size());
335 TEST_F(ConnectionManagerTest, testAddDuringShutdown) {
340 cm_->onActivated(*conns_.front());
341 for (
const auto& conn : conns_) {
344 cm_->initiateGracefulShutdown(std::chrono::milliseconds(50));
345 eventBase_.loopOnce();
346 conns_.insert(conns_.begin(),
std::move(extraConn));
347 EXPECT_CALL(*conns_.front(), notifyPendingShutdown());
348 cm_->addConnection(conns_.front().get());
349 for (
const auto& conn : conns_) {
356 TEST_F(ConnectionManagerTest, testAddDuringShutdownWithoutIdleGrace) {
360 cm_->onActivated(*conns_.front());
361 for (
const auto& conn : conns_) {
364 cm_->initiateGracefulShutdown(std::chrono::milliseconds(0));
365 eventBase_.loopOnce();
367 conns_.insert(conns_.begin(),
std::move(extraConn));
368 EXPECT_CALL(*conns_.front().get(), closeWhenIdle());
369 cm_->addConnection(conns_.front().get());
373 void ConnectionManagerTest::testAddDuringCloseWhenIdle(
bool deactivate) {
378 for (
const auto& conn : conns_) {
382 cm_->initiateGracefulShutdown(std::chrono::milliseconds(0));
384 extraConn->setIdle(
true);
385 conns_.insert(conns_.begin(),
std::move(extraConn));
386 cm_->addConnection(conns_.front().get());
391 conns_.front()->setIdle(
false);
396 cm_->onDeactivated(*conns_.front());
397 conns_.front()->setIdle(
true);
404 cm_->onDeactivated(*conns_.front());
405 conns_.front()->setIdle(
true);
410 TEST_F(ConnectionManagerTest, testAddDuringCloseWhenIdleActive) {
411 testAddDuringCloseWhenIdle(
false);
414 TEST_F(ConnectionManagerTest, testAddDuringCloseWhenIdleInactive) {
415 testAddDuringCloseWhenIdle(
true);
#define MOCK_CONST_METHOD1(m,...)
#define EXPECT_EQ(val1, val2)
std::unique_ptr< ConnectionManager, Destructor > UniquePtr
constexpr detail::Map< Move > move
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define ON_CALL(obj, call)
#define MOCK_METHOD1(m,...)
#define MOCK_CONST_METHOD0(m,...)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define MOCK_METHOD2(m,...)
#define ASSERT_TRUE(condition)
internal::ReturnAction< R > Return(R value)
#define MOCK_METHOD0(m,...)