WriteChainAsyncTransportWrapperTest.cpp 2.8 KB

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