proxygen
AcceptorTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <gtest/gtest.h>
19 
20 using namespace folly;
21 using namespace testing;
22 
23 namespace wangle {
24 
26  public:
27  void setNumConnections(const uint64_t numConnections) {
28  numConnections_ = numConnections;
29  }
30 };
31 
32 class TestableAcceptor : public Acceptor {
33  public:
34  explicit TestableAcceptor(const ServerSocketConfig& accConfig) :
35  Acceptor(accConfig) {}
36  ~TestableAcceptor() override {}
37 
39  const uint64_t activeConnectionCountForLoadShedding) {
40  activeConnectionCountForLoadShedding_ =
41  activeConnectionCountForLoadShedding;
42  }
43 
45  const uint64_t connectionCountForLoadShedding) {
46  connectionCountForLoadShedding_ = connectionCountForLoadShedding;
47  }
48 
49  using Acceptor::setLoadShedConfig;
50  using Acceptor::canAccept;
51 
52  protected:
54  return connectionCountForLoadShedding_;
55  }
57  return activeConnectionCountForLoadShedding_;
58  }
59 
60  private:
61  uint64_t connectionCountForLoadShedding_{0};
62  uint64_t activeConnectionCountForLoadShedding_{0};
63 };
64 
65 class AcceptorTest : public Test {
66  protected:
67  void SetUp() override {
68  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
69  }
70 
71  SocketAddress address_ { "127.0.0.1", 2000 };
73  std::shared_ptr<LoadShedConfiguration> loadShedConfig_ =
74  std::make_shared<LoadShedConfiguration>();
76 };
77 
78 TEST_F(AcceptorTest, TestCanAcceptWithNoConnectionCounter) {
79  acceptor_.setLoadShedConfig(loadShedConfig_, nullptr);
80  // Should accept if there is no IConnectionCounter set
81  EXPECT_TRUE(acceptor_.canAccept(address_));
82 }
83 
84 TEST_F(AcceptorTest, TestCanAcceptWithMaxConnectionsZero) {
85  // Should accept if maxConnections is zero
86  connectionCounter_.setMaxConnections(0);
87  EXPECT_TRUE(acceptor_.canAccept(address_));
88 }
89 
90 TEST_F(AcceptorTest, TestCanAcceptWithCurrentConnsLessThanMax) {
91  // Should accept if currentConnections is less than maxConnections
92  connectionCounter_.setNumConnections(100);
93  connectionCounter_.setMaxConnections(200);
94  EXPECT_TRUE(acceptor_.canAccept(address_));
95 }
96 
97 TEST_F(AcceptorTest, TestCanAcceptWithCurrentConnsGreaterThanMax) {
98  // Should not accept if currentConnections is larger than maxConnections
99  connectionCounter_.setNumConnections(300);
100  connectionCounter_.setMaxConnections(200);
101  acceptor_.setConnectionCountForLoadShedding(300);
102  loadShedConfig_->setMaxConnections(200);
103  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
104  EXPECT_FALSE(acceptor_.canAccept(address_));
105 }
106 
107 TEST_F(AcceptorTest, TestCanAcceptWhiteListedAddress) {
108  // Should accept if currentConnections is larger than maxConnections
109  // but the address is whitelisted
110  connectionCounter_.setNumConnections(300);
111  connectionCounter_.setMaxConnections(200);
112  LoadShedConfiguration::AddressSet addrs = { address_ };
113  loadShedConfig_->setWhitelistAddrs(addrs);
114  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
115  EXPECT_TRUE(acceptor_.canAccept(address_));
116 }
117 
118 TEST_F(AcceptorTest, TestCanAcceptWithNoLoadShed) {
119  // Should accept if currentConnections is larger than maxConnections,
120  // the address is not whitelisted and the current active and total connections
121  // counts are below the corresponding thresholds
122  connectionCounter_.setNumConnections(300);
123  connectionCounter_.setMaxConnections(200);
124  loadShedConfig_->setMaxActiveConnections(100);
125  loadShedConfig_->setMaxConnections(200);
126  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
127  EXPECT_TRUE(acceptor_.canAccept(address_));
128 }
129 
130 TEST_F(AcceptorTest, TestCanAcceptWithMaxActiveConnectionsNotSet) {
131  // Should accept if max active connections threshold is not set and
132  // total connections is within the overall max connections limit
133  connectionCounter_.setNumConnections(300);
134  connectionCounter_.setMaxConnections(200);
135  loadShedConfig_->setMaxConnections(400);
136  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
137  acceptor_.setActiveConnectionCountForLoadShedding(300);
138  acceptor_.setConnectionCountForLoadShedding(300);
139  EXPECT_TRUE(acceptor_.canAccept(address_));
140 }
141 
142 TEST_F(AcceptorTest, TestCanAcceptWithActiveConnectionsBreachingThreshold) {
143  // Should not accept if currentConnections is larger than maxConnections,
144  // the address is not whitelisted and the current active connection is larger
145  // than the threshold
146  connectionCounter_.setNumConnections(300);
147  connectionCounter_.setMaxConnections(200);
148  loadShedConfig_->setMaxActiveConnections(100);
149  loadShedConfig_->setMaxConnections(200);
150  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
151  acceptor_.setActiveConnectionCountForLoadShedding(110);
152  EXPECT_FALSE(acceptor_.canAccept(address_));
153 }
154 
155 TEST_F(AcceptorTest, TestCanAcceptWithTotalConnectionsBreachingThreshold) {
156  // Should not accept if currentConnections is larger than maxConnections,
157  // the address is not whitelisted and the current connection is larger
158  // than the threshold
159  connectionCounter_.setNumConnections(300);
160  connectionCounter_.setMaxConnections(200);
161  loadShedConfig_->setMaxActiveConnections(100);
162  loadShedConfig_->setMaxConnections(200);
163  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
164  acceptor_.setConnectionCountForLoadShedding(210);
165  EXPECT_FALSE(acceptor_.canAccept(address_));
166 }
167 
168 TEST_F(AcceptorTest, TestCanAcceptWithBothConnectionCountsBreachingThresholds) {
169  // Should not accept if currentConnections is larger than maxConnections,
170  // the address is not whitelisted and the both current active and total
171  // connections counts are larger than the corresponding thresholds
172  connectionCounter_.setNumConnections(300);
173  connectionCounter_.setMaxConnections(200);
174  loadShedConfig_->setMaxActiveConnections(100);
175  loadShedConfig_->setMaxConnections(200);
176  acceptor_.setLoadShedConfig(loadShedConfig_, &connectionCounter_);
177  acceptor_.setActiveConnectionCountForLoadShedding(110);
178  acceptor_.setConnectionCountForLoadShedding(210);
179  EXPECT_FALSE(acceptor_.canAccept(address_));
180 }
181 
182 } // namespace wangle
void setActiveConnectionCountForLoadShedding(const uint64_t activeConnectionCountForLoadShedding)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
TestableAcceptor(const ServerSocketConfig &accConfig)
void setNumConnections(const uint64_t numConnections)
FizzServerAcceptor * acceptor_
uint64_t getConnectionCountForLoadShedding() const override
std::set< folly::SocketAddress, AddressOnlyCompare > AddressSet
SimpleConnectionCounterForTest connectionCounter_
void setConnectionCountForLoadShedding(const uint64_t connectionCountForLoadShedding)
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
void SetUp() override
uint64_t getActiveConnectionCountForLoadShedding() const override