removing non-existing file from the build
[folly.git] / folly / wangle / channel / test / OutputBufferingHandlerTest.cpp
1 /*
2  * Copyright 2015 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/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>
23
24 using namespace folly;
25 using namespace folly::wangle;
26 using namespace testing;
27
28 typedef StrictMock<MockHandlerAdapter<
29   IOBufQueue&,
30   std::unique_ptr<IOBuf>>>
31 MockBytesHandler;
32
33 MATCHER_P(IOBufContains, str, "") { return arg->moveToFbString() == str; }
34
35 TEST(OutputBufferingHandlerTest, Basic) {
36   MockBytesHandler mockHandler;
37   EXPECT_CALL(mockHandler, attachPipeline(_));
38   StaticPipeline<IOBufQueue&, std::unique_ptr<IOBuf>,
39     MockBytesHandler,
40     OutputBufferingHandler>
41   pipeline(&mockHandler, OutputBufferingHandler{});
42
43   EventBase eb;
44   auto socket = AsyncSocket::newSocket(&eb);
45   pipeline.setTransport(socket);
46
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")));
54   eb.loopOnce();
55   EXPECT_TRUE(f1.isReady());
56   EXPECT_TRUE(f2.isReady());
57   EXPECT_CALL(mockHandler, detachPipeline(_));
58
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")));
63   eb.loopOnce();
64   EXPECT_TRUE(f.isReady());
65 }