2017
[folly.git] / folly / io / async / test / WriteChainAsyncTransportWrapperTest.cpp
1 /*
2  * Copyright 2017 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   virtual ~TestWriteChainAsyncTransportWrapper() {
48   }
49 };
50
51 MATCHER_P(BufMatches, expected, "") {
52   folly::IOBufEqual eq;
53   return eq(*arg, *expected);
54 }
55
56 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleIov) {
57   TestWriteChainAsyncTransportWrapper transport;
58   auto buf = folly::IOBuf::copyBuffer("foo");
59
60   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
61
62   auto iov = buf->getIov();
63   transport.writev(nullptr, iov.data(), iov.size());
64 }
65
66 TEST(WriteChainAsyncTransportWrapperTest, TestChainedIov) {
67   TestWriteChainAsyncTransportWrapper transport;
68   auto buf = folly::IOBuf::copyBuffer("hello");
69   buf->prependChain(folly::IOBuf::copyBuffer("world"));
70
71   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
72
73   auto iov = buf->getIov();
74   transport.writev(nullptr, iov.data(), iov.size());
75 }
76
77 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleBuf) {
78   TestWriteChainAsyncTransportWrapper transport;
79   auto buf = folly::IOBuf::copyBuffer("foobar");
80
81   EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
82
83   transport.write(nullptr, buf->data(), buf->length());
84 }
85
86 }}