Fix copyright lines
[folly.git] / folly / io / async / test / WriteChainAsyncTransportWrapperTest.cpp
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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>
21
22 using namespace testing;
23 using testing::_;
24
25 namespace folly {
26 namespace test {
27
28 class TestWriteChainAsyncTransportWrapper :
29   public WriteChainAsyncTransportWrapper<folly::AsyncTransportWrapper> {
30  public:
31   TestWriteChainAsyncTransportWrapper() :
32     WriteChainAsyncTransportWrapper<folly::AsyncTransportWrapper>(nullptr) {}
33
34   MOCK_METHOD3(writeChain, void(
35         folly::AsyncTransportWrapper::WriteCallback*,
36         std::shared_ptr<folly::IOBuf>,
37         folly::WriteFlags));
38
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);
44   }
45
46   // Allow this to be constructed on the stack for easier testing.
47   ~TestWriteChainAsyncTransportWrapper() override {}
48 };
49
50 MATCHER_P(BufMatches, expected, "") {
51   folly::IOBufEqual eq;
52   return eq(*arg, *expected);
53 }
54
55 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleIov) {
56   TestWriteChainAsyncTransportWrapper transport;
57   auto buf = folly::IOBuf::copyBuffer("foo");
58
59   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
60
61   auto iov = buf->getIov();
62   transport.writev(nullptr, iov.data(), iov.size());
63 }
64
65 TEST(WriteChainAsyncTransportWrapperTest, TestChainedIov) {
66   TestWriteChainAsyncTransportWrapper transport;
67   auto buf = folly::IOBuf::copyBuffer("hello");
68   buf->prependChain(folly::IOBuf::copyBuffer("world"));
69
70   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
71
72   auto iov = buf->getIov();
73   transport.writev(nullptr, iov.data(), iov.size());
74 }
75
76 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleBuf) {
77   TestWriteChainAsyncTransportWrapper transport;
78   auto buf = folly::IOBuf::copyBuffer("foobar");
79
80   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
81
82   transport.write(nullptr, buf->data(), buf->length());
83 }
84
85 } // namespace test
86 } // namespace folly