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/StaticPipeline.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 StaticPipeline<IOBufQueue&, std::unique_ptr<IOBuf>,
40 OutputBufferingHandler>
41 pipeline(&mockHandler, OutputBufferingHandler{});
44 auto socket = AsyncSocket::newSocket(&eb);
45 pipeline.setTransport(socket);
47 // Buffering should prevent writes until the EB loops, and the writes should
48 // be batched into one write call.
49 auto f1 = pipeline.write(IOBuf::copyBuffer("hello"));
50 auto f2 = pipeline.write(IOBuf::copyBuffer("world"));
51 EXPECT_FALSE(f1.isReady());
52 EXPECT_FALSE(f2.isReady());
53 EXPECT_CALL(mockHandler, write_(_, IOBufContains("helloworld")));
55 EXPECT_TRUE(f1.isReady());
56 EXPECT_TRUE(f2.isReady());
57 EXPECT_CALL(mockHandler, detachPipeline(_));
59 // Make sure the SharedPromise resets correctly
60 auto f = pipeline.write(IOBuf::copyBuffer("foo"));
61 EXPECT_FALSE(f.isReady());
62 EXPECT_CALL(mockHandler, write_(_, IOBufContains("foo")));
64 EXPECT_TRUE(f.isReady());