2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/wangle/channel/Pipeline.h>
18 #include <folly/wangle/channel/OutputBufferingHandler.h>
19 #include <folly/wangle/channel/test/MockHandler.h>
20 #include <folly/io/async/AsyncSocket.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
24 using namespace folly;
25 using namespace folly::wangle;
26 using namespace testing;
28 typedef StrictMock<MockHandlerAdapter<
30 std::unique_ptr<IOBuf>>>
33 MATCHER_P(IOBufContains, str, "") { return arg->moveToFbString() == str; }
35 TEST(OutputBufferingHandlerTest, Basic) {
36 MockBytesHandler mockHandler;
37 EXPECT_CALL(mockHandler, attachPipeline(_));
38 Pipeline<IOBufQueue&, std::unique_ptr<IOBuf>,
39 HandlerPtr<MockBytesHandler, false>,
40 OutputBufferingHandler>
41 pipeline(&mockHandler, OutputBufferingHandler{});
44 auto socket = AsyncSocket::newSocket(&eb);
45 EXPECT_CALL(mockHandler, attachTransport(_));
46 pipeline.attachTransport(socket);
48 // Buffering should prevent writes until the EB loops, and the writes should
49 // be batched into one write call.
50 auto f1 = pipeline.write(IOBuf::copyBuffer("hello"));
51 auto f2 = pipeline.write(IOBuf::copyBuffer("world"));
52 EXPECT_FALSE(f1.isReady());
53 EXPECT_FALSE(f2.isReady());
54 EXPECT_CALL(mockHandler, write_(_, IOBufContains("helloworld")));
56 EXPECT_TRUE(f1.isReady());
57 EXPECT_TRUE(f2.isReady());
58 EXPECT_CALL(mockHandler, detachPipeline(_));