Make sendmsg in AsyncUDPSocket overridable
authorYang Chi <yangchi@fb.com>
Mon, 31 Jul 2017 20:48:23 +0000 (13:48 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 31 Jul 2017 20:53:14 +0000 (13:53 -0700)
Summary: Add a protected virtual sendmsg method to AsyncUDPSocket. And make it default to call the system sendmsg function. This enables subclasses of AsyncUDPSocket to be able to override the behavior of sendmseg, also makes AsyncUDPSocket easier to mock and test.

Reviewed By: afrind

Differential Revision: D5459745

fbshipit-source-id: b0227bf7503b1096cb1f0bfc8b9c784db2f2e45d

folly/io/async/AsyncUDPSocket.cpp
folly/io/async/AsyncUDPSocket.h
folly/io/async/test/AsyncUDPSocketTest.cpp

index 4d195d36dcae40aefb785418c6c0cce9644d0acb..6f46c011282eabe4264cdd638e94aa88c8320bb3 100644 (file)
@@ -176,7 +176,7 @@ ssize_t AsyncUDPSocket::writev(const folly::SocketAddress& address,
   msg.msg_controllen = 0;
   msg.msg_flags = 0;
 
-  return ::sendmsg(fd_, &msg, 0);
+  return sendmsg(fd_, &msg, 0);
 }
 
 void AsyncUDPSocket::resumeRead(ReadCallback* cob) {
index 21449420942a705771714e490483ce6fbc637da7..6d49ed675f172030079fc86c3e098505bf0ed597 100644 (file)
@@ -162,6 +162,11 @@ class AsyncUDPSocket : public EventHandler {
     return eventBase_;
   }
 
+ protected:
+  virtual ssize_t sendmsg(int socket, const struct msghdr* message, int flags) {
+    return ::sendmsg(socket, message, flags);
+  }
+
  private:
   AsyncUDPSocket(const AsyncUDPSocket&) = delete;
   AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
index 09ebfcc1d8cf08865fb04983fc4ed73f02a28e78..372e4c37d8cfe5270d6e0ad3ce141d0a3d11b243 100644 (file)
@@ -21,6 +21,7 @@
 #include <folly/io/async/EventBase.h>
 
 #include <folly/io/IOBuf.h>
+#include <folly/portability/GMock.h>
 #include <folly/portability/GTest.h>
 
 #include <thread>
@@ -31,6 +32,7 @@ using folly::AsyncTimeout;
 using folly::EventBase;
 using folly::SocketAddress;
 using folly::IOBuf;
+using namespace testing;
 
 class UDPAcceptor
     : public AsyncUDPServerSocket::Callback {
@@ -289,3 +291,10 @@ TEST(AsyncSocketTest, PingPong) {
   // Wait for server thread to joib
   serverThread.join();
 }
+
+class TestAsyncUDPSocket : public AsyncUDPSocket {
+ public:
+  explicit TestAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {}
+
+  MOCK_METHOD3(sendmsg, ssize_t(int, const struct msghdr*, int));
+};