Add socket mocks
authorAlan Frindell <afrind@fb.com>
Thu, 2 Apr 2015 17:26:53 +0000 (10:26 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 19:02:47 +0000 (12:02 -0700)
Summary: These parallel the MockTAsync* from thrift/lib/cpp/test.  Once all the users of the thrift versions are gone they can be deleted

Test Plan: Unit tests

Reviewed By: yfeldblum@fb.com, alandau@fb.com

Subscribers: doug, net-systems@, folly-diffs@, yfeldblum, chalfant

FB internal diff: D1959979

Signature: t1:1959979:1427917909:19af219f88dd6847a064da986dd30765e29bdc99

folly/Makefile.am
folly/io/async/test/MockAsyncSSLSocket.h [new file with mode: 0644]
folly/io/async/test/MockAsyncServerSocket.h [new file with mode: 0644]
folly/io/async/test/MockAsyncSocket.h [new file with mode: 0644]
folly/io/async/test/MockAsyncTransport.h [new file with mode: 0644]

index 2dcf677128aaefcab6bad1c17b21bc1d3bc5a610..d37cb9b1d545e415e329ceb82b35c717c2ea134b 100644 (file)
@@ -164,6 +164,10 @@ nobase_follyinclude_HEADERS = \
        io/async/TimeoutManager.h \
        io/async/test/AsyncSSLSocketTest.h \
        io/async/test/BlockingSocket.h \
+       io/async/test/MockAsyncSocket.h \
+       io/async/test/MockAsyncServerSocket.h \
+       io/async/test/MockAsyncSSLSocket.h \
+       io/async/test/MockAsyncTransport.h \
        io/async/test/TimeUtil.h \
        io/async/test/UndelayedDestruction.h \
        io/async/test/Util.h \
diff --git a/folly/io/async/test/MockAsyncSSLSocket.h b/folly/io/async/test/MockAsyncSSLSocket.h
new file mode 100644 (file)
index 0000000..47df2f7
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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/AsyncSSLSocket.h>
+
+namespace folly { namespace test {
+
+class MockAsyncSSLSocket : public AsyncSSLSocket {
+ public:
+  MockAsyncSSLSocket(
+   const std::shared_ptr<SSLContext>& ctx,
+   EventBase* base) :
+    AsyncSSLSocket(ctx, base) {
+  }
+
+  GMOCK_METHOD5_(, noexcept, ,
+   connect,
+   void(AsyncSocket::ConnectCallback*,
+    const folly::SocketAddress&,
+    int,
+    const OptionMap&,
+    const folly::SocketAddress&));
+  MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*));
+  MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*));
+  MOCK_METHOD0(closeNow, void());
+  MOCK_CONST_METHOD0(good, bool());
+  MOCK_CONST_METHOD0(readable, bool());
+  MOCK_CONST_METHOD0(hangup, bool());
+  MOCK_CONST_METHOD2(
+   getSelectedNextProtocol,
+   void(const unsigned char**, unsigned*));
+  MOCK_CONST_METHOD2(
+   getSelectedNextProtocolNoThrow,
+   bool(const unsigned char**, unsigned*));
+
+  void sslConn(
+    AsyncSSLSocket::HandshakeCB* cb,
+    uint64_t timeout,
+    const SSLContext::SSLVerifyPeerEnum& verify)
+      override {
+    if (timeout > 0) {
+      handshakeTimeout_.scheduleTimeout((uint32_t)timeout);
+    }
+
+    state_ = StateEnum::ESTABLISHED;
+    sslState_ = STATE_CONNECTING;
+    handshakeCallback_ = cb;
+
+    sslConnectMockable(cb, timeout, verify);
+  }
+  MOCK_METHOD3(
+   sslConnectMockable,
+   void(AsyncSSLSocket::HandshakeCB*, uint64_t,
+     const SSLContext::SSLVerifyPeerEnum&));
+};
+
+}}
diff --git a/folly/io/async/test/MockAsyncServerSocket.h b/folly/io/async/test/MockAsyncServerSocket.h
new file mode 100644 (file)
index 0000000..ac0b8d5
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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/AsyncServerSocket.h>
+#include <folly/io/async/EventBase.h>
+
+namespace folly {
+
+namespace test {
+
+class MockAsyncServerSocket : public AsyncServerSocket {
+public:
+  typedef std::unique_ptr<MockAsyncServerSocket, Destructor> UniquePtr;
+
+  // We explicitly do not mock destroy(), since the base class implementation
+  // in TDelayedDestruction is what actually deletes the object.
+  //MOCK_METHOD0(destroy,
+  //             void());
+  MOCK_METHOD1(bind,
+               void(const folly::SocketAddress& address));
+  MOCK_METHOD2(bind,
+               void(const std::vector<folly::IPAddress>& ipAddresses,
+                    uint16_t port));
+  MOCK_METHOD1(bind,
+               void(uint16_t port));
+  MOCK_METHOD1(listen,
+               void(int backlog));
+  MOCK_METHOD0(startAccepting,
+               void());
+  MOCK_METHOD3(addAcceptCallback,
+               void(AcceptCallback *callback,
+                    EventBase *eventBase,
+                    uint32_t maxAtOnce));
+};
+
+}}
diff --git a/folly/io/async/test/MockAsyncSocket.h b/folly/io/async/test/MockAsyncSocket.h
new file mode 100644 (file)
index 0000000..2da6e8e
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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/AsyncSocket.h>
+#include <folly/io/async/EventBase.h>
+
+namespace folly {
+
+namespace test {
+
+class MockAsyncSocket : public AsyncSocket {
+ public:
+  typedef std::unique_ptr<MockAsyncSocket, Destructor> UniquePtr;
+
+  explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) {
+  }
+
+  GMOCK_METHOD5_(, noexcept, , connect,
+      void(AsyncSocket::ConnectCallback*,
+           const folly::SocketAddress&,
+           int,
+           const OptionMap&,
+           const folly::SocketAddress&));
+
+  MOCK_CONST_METHOD1(getPeerAddress,
+                     void(folly::SocketAddress*));
+  MOCK_METHOD0(detachFd, int());
+  MOCK_CONST_METHOD0(getFd, int());
+  MOCK_METHOD0(closeNow, void());
+  MOCK_CONST_METHOD0(good, bool());
+  MOCK_CONST_METHOD0(readable, bool());
+  MOCK_CONST_METHOD0(hangup, bool());
+};
+
+}}
diff --git a/folly/io/async/test/MockAsyncTransport.h b/folly/io/async/test/MockAsyncTransport.h
new file mode 100644 (file)
index 0000000..e4f559c
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * 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/AsyncTransport.h>
+
+namespace folly { namespace test {
+
+class MockAsyncTransport: public AsyncTransportWrapper {
+ public:
+  MOCK_METHOD1(setReadCB, void(ReadCallback*));
+  MOCK_CONST_METHOD0(getReadCallback, ReadCallback*());
+  MOCK_CONST_METHOD0(getReadCB, ReadCallback*());
+  MOCK_METHOD4(write, void(WriteCallback*,
+                           const void*, size_t,
+                           WriteFlags));
+  MOCK_METHOD4(writev, void(WriteCallback*,
+                            const iovec*, size_t,
+                            WriteFlags));
+  MOCK_METHOD3(writeChain,
+               void(WriteCallback*,
+                    std::shared_ptr<folly::IOBuf>,
+                    WriteFlags));
+
+
+  void writeChain(WriteCallback* callback,
+                  std::unique_ptr<folly::IOBuf>&& iob,
+                  WriteFlags flags =
+                  WriteFlags::NONE) override {
+    writeChain(callback, std::shared_ptr<folly::IOBuf>(iob.release()), flags);
+  }
+
+  MOCK_METHOD0(close, void());
+  MOCK_METHOD0(closeNow, void());
+  MOCK_METHOD0(closeWithReset, void());
+  MOCK_METHOD0(shutdownWrite, void());
+  MOCK_METHOD0(shutdownWriteNow, void());
+  MOCK_CONST_METHOD0(good, bool());
+  MOCK_CONST_METHOD0(readable, bool());
+  MOCK_CONST_METHOD0(connecting, bool());
+  MOCK_CONST_METHOD0(error, bool());
+  MOCK_METHOD1(attachEventBase, void(EventBase*));
+  MOCK_METHOD0(detachEventBase, void());
+  MOCK_CONST_METHOD0(isDetachable, bool());
+  MOCK_CONST_METHOD0(getEventBase, EventBase*());
+  MOCK_METHOD1(setSendTimeout, void(uint32_t));
+  MOCK_CONST_METHOD0(getSendTimeout, uint32_t());
+  MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*));
+  MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*));
+  MOCK_CONST_METHOD0(getAppBytesWritten, size_t());
+  MOCK_CONST_METHOD0(getRawBytesWritten, size_t());
+  MOCK_CONST_METHOD0(getAppBytesReceived, size_t());
+  MOCK_CONST_METHOD0(getRawBytesReceived, size_t());
+  MOCK_CONST_METHOD0(isEorTrackingEnabled, bool());
+  MOCK_METHOD1(setEorTracking, void(bool));
+
+};
+
+class MockReadCallback: public AsyncTransportWrapper::ReadCallback {
+ public:
+  MOCK_METHOD2(getReadBuffer, void(void**, size_t*));
+  GMOCK_METHOD1_(, noexcept, , readDataAvailable, void(size_t));
+  GMOCK_METHOD0_(, noexcept, , readEOF, void());
+  GMOCK_METHOD1_(, noexcept, , readErr,
+                 void(const AsyncSocketException&));
+};
+
+class MockWriteCallback: public AsyncTransportWrapper::WriteCallback {
+ public:
+  GMOCK_METHOD0_(, noexcept, , writeSuccess, void());
+  GMOCK_METHOD2_(, noexcept, , writeErr,
+                 void(size_t, const AsyncSocketException&));
+};
+
+}}