Skip to content

Commit

Permalink
Add socket mocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
afrind committed Apr 2, 2015
1 parent fe6985d commit b6005aa
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 0 deletions.
4 changes: 4 additions & 0 deletions folly/Makefile.am
Expand Up @@ -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 \
Expand Down
72 changes: 72 additions & 0 deletions folly/io/async/test/MockAsyncSSLSocket.h
@@ -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&));
};

}}
52 changes: 52 additions & 0 deletions folly/io/async/test/MockAsyncServerSocket.h
@@ -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));
};

}}
51 changes: 51 additions & 0 deletions folly/io/async/test/MockAsyncSocket.h
@@ -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());
};

}}
90 changes: 90 additions & 0 deletions folly/io/async/test/MockAsyncTransport.h
@@ -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&));
};

}}

0 comments on commit b6005aa

Please sign in to comment.