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