proxygen
BootstrapTest.cpp File Reference
#include "wangle/bootstrap/ServerBootstrap.h"
#include "wangle/bootstrap/ClientBootstrap.h"
#include "wangle/channel/Handler.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <folly/String.h>
#include <folly/experimental/TestUtil.h>

Go to the source code of this file.

Classes

class  TestClientPipelineFactory
 
class  TestPipelineFactory
 
class  TestAcceptor
 
class  TestAcceptorFactory
 
class  TestHandlerPipeline
 
class  TestHandlerPipelineFactory< HandlerPipeline >
 
class  TestUDPPipeline
 

Typedefs

typedef Pipeline< IOBufQueue &, std::unique_ptr< IOBuf > > BytesPipeline
 
typedef ServerBootstrap< BytesPipelineTestServer
 
typedef ClientBootstrap< BytesPipelineTestClient
 

Functions

 TEST (Bootstrap, Basic)
 
 TEST (Bootstrap, ServerWithPipeline)
 
 TEST (Bootstrap, ServerWithChildHandler)
 
 TEST (Bootstrap, ClientServerTest)
 
 TEST (Bootstrap, ClientConnectionManagerTest)
 
 TEST (Bootstrap, ServerAcceptGroupTest)
 
 TEST (Bootstrap, ServerAcceptGroup2Test)
 
 TEST (Bootstrap, SharedThreadPool)
 
 TEST (Bootstrap, ExistingSocket)
 
 TEST (Bootstrap, LoadBalanceHandler)
 
 TEST (Bootstrap, UDP)
 
 TEST (Bootstrap, UDPClientServerTest)
 
 TEST (Bootstrap, UnixServer)
 
 TEST (Bootstrap, ServerBindFailure)
 

Variables

std::atomic< int > connections {0}
 

Typedef Documentation

typedef Pipeline<IOBufQueue&, std::unique_ptr<IOBuf> > BytesPipeline

Definition at line 30 of file BootstrapTest.cpp.

Definition at line 33 of file BootstrapTest.cpp.

Definition at line 32 of file BootstrapTest.cpp.

Function Documentation

TEST ( Bootstrap  ,
Basic   
)

Definition at line 87 of file BootstrapTest.cpp.

TEST ( Bootstrap  ,
ServerWithPipeline   
)

Definition at line 92 of file BootstrapTest.cpp.

92  {
93  TestServer server;
94  server.childPipeline(std::make_shared<TestPipelineFactory>());
95  server.bind(0);
96  server.stop();
97 }
TEST ( Bootstrap  ,
ServerWithChildHandler   
)

Definition at line 99 of file BootstrapTest.cpp.

99  {
100  TestServer server;
101  server.childHandler(std::make_shared<TestAcceptorFactory>());
102  server.bind(0);
103  server.stop();
104 }
TEST ( Bootstrap  ,
ClientServerTest   
)

Definition at line 106 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

106  {
107  TestServer server;
108  auto factory = std::make_shared<TestPipelineFactory>();
109  server.childPipeline(factory);
110  server.bind(0);
111  auto base = EventBaseManager::get()->getEventBase();
112 
113  SocketAddress address;
114  server.getSockets()[0]->getAddress(&address);
115 
116  TestClient client;
117  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
118  client.connect(address);
119  base->loop();
120  server.stop();
121  server.join();
122 
123  EXPECT_EQ(factory->pipelines, 1);
124 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
ClientConnectionManagerTest   
)

Definition at line 126 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

126  {
127  // Create a single IO thread, and verify that
128  // client connections are pooled properly
129 
130  TestServer server;
131  auto factory = std::make_shared<TestPipelineFactory>();
132  server.childPipeline(factory);
133  server.group(std::make_shared<IOThreadPoolExecutor>(1));
134  server.bind(0);
135  auto base = EventBaseManager::get()->getEventBase();
136 
137  SocketAddress address;
138  server.getSockets()[0]->getAddress(&address);
139 
140  TestClient client;
141  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
142 
143  client.connect(address);
144 
145  TestClient client2;
146  client2.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
147  client2.connect(address);
148 
149  base->loop();
150  server.stop();
151  server.join();
152 
153  EXPECT_EQ(factory->pipelines, 2);
154 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
ServerAcceptGroupTest   
)

Definition at line 156 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), folly::EventBase::loop(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

156  {
157  // Verify that server is using the accept IO group
158 
159  TestServer server;
160  auto factory = std::make_shared<TestPipelineFactory>();
161  server.childPipeline(factory);
162  server.group(std::make_shared<IOThreadPoolExecutor>(1), nullptr);
163  server.bind(0);
164 
165  SocketAddress address;
166  server.getSockets()[0]->getAddress(&address);
167 
168  boost::barrier barrier(2);
169  auto thread = std::thread([&](){
170  TestClient client;
171  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
172  client.connect(address);
173  EventBaseManager::get()->getEventBase()->loop();
174  barrier.wait();
175  });
176  barrier.wait();
177  server.stop();
178  thread.join();
179  server.join();
180 
181  EXPECT_EQ(factory->pipelines, 1);
182 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
ServerAcceptGroup2Test   
)

Definition at line 184 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), folly::INFO, folly::EventBase::loop(), folly::AsyncServerSocket::newSocket(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

184  {
185  // Verify that server is using the accept IO group
186 
187  // Check if reuse port is supported, if not, don't run this test
188  try {
189  EventBase base;
190  auto serverSocket = AsyncServerSocket::newSocket(&base);
191  serverSocket->bind(0);
192  serverSocket->listen(0);
193  serverSocket->startAccepting();
194  serverSocket->setReusePortEnabled(true);
195  serverSocket->stopAccepting();
196  } catch(...) {
197  LOG(INFO) << "Reuse port probably not supported";
198  return;
199  }
200 
201  TestServer server;
202  auto factory = std::make_shared<TestPipelineFactory>();
203  server.childPipeline(factory);
204  server.group(std::make_shared<IOThreadPoolExecutor>(4), nullptr);
205  server.bind(0);
206 
207  SocketAddress address;
208  server.getSockets()[0]->getAddress(&address);
209 
210  TestClient client;
211  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
212 
213  client.connect(address);
214  EventBaseManager::get()->getEventBase()->loop();
215 
216  server.stop();
217  server.join();
218 
219  EXPECT_EQ(factory->pipelines, 1);
220 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
SharedThreadPool   
)

Definition at line 222 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, folly::EventBaseManager::get(), folly::SocketAddress::getAddress(), folly::EventBaseManager::getEventBase(), folly::INFO, folly::EventBase::loop(), folly::AsyncServerSocket::newSocket(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

222  {
223  // Check if reuse port is supported, if not, don't run this test
224  try {
225  EventBase base;
226  auto serverSocket = AsyncServerSocket::newSocket(&base);
227  serverSocket->bind(0);
228  serverSocket->listen(0);
229  serverSocket->startAccepting();
230  serverSocket->setReusePortEnabled(true);
231  serverSocket->stopAccepting();
232  } catch(...) {
233  LOG(INFO) << "Reuse port probably not supported";
234  return;
235  }
236 
237  auto pool = std::make_shared<IOThreadPoolExecutor>(2);
238 
239  TestServer server;
240  auto factory = std::make_shared<TestPipelineFactory>();
241  server.childPipeline(factory);
242  server.group(pool, pool);
243 
244  server.bind(0);
245 
246  SocketAddress address;
247  server.getSockets()[0]->getAddress(&address);
248 
249  TestClient client;
250  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
251  client.connect(address);
252 
253  TestClient client2;
254  client2.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
255  client2.connect(address);
256 
257  TestClient client3;
258  client3.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
259  client3.connect(address);
260 
261  TestClient client4;
262  client4.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
263  client4.connect(address);
264 
265  TestClient client5;
266  client5.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
267  client5.connect(address);
268 
269  EventBaseManager::get()->getEventBase()->loop();
270 
271  server.stop();
272  server.join();
273 
274  EXPECT_EQ(factory->pipelines, 5);
275 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
socklen_t getAddress(sockaddr_storage *addr) const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
ExistingSocket   
)

Definition at line 277 of file BootstrapTest.cpp.

References folly::gen::move, and folly::netops::socket().

277  {
278  TestServer server;
279  auto factory = std::make_shared<TestPipelineFactory>();
280  server.childPipeline(factory);
282  server.bind(std::move(socket));
283 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
std::unique_ptr< AsyncServerSocket, Destructor > UniquePtr
TEST ( Bootstrap  ,
LoadBalanceHandler   
)

Definition at line 310 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), connections, EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), and wangle::BaseClientBootstrap< P >::pipelineFactory().

310  {
311  TestServer server;
312  auto factory = std::make_shared<TestPipelineFactory>();
313  server.childPipeline(factory);
314 
315  auto pipelinefactory =
316  std::make_shared<TestHandlerPipelineFactory<TestHandlerPipeline>>();
317  server.pipeline(pipelinefactory);
318  server.bind(0);
319  auto base = EventBaseManager::get()->getEventBase();
320 
321  SocketAddress address;
322  server.getSockets()[0]->getAddress(&address);
323 
324  TestClient client;
325  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
326  client.connect(address);
327  base->loop();
328  server.stop();
329  server.join();
330 
331  EXPECT_EQ(factory->pipelines, 1);
333 }
std::atomic< int > connections
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
UDP   
)

Definition at line 340 of file BootstrapTest.cpp.

340  {
341  TestServer server;
342  auto factory = std::make_shared<TestPipelineFactory>();
343  auto pipelinefactory =
344  std::make_shared<TestHandlerPipelineFactory<TestUDPPipeline>>();
345  server.pipeline(pipelinefactory);
346  server.channelFactory(std::make_shared<AsyncUDPServerSocketFactory>());
347  server.bind(0);
348 }
TEST ( Bootstrap  ,
UDPClientServerTest   
)

Definition at line 350 of file BootstrapTest.cpp.

References folly::AsyncUDPSocket::bind(), connections, folly::IOBuf::create(), data, EXPECT_EQ, folly::EventBaseManager::get(), TestServer::getAddress(), folly::EventBaseManager::getEventBase(), folly::gen::move, and folly::AsyncUDPSocket::write().

350  {
351  connections = 0;
352 
353  TestServer server;
354  auto factory = std::make_shared<TestPipelineFactory>();
355  auto pipelinefactory =
356  std::make_shared<TestHandlerPipelineFactory<TestUDPPipeline>>();
357  server.pipeline(pipelinefactory);
358  server.channelFactory(std::make_shared<AsyncUDPServerSocketFactory>());
359  server.bind(0);
360 
361  auto base = EventBaseManager::get()->getEventBase();
362 
363  SocketAddress address;
364  server.getSockets()[0]->getAddress(&address);
365 
366  SocketAddress localhost("::1", 0);
367  AsyncUDPSocket client(base);
368  client.bind(localhost);
369  auto data = IOBuf::create(1);
370  data->append(1);
371  *(data->writableData()) = 'a';
372  client.write(address, std::move(data));
373  base->loop();
374  server.stop();
375  server.join();
376 
378 }
std::atomic< int > connections
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const folly::SocketAddress & getAddress() const
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST ( Bootstrap  ,
UnixServer   
)

Definition at line 380 of file BootstrapTest.cpp.

References wangle::ClientBootstrap< Pipeline >::connect(), EXPECT_EQ, EXPECT_TRUE, folly::EventBaseManager::get(), folly::EventBaseManager::getEventBase(), folly::gen::move, wangle::BaseClientBootstrap< P >::pipelineFactory(), and folly::SocketAddress::setFromPath().

380  {
381  TestServer server;
382  auto factory = std::make_shared<TestPipelineFactory>();
383 
384  folly::test::TemporaryDirectory tmpdir("wangle-bootstrap-test");
385  auto socketPath = (tmpdir.path() / "sock").string();
386 
387  server.childPipeline(factory);
388  SocketAddress address;
389  address.setFromPath(socketPath);
390  server.bind(address);
391  auto base = EventBaseManager::get()->getEventBase();
392 
393  TestClient client;
394  client.pipelineFactory(std::make_shared<TestClientPipelineFactory>());
395  auto pipelineFuture = client.connect(address);
396  base->loop();
397  server.stop();
398  server.join();
399 
400  EXPECT_TRUE(std::move(pipelineFuture).get() != nullptr);
401  EXPECT_EQ(factory->pipelines, 1);
402 }
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
BaseClientBootstrap< P > * pipelineFactory(std::shared_ptr< PipelineFactory< P >> factory) noexcept
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( Bootstrap  ,
ServerBindFailure   
)

Definition at line 404 of file BootstrapTest.cpp.

References EXPECT_EQ, FAIL, folly::SocketAddress::getAddress(), and folly::AsyncServerSocket::newSocket().

404  {
405  // Bind to a TCP socket
406  EventBase base;
407  auto serverSocket = AsyncServerSocket::newSocket(&base);
408  serverSocket->bind(0);
409  serverSocket->listen(0);
410 
411  SocketAddress address;
412  serverSocket->getAddress(&address);
413 
414  // Now try starting a server using the address we are already listening on
415  // This should fail.
416 
417  TestServer server;
418  auto factory = std::make_shared<TestPipelineFactory>();
419  server.childPipeline(factory);
420  try {
421  server.bind(address);
422  FAIL() << "shouldn't be allowed to bind to an in-use address";
423  } catch (const std::system_error& ex) {
424  EXPECT_EQ(EADDRINUSE, ex.code().value()) << "unexpected error code " <<
425  ex.code().value() << ": " << ex.code().message();
426  }
427 }
#define FAIL()
Definition: gtest.h:1822
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
socklen_t getAddress(sockaddr_storage *addr) const

Variable Documentation

std::atomic<int> connections {0}

Definition at line 285 of file BootstrapTest.cpp.

Referenced by TestHandlerPipeline::read(), TestUDPPipeline::read(), and TEST().