Create MockAsyncUDPSocket
authorPeter Griess <pgriess@fb.com>
Mon, 24 Aug 2015 13:12:29 +0000 (06:12 -0700)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Mon, 24 Aug 2015 13:20:21 +0000 (06:20 -0700)
Summary: - Mark all methods in AsyncUDPSocket as virtual
- Create new gmock AsyncUDPSocket: MockAsyncUDPSocket

Reviewed By: @yfeldblum

Differential Revision: D2370668

folly/Makefile.am
folly/io/async/AsyncUDPSocket.h
folly/io/async/test/MockAsyncUDPSocket.h [new file with mode: 0644]

index ffc9aef033c0ab3a082f0ba99a04698158a66be5..1a383c5fe37dda1aae69555fce754c3edec8104f 100644 (file)
@@ -216,6 +216,7 @@ nobase_follyinclude_HEADERS = \
        io/async/test/MockAsyncServerSocket.h \
        io/async/test/MockAsyncSSLSocket.h \
        io/async/test/MockAsyncTransport.h \
+       io/async/test/MockAsyncUDPSocket.h \
        io/async/test/SocketPair.h \
        io/async/test/TimeUtil.h \
        io/async/test/UndelayedDestruction.h \
index b8b167d42f2f08927254c1f7f82dc1e382505997..5ad2d1f2463537a71968fc285dcce47ac1d36314 100644 (file)
@@ -87,7 +87,7 @@ class AsyncUDPSocket : public EventHandler {
   /**
    * Returns the address server is listening on
    */
-  const folly::SocketAddress& address() const {
+  virtual const folly::SocketAddress& address() const {
     CHECK_NE(-1, fd_) << "Server not yet bound to an address";
     return localAddress_;
   }
@@ -98,7 +98,7 @@ class AsyncUDPSocket : public EventHandler {
    * use `address()` method above to get it after this method successfully
    * returns.
    */
-  void bind(const folly::SocketAddress& address);
+  virtual void bind(const folly::SocketAddress& address);
 
   /**
    * Use an already bound file descriptor. You can either transfer ownership
@@ -106,40 +106,40 @@ class AsyncUDPSocket : public EventHandler {
    * FDOwnership::SHARED. In case FD is shared, it will not be `close`d in
    * destructor.
    */
-  void setFD(int fd, FDOwnership ownership);
+  virtual void setFD(int fd, FDOwnership ownership);
 
   /**
    * Send the data in buffer to destination. Returns the return code from
    * ::sendmsg.
    */
-  ssize_t write(const folly::SocketAddress& address,
-                const std::unique_ptr<folly::IOBuf>& buf);
+  virtual ssize_t write(const folly::SocketAddress& address,
+                        const std::unique_ptr<folly::IOBuf>& buf);
 
   /**
    * Send data in iovec to destination. Returns the return code from sendmsg.
    */
-  ssize_t writev(const folly::SocketAddress& address,
-                 const struct iovec* vec, size_t veclen);
+  virtual ssize_t writev(const folly::SocketAddress& address,
+                         const struct iovec* vec, size_t veclen);
 
   /**
    * Start reading datagrams
    */
-  void resumeRead(ReadCallback* cob);
+  virtual void resumeRead(ReadCallback* cob);
 
   /**
    * Pause reading datagrams
    */
-  void pauseRead();
+  virtual void pauseRead();
 
   /**
    * Stop listening on the socket.
    */
-  void close();
+  virtual void close();
 
   /**
    * Get internal FD used by this socket
    */
-  int getFD() const {
+  virtual int getFD() const {
     CHECK_NE(-1, fd_) << "Need to bind before getting FD out";
     return fd_;
   }
@@ -147,14 +147,14 @@ class AsyncUDPSocket : public EventHandler {
   /**
    * Set reuse port mode to call bind() on the same address multiple times
    */
-  void setReusePort(bool reusePort) {
+  virtual void setReusePort(bool reusePort) {
     reusePort_ = reusePort;
   }
 
   /**
    * Set SO_REUSEADDR flag on the socket. Default is ON.
    */
-  void setReuseAddr(bool reuseAddr) {
+  virtual void setReuseAddr(bool reuseAddr) {
     reuseAddr_ = reuseAddr;
   }
 
diff --git a/folly/io/async/test/MockAsyncUDPSocket.h b/folly/io/async/test/MockAsyncUDPSocket.h
new file mode 100644 (file)
index 0000000..80f5ccd
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <gmock/gmock.h>
+
+#include <folly/io/async/AsyncUDPSocket.h>
+
+namespace folly { namespace test {
+
+struct MockAsyncUDPSocket : public AsyncUDPSocket {
+  explicit MockAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {}
+  virtual ~MockAsyncUDPSocket() {}
+
+  MOCK_CONST_METHOD0(address, const SocketAddress&());
+  MOCK_METHOD1(bind, void(const SocketAddress&));
+  MOCK_METHOD2(setFD, void(int, AsyncUDPSocket::FDOwnership));
+  MOCK_METHOD2(
+   write,
+   ssize_t(const SocketAddress&, const std::unique_ptr<IOBuf>&));
+  MOCK_METHOD3(
+   writev,
+   ssize_t(const SocketAddress&, const struct iovec*, size_t));
+  MOCK_METHOD1(resumeRead, void(ReadCallback*));
+  MOCK_METHOD0(pauseRead, void());
+  MOCK_METHOD0(close, void());
+  MOCK_CONST_METHOD0(getFD, int());
+  MOCK_METHOD1(setReusePort, void(bool));
+  MOCK_METHOD1(setReuseAddr, void(bool));
+};
+
+}}