proxygen
AsyncUDPSocketTest.cpp File Reference

Go to the source code of this file.

Classes

class  UDPAcceptor
 
class  UDPServer
 
class  UDPClient
 
class  ConnectedWriteUDPClient
 
class  AsyncSocketIntegrationTest
 
class  TestAsyncUDPSocket
 
class  MockErrMessageCallback
 
class  MockUDPReadCallback
 
class  AsyncUDPSocketTest
 

Functions

 TEST_F (AsyncSocketIntegrationTest, PingPong)
 
 TEST_F (AsyncSocketIntegrationTest, ConnectedPingPong)
 
 TEST_F (AsyncSocketIntegrationTest, ConnectedPingPongServerWrongAddress)
 
 TEST_F (AsyncSocketIntegrationTest, ConnectedPingPongClientWrongAddress)
 
 TEST_F (AsyncSocketIntegrationTest, PingPongUseConnectedSendMsg)
 
 TEST_F (AsyncSocketIntegrationTest, PingPongUseConnectedSendMsgServerWrongAddr)
 
 TEST_F (AsyncSocketIntegrationTest, PingPongPauseResumeListening)
 
 TEST_F (AsyncUDPSocketTest, TestConnect)
 
 TEST_F (AsyncUDPSocketTest, TestErrToNonExistentServer)
 
 TEST_F (AsyncUDPSocketTest, TestUnsetErrCallback)
 
 TEST_F (AsyncUDPSocketTest, CloseInErrorCallback)
 
 TEST_F (AsyncUDPSocketTest, TestNonExistentServerNoErrCb)
 
 TEST_F (AsyncUDPSocketTest, TestBound)
 
 TEST_F (AsyncUDPSocketTest, TestAttachAfterDetachEvbWithReadCallback)
 
 TEST_F (AsyncUDPSocketTest, TestAttachAfterDetachEvbNoReadCallback)
 
 TEST_F (AsyncUDPSocketTest, TestDetachAttach)
 

Function Documentation

TEST_F ( AsyncSocketIntegrationTest  ,
PingPong   
)

Definition at line 394 of file AsyncUDPSocketTest.cpp.

References ASSERT_GT, and folly::none.

394  {
395  startServer();
396  auto pingClient = performPingPongTest(folly::none, false);
397  // This should succeed.
398  ASSERT_GT(pingClient->pongRecvd(), 0);
399 }
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
constexpr None none
Definition: Optional.h:87
TEST_F ( AsyncSocketIntegrationTest  ,
ConnectedPingPong   
)

Definition at line 401 of file AsyncUDPSocketTest.cpp.

References ASSERT_GT.

401  {
402  server->setChangePortForWrites(false);
403  startServer();
404  auto pingClient = performPingPongTest(server->address(), false);
405  // This should succeed
406  ASSERT_GT(pingClient->pongRecvd(), 0);
407 }
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
TEST_F ( AsyncSocketIntegrationTest  ,
ConnectedPingPongServerWrongAddress   
)

Definition at line 409 of file AsyncUDPSocketTest.cpp.

References ASSERT_EQ.

409  {
410  server->setChangePortForWrites(true);
411  startServer();
412  auto pingClient = performPingPongTest(server->address(), false);
413  // This should fail.
414  ASSERT_EQ(pingClient->pongRecvd(), 0);
415 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F ( AsyncSocketIntegrationTest  ,
ConnectedPingPongClientWrongAddress   
)

Definition at line 417 of file AsyncUDPSocketTest.cpp.

References ASSERT_EQ.

417  {
418  server->setChangePortForWrites(false);
419  startServer();
420  folly::SocketAddress connectAddr(
421  server->address().getIPAddress(), server->address().getPort() + 1);
422  auto pingClient = performPingPongTest(connectAddr, false);
423  // This should fail.
424  ASSERT_EQ(pingClient->pongRecvd(), 0);
425 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F ( AsyncSocketIntegrationTest  ,
PingPongUseConnectedSendMsg   
)

Definition at line 427 of file AsyncUDPSocketTest.cpp.

References ASSERT_GT.

427  {
428  server->setChangePortForWrites(false);
429  startServer();
430  auto pingClient = performPingPongTest(server->address(), true);
431  // This should succeed.
432  ASSERT_GT(pingClient->pongRecvd(), 0);
433 }
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
TEST_F ( AsyncSocketIntegrationTest  ,
PingPongUseConnectedSendMsgServerWrongAddr   
)

Definition at line 435 of file AsyncUDPSocketTest.cpp.

References ASSERT_EQ.

435  {
436  server->setChangePortForWrites(true);
437  startServer();
438  auto pingClient = performPingPongTest(server->address(), true);
439  // This should fail.
440  ASSERT_EQ(pingClient->pongRecvd(), 0);
441 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F ( AsyncSocketIntegrationTest  ,
PingPongPauseResumeListening   
)

Definition at line 443 of file AsyncUDPSocketTest.cpp.

References ASSERT_EQ, ASSERT_GT, and folly::none.

443  {
444  startServer();
445 
446  // Exchange should not happen when paused.
447  server->pauseAccepting();
448  auto pausedClient = performPingPongTest(folly::none, false);
449  ASSERT_EQ(pausedClient->pongRecvd(), 0);
450 
451  // Exchange does occur after resuming.
452  server->resumeAccepting();
453  auto pingClient = performPingPongTest(folly::none, false);
454  ASSERT_GT(pingClient->pongRecvd(), 0);
455 }
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
constexpr None none
Definition: Optional.h:87
TEST_F ( AsyncUDPSocketTest  ,
TestConnect   
)

Definition at line 527 of file AsyncUDPSocketTest.cpp.

References EXPECT_EQ, and socket_.

527  {
528  EXPECT_EQ(socket_->connect(addr_), 0);
529 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
AsyncServerSocket::UniquePtr socket_
TEST_F ( AsyncUDPSocketTest  ,
TestErrToNonExistentServer   
)

Definition at line 531 of file AsyncUDPSocketTest.cpp.

References testing::_, addr, folly::IOBuf::copyBuffer(), evb_, EXPECT_CALL, EXPECT_TRUE, testing::Invoke(), folly::EventBase::loopForever(), socket_, and folly::EventBase::terminateLoopSoon().

531  {
532  socket_->resumeRead(&readCb);
533  socket_->setErrMessageCallback(&err);
534  folly::SocketAddress addr("127.0.0.1", 10000);
535  bool errRecvd = false;
536 #ifdef FOLLY_HAVE_MSG_ERRQUEUE
537  EXPECT_CALL(err, errMessage_(_))
538  .WillOnce(Invoke([this, &errRecvd](auto& cmsg) {
539  if ((cmsg.cmsg_level == SOL_IP && cmsg.cmsg_type == IP_RECVERR) ||
540  (cmsg.cmsg_level == SOL_IPV6 && cmsg.cmsg_type == IPV6_RECVERR)) {
541  const struct sock_extended_err* serr =
542  reinterpret_cast<const struct sock_extended_err*>(
543  CMSG_DATA(&cmsg));
544  errRecvd =
545  (serr->ee_origin == SO_EE_ORIGIN_ICMP || SO_EE_ORIGIN_ICMP6);
546  LOG(ERROR) << "errno " << errnoStr(serr->ee_errno);
547  }
549  }));
550 #endif // FOLLY_HAVE_MSG_ERRQUEUE
551  socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
552  evb_.loopForever();
553  EXPECT_TRUE(errRecvd);
554 }
EventBase * evb_
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void terminateLoopSoon()
Definition: EventBase.cpp:493
AsyncServerSocket::UniquePtr socket_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
ThreadPoolListHook * addr
TEST_F ( AsyncUDPSocketTest  ,
TestUnsetErrCallback   
)

Definition at line 556 of file AsyncUDPSocketTest.cpp.

References testing::_, addr, folly::IOBuf::copyBuffer(), evb_, EXPECT_CALL, folly::EventBase::loopForever(), folly::HHWheelTimer::scheduleTimeoutFn(), socket_, folly::EventBase::terminateLoopSoon(), and folly::EventBase::timer().

556  {
557  socket_->resumeRead(&readCb);
558  socket_->setErrMessageCallback(&err);
559  socket_->setErrMessageCallback(nullptr);
560  folly::SocketAddress addr("127.0.0.1", 10000);
561  EXPECT_CALL(err, errMessage_(_)).Times(0);
562  socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
564  [&] { evb_.terminateLoopSoon(); }, std::chrono::milliseconds(30));
565  evb_.loopForever();
566 }
EventBase * evb_
void terminateLoopSoon()
Definition: EventBase.cpp:493
AsyncServerSocket::UniquePtr socket_
void scheduleTimeoutFn(F fn, std::chrono::milliseconds timeout)
Definition: HHWheelTimer.h:229
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
ThreadPoolListHook * addr
HHWheelTimer & timer()
Definition: EventBase.h:526
TEST_F ( AsyncUDPSocketTest  ,
CloseInErrorCallback   
)

Definition at line 568 of file AsyncUDPSocketTest.cpp.

References testing::_, addr, folly::IOBuf::copyBuffer(), evb_, EXPECT_CALL, EXPECT_TRUE, testing::Invoke(), folly::EventBase::loopForever(), socket_, and folly::EventBase::terminateLoopSoon().

568  {
569  socket_->resumeRead(&readCb);
570  socket_->setErrMessageCallback(&err);
571  folly::SocketAddress addr("127.0.0.1", 10000);
572  bool errRecvd = false;
573  EXPECT_CALL(err, errMessage_(_)).WillOnce(Invoke([this, &errRecvd](auto&) {
574  errRecvd = true;
575  socket_->close();
577  }));
578  socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
579  socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
580  evb_.loopForever();
581  EXPECT_TRUE(errRecvd);
582 }
EventBase * evb_
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void terminateLoopSoon()
Definition: EventBase.cpp:493
AsyncServerSocket::UniquePtr socket_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
ThreadPoolListHook * addr
TEST_F ( AsyncUDPSocketTest  ,
TestNonExistentServerNoErrCb   
)

Definition at line 584 of file AsyncUDPSocketTest.cpp.

References testing::_, addr, folly::IOBuf::copyBuffer(), evb_, EXPECT_CALL, EXPECT_FALSE, testing::Invoke(), folly::EventBase::loopForever(), ON_CALL, folly::IOBufQueue::preallocate(), fizz::detail::readBuf(), folly::HHWheelTimer::scheduleTimeoutFn(), socket_, folly::EventBase::terminateLoopSoon(), and folly::EventBase::timer().

584  {
585  socket_->resumeRead(&readCb);
586  folly::SocketAddress addr("127.0.0.1", 10000);
587  bool errRecvd = false;
589  EXPECT_CALL(readCb, getReadBuffer_(_, _))
590  .WillRepeatedly(Invoke([&readBuf](void** buf, size_t* len) {
591  auto readSpace = readBuf.preallocate(2000, 10000);
592  *buf = readSpace.first;
593  *len = readSpace.second;
594  }));
595  ON_CALL(readCb, onReadError_(_)).WillByDefault(Invoke([&errRecvd](auto& ex) {
596  LOG(ERROR) << ex.what();
597  errRecvd = true;
598  }));
599  socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
601  [&] { evb_.terminateLoopSoon(); }, std::chrono::milliseconds(30));
602  evb_.loopForever();
603  EXPECT_FALSE(errRecvd);
604 }
size_t readBuf(Buf &buf, folly::io::Cursor &cursor)
Definition: Types-inl.h:220
std::pair< void *, std::size_t > preallocate(std::size_t min, std::size_t newAllocationSize, std::size_t max=std::numeric_limits< std::size_t >::max())
Definition: IOBufQueue.h:356
EventBase * evb_
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void terminateLoopSoon()
Definition: EventBase.cpp:493
AsyncServerSocket::UniquePtr socket_
void scheduleTimeoutFn(F fn, std::chrono::milliseconds timeout)
Definition: HHWheelTimer.h:229
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
ThreadPoolListHook * addr
HHWheelTimer & timer()
Definition: EventBase.h:526
TEST_F ( AsyncUDPSocketTest  ,
TestBound   
)

Definition at line 606 of file AsyncUDPSocketTest.cpp.

References folly::AsyncUDPSocket::bind(), evb_, EXPECT_FALSE, EXPECT_TRUE, folly::AsyncUDPSocket::isBound(), and folly::netops::socket().

606  {
608  EXPECT_FALSE(socket.isBound());
609  folly::SocketAddress address("0.0.0.0", 0);
610  socket.bind(address);
611  EXPECT_TRUE(socket.isBound());
612 }
EventBase * evb_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( AsyncUDPSocketTest  ,
TestAttachAfterDetachEvbWithReadCallback   
)

Definition at line 614 of file AsyncUDPSocketTest.cpp.

References evb_, EXPECT_FALSE, EXPECT_TRUE, and socket_.

614  {
615  socket_->resumeRead(&readCb);
616  EXPECT_TRUE(socket_->isHandlerRegistered());
617  socket_->detachEventBase();
618  EXPECT_FALSE(socket_->isHandlerRegistered());
619  socket_->attachEventBase(&evb_);
620  EXPECT_TRUE(socket_->isHandlerRegistered());
621 }
EventBase * evb_
AsyncServerSocket::UniquePtr socket_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( AsyncUDPSocketTest  ,
TestAttachAfterDetachEvbNoReadCallback   
)

Definition at line 623 of file AsyncUDPSocketTest.cpp.

References evb_, EXPECT_FALSE, and socket_.

623  {
624  EXPECT_FALSE(socket_->isHandlerRegistered());
625  socket_->detachEventBase();
626  EXPECT_FALSE(socket_->isHandlerRegistered());
627  socket_->attachEventBase(&evb_);
628  EXPECT_FALSE(socket_->isHandlerRegistered());
629 }
EventBase * evb_
AsyncServerSocket::UniquePtr socket_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( AsyncUDPSocketTest  ,
TestDetachAttach   
)

Definition at line 631 of file AsyncUDPSocketTest.cpp.

References testing::_, folly::IOBuf::copyBuffer(), data, evb_, EXPECT_CALL, EXPECT_EQ, testing::Invoke(), folly::EventBase::loopForever(), folly::EventBase::loopOnce(), now(), folly::EventBase::runInEventBaseThread(), folly::EventBase::runInEventBaseThreadAndWait(), socket_, folly::pushmi::detail::t, and folly::EventBase::terminateLoopSoon().

631  {
632  folly::EventBase evb2;
633  auto writeSocket = std::make_shared<folly::AsyncUDPSocket>(&evb_);
634  folly::SocketAddress address("127.0.0.1", 0);
635  writeSocket->bind(address);
636  std::array<uint8_t, 1024> data;
637  std::atomic<int> packetsRecvd{0};
638  EXPECT_CALL(readCb, getReadBuffer_(_, _))
639  .WillRepeatedly(Invoke([&](void** buf, size_t* len) {
640  *buf = data.data();
641  *len = 1024;
642  }));
643  EXPECT_CALL(readCb, onDataAvailable_(_, _, _))
644  .WillRepeatedly(Invoke(
645  [&](const folly::SocketAddress&, size_t, bool) { packetsRecvd++; }));
646  socket_->resumeRead(&readCb);
647  writeSocket->write(socket_->address(), folly::IOBuf::copyBuffer("hello"));
648  while (packetsRecvd != 1) {
649  evb_.loopOnce();
650  }
651  EXPECT_EQ(packetsRecvd, 1);
652 
653  socket_->detachEventBase();
654  std::thread t([&] { evb2.loopForever(); });
655  evb2.runInEventBaseThreadAndWait([&] { socket_->attachEventBase(&evb2); });
656  writeSocket->write(socket_->address(), folly::IOBuf::copyBuffer("hello"));
658  while (packetsRecvd != 2 ||
660  now + std::chrono::milliseconds(10)) {
661  std::this_thread::sleep_for(std::chrono::milliseconds(1));
662  }
663  evb2.runInEventBaseThread([&] {
664  socket_ = nullptr;
665  evb2.terminateLoopSoon();
666  });
667  t.join();
668  EXPECT_EQ(packetsRecvd, 2);
669 }
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::chrono::steady_clock::time_point now()
EventBase * evb_
bool loopOnce(int flags=0)
Definition: EventBase.cpp:271
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void terminateLoopSoon()
Definition: EventBase.cpp:493
AsyncServerSocket::UniquePtr socket_
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43