Make some AsyncTest methods virtual to allow mocking them using gtest/gmock
authorMaxim Georgiev <maxgeorg@fb.com>
Tue, 4 Apr 2017 19:16:07 +0000 (12:16 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 4 Apr 2017 19:21:02 +0000 (12:21 -0700)
Summary: GMock can't mock class methods that are not virtual. This way classes relying on the non-virtual methods of AsyncSocket can't be efficiently tested using gtest/gmock.

Reviewed By: yfeldblum

Differential Revision: D4806838

fbshipit-source-id: fde852b75f7ac1d468c177e317c516cb0dc333b5

folly/io/async/AsyncSocket.h
folly/io/async/test/MockAsyncSocket.h

index fca560bbc3132e13578645961b1aafc8683fc695..f8aa27f6019fa4955b6d90e4af789a42fcc08e86 100644 (file)
@@ -473,28 +473,28 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
    * )
    *
    */
    * )
    *
    */
-  void setErrMessageCB(ErrMessageCallback* callback);
+  virtual void setErrMessageCB(ErrMessageCallback* callback);
 
   /**
    * Get a pointer to ErrMessageCallback implementation currently
    * registered with this socket.
    *
    */
 
   /**
    * Get a pointer to ErrMessageCallback implementation currently
    * registered with this socket.
    *
    */
-  ErrMessageCallback* getErrMessageCallback() const;
+  virtual ErrMessageCallback* getErrMessageCallback() const;
 
   /**
    * Set a pointer to SendMsgParamsCallback implementation which
    * will be used to form ::sendmsg() system call parameters
    *
    */
 
   /**
    * Set a pointer to SendMsgParamsCallback implementation which
    * will be used to form ::sendmsg() system call parameters
    *
    */
-  void setSendMsgParamCB(SendMsgParamsCallback* callback);
+  virtual void setSendMsgParamCB(SendMsgParamsCallback* callback);
 
   /**
    * Get a pointer to SendMsgParamsCallback implementation currently
    * registered with this socket.
    *
    */
 
   /**
    * Get a pointer to SendMsgParamsCallback implementation currently
    * registered with this socket.
    *
    */
-  SendMsgParamsCallback* getSendMsgParamsCB() const;
+  virtual SendMsgParamsCallback* getSendMsgParamsCB() const;
 
   // Read and write methods
   void setReadCB(ReadCallback* callback) override;
 
   // Read and write methods
   void setReadCB(ReadCallback* callback) override;
@@ -698,6 +698,41 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
     return setsockopt(fd_, level, optname, optval, sizeof(T));
   }
 
     return setsockopt(fd_, level, optname, optval, sizeof(T));
   }
 
+  /**
+   * Virtual method for reading a socket option returning integer
+   * value, which is the most typical case. Convenient for overriding
+   * and mocking.
+   *
+   * @param level     same as the "level" parameter in getsockopt().
+   * @param optname   same as the "optname" parameter in getsockopt().
+   * @param optval    same as "optval" parameter in getsockopt().
+   * @param optlen    same as "optlen" parameter in getsockopt().
+   * @return          same as the return value of getsockopt().
+   */
+  virtual int
+  getSockOptVirtual(int level, int optname, void* optval, socklen_t* optlen) {
+    return getsockopt(fd_, level, optname, optval, optlen);
+  }
+
+  /**
+   * Virtual method for setting a socket option accepting integer
+   * value, which is the most typical case. Convenient for overriding
+   * and mocking.
+   *
+   * @param level     same as the "level" parameter in setsockopt().
+   * @param optname   same as the "optname" parameter in setsockopt().
+   * @param optval    same as "optval" parameter in setsockopt().
+   * @param optlen    same as "optlen" parameter in setsockopt().
+   * @return          same as the return value of setsockopt().
+   */
+  virtual int setSockOptVirtual(
+      int level,
+      int optname,
+      void const* optval,
+      socklen_t optlen) {
+    return setsockopt(fd_, level, optname, optval, optlen);
+  }
+
   /**
    * Set pre-received data, to be returned to read callback before any data
    * from the socket.
   /**
    * Set pre-received data, to be returned to read callback before any data
    * from the socket.
index d6cb3da2fd5a635c66a95aa54d2fbd11c14acca2..c4b9dcacf4905b830add790c9c5b46cb96a21a41 100644 (file)
@@ -47,6 +47,10 @@ class MockAsyncSocket : public AsyncSocket {
   MOCK_CONST_METHOD0(hangup, bool());
   MOCK_METHOD1(setReadCB, void(ReadCallback*));
   MOCK_METHOD1(_setPreReceivedData, void(std::unique_ptr<IOBuf>&));
   MOCK_CONST_METHOD0(hangup, bool());
   MOCK_METHOD1(setReadCB, void(ReadCallback*));
   MOCK_METHOD1(_setPreReceivedData, void(std::unique_ptr<IOBuf>&));
+  MOCK_CONST_METHOD0(getRawBytesWritten, size_t());
+  MOCK_METHOD4(setSockOptVirtual, int(int, int, void const*, socklen_t));
+  MOCK_METHOD1(setErrMessageCB, void(AsyncSocket::ErrMessageCallback*));
+  MOCK_METHOD1(setSendMsgParamCB, void(AsyncSocket::SendMsgParamsCallback*));
   void setPreReceivedData(std::unique_ptr<IOBuf> data) override {
     return _setPreReceivedData(data);
   }
   void setPreReceivedData(std::unique_ptr<IOBuf> data) override {
     return _setPreReceivedData(data);
   }