2 * Copyright 2017 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 ~TestWriteChainAsyncTransportWrapper() override {}
50 MATCHER_P(BufMatches, expected, "") {
52 return eq(*arg, *expected);
55 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleIov) {
56 TestWriteChainAsyncTransportWrapper transport;
57 auto buf = folly::IOBuf::copyBuffer("foo");
59 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
61 auto iov = buf->getIov();
62 transport.writev(nullptr, iov.data(), iov.size());
65 TEST(WriteChainAsyncTransportWrapperTest, TestChainedIov) {
66 TestWriteChainAsyncTransportWrapper transport;
67 auto buf = folly::IOBuf::copyBuffer("hello");
68 buf->prependChain(folly::IOBuf::copyBuffer("world"));
70 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
72 auto iov = buf->getIov();
73 transport.writev(nullptr, iov.data(), iov.size());
76 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleBuf) {
77 TestWriteChainAsyncTransportWrapper transport;
78 auto buf = folly::IOBuf::copyBuffer("foobar");
80 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
82 transport.write(nullptr, buf->data(), buf->length());