2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/async/AsyncTransport.h>
18 #include <folly/io/async/WriteChainAsyncTransportWrapper.h>
19 #include <folly/portability/GMock.h>
20 #include <folly/portability/GTest.h>
22 using namespace testing;
28 class TestWriteChainAsyncTransportWrapper :
29 public WriteChainAsyncTransportWrapper<folly::AsyncTransportWrapper> {
31 TestWriteChainAsyncTransportWrapper() :
32 WriteChainAsyncTransportWrapper<folly::AsyncTransportWrapper>(nullptr) {}
34 MOCK_METHOD3(writeChain, void(
35 folly::AsyncTransportWrapper::WriteCallback*,
36 std::shared_ptr<folly::IOBuf>,
39 // gmock doesn't work with the IOBuf&& so we have to wrap this.
40 void writeChain(WriteCallback* callback,
41 std::unique_ptr<folly::IOBuf>&& iob,
42 folly::WriteFlags flags = folly::WriteFlags::NONE) override {
43 writeChain(callback, std::shared_ptr<folly::IOBuf>(iob.release()), flags);
46 // Allow this to be constructed on the stack for easier testing.
47 virtual ~TestWriteChainAsyncTransportWrapper() {
51 MATCHER_P(BufMatches, expected, "") {
53 return eq(*arg, *expected);
56 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleIov) {
57 TestWriteChainAsyncTransportWrapper transport;
58 auto buf = folly::IOBuf::copyBuffer("foo");
60 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
62 auto iov = buf->getIov();
63 transport.writev(nullptr, iov.data(), iov.size());
66 TEST(WriteChainAsyncTransportWrapperTest, TestChainedIov) {
67 TestWriteChainAsyncTransportWrapper transport;
68 auto buf = folly::IOBuf::copyBuffer("hello");
69 buf->prependChain(folly::IOBuf::copyBuffer("world"));
71 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
73 auto iov = buf->getIov();
74 transport.writev(nullptr, iov.data(), iov.size());
77 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleBuf) {
78 TestWriteChainAsyncTransportWrapper transport;
79 auto buf = folly::IOBuf::copyBuffer("foobar");
81 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
83 transport.write(nullptr, buf->data(), buf->length());