1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
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/WriteChainAsyncTransportWrapper.h>
18
19 #include <folly/io/async/AsyncTransport.h>
20 #include <folly/portability/GMock.h>
21 #include <folly/portability/GTest.h>
22
23 using testing::_;
24
25 namespace folly {
26 namespace test {
27
28 class TestWriteChainAsyncTransportWrapper
29 : public WriteChainAsyncTransportWrapper<folly::AsyncTransport> {
30 public:
TestWriteChainAsyncTransportWrapper()31 TestWriteChainAsyncTransportWrapper()
32 : WriteChainAsyncTransportWrapper<folly::AsyncTransport>(nullptr) {}
33
34 MOCK_METHOD3(
35 writeChain,
36 void(
37 folly::AsyncTransport::WriteCallback*,
38 std::shared_ptr<folly::IOBuf>,
39 folly::WriteFlags));
40
41 // gmock doesn't work with the IOBuf&& so we have to wrap this.
writeChain(WriteCallback * callback,std::unique_ptr<folly::IOBuf> && iob,folly::WriteFlags flags=folly::WriteFlags::NONE)42 void writeChain(
43 WriteCallback* callback,
44 std::unique_ptr<folly::IOBuf>&& iob,
45 folly::WriteFlags flags = folly::WriteFlags::NONE) override {
46 writeChain(callback, std::shared_ptr<folly::IOBuf>(iob.release()), flags);
47 }
48
49 // Allow this to be constructed on the stack for easier testing.
~TestWriteChainAsyncTransportWrapper()50 ~TestWriteChainAsyncTransportWrapper() override {}
51 };
52
53 MATCHER_P(BufMatches, expected, "") {
54 folly::IOBufEqualTo eq;
55 return eq(*arg, *expected);
56 }
57
TEST(WriteChainAsyncTransportWrapperTest,TestSimpleIov)58 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleIov) {
59 TestWriteChainAsyncTransportWrapper transport;
60 auto buf = folly::IOBuf::copyBuffer("foo");
61
62 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
63
64 auto iov = buf->getIov();
65 transport.writev(nullptr, iov.data(), iov.size());
66 }
67
TEST(WriteChainAsyncTransportWrapperTest,TestChainedIov)68 TEST(WriteChainAsyncTransportWrapperTest, TestChainedIov) {
69 TestWriteChainAsyncTransportWrapper transport;
70 auto buf = folly::IOBuf::copyBuffer("hello");
71 buf->prependChain(folly::IOBuf::copyBuffer("world"));
72
73 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
74
75 auto iov = buf->getIov();
76 transport.writev(nullptr, iov.data(), iov.size());
77 }
78
TEST(WriteChainAsyncTransportWrapperTest,TestSimpleBuf)79 TEST(WriteChainAsyncTransportWrapperTest, TestSimpleBuf) {
80 TestWriteChainAsyncTransportWrapper transport;
81 auto buf = folly::IOBuf::copyBuffer("foobar");
82
83 EXPECT_CALL(transport, writeChain(_, BufMatches(buf.get()), _));
84
85 transport.write(nullptr, buf->data(), buf->length());
86 }
87
88 } // namespace test
89 } // namespace folly
90