test for OutputBufferingHandler
authorJames Sedgwick <jsedgwick@fb.com>
Fri, 21 Nov 2014 19:55:49 +0000 (11:55 -0800)
committerDave Watson <davejwatson@fb.com>
Thu, 11 Dec 2014 15:58:34 +0000 (07:58 -0800)
Summary:
MLE: my first custom matcher

revealed some include issues, also fixed

Test Plan: guess

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, fugalh, njormrod, folly-diffs@

FB internal diff: D1686223

Signature: t1:1686223:1416334120:41bce88d56dd9ca107f3ddc3927c6be9419370cf

folly/experimental/wangle/channel/ChannelHandlerContext.h
folly/experimental/wangle/channel/test/OutputBufferingHandlerTest.cpp [new file with mode: 0644]
folly/io/async/AsyncTransport.h

index b0f1064b254ebe06297e0e710a89e6663e246ef8..baf413ff969413ef28269e26e531dc139ecfe742 100644 (file)
@@ -16,6 +16,7 @@
 
 #pragma once
 
+#include <folly/io/async/AsyncTransport.h>
 #include <folly/wangle/Future.h>
 #include <folly/ExceptionWrapper.h>
 
diff --git a/folly/experimental/wangle/channel/test/OutputBufferingHandlerTest.cpp b/folly/experimental/wangle/channel/test/OutputBufferingHandlerTest.cpp
new file mode 100644 (file)
index 0000000..cf7af13
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2014 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/experimental/wangle/channel/ChannelPipeline.h>
+#include <folly/experimental/wangle/channel/OutputBufferingHandler.h>
+#include <folly/io/async/AsyncSocket.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace folly;
+using namespace folly::wangle;
+using namespace testing;
+
+// TODO(jsedgwick) Extract this to somewhere common and fill it out.
+template <class R, class W = R>
+class MockChannelHandler : public ChannelHandlerAdapter<R, W> {
+ public:
+  typedef typename ChannelHandlerAdapter<R, W>::Context Context;
+  MOCK_METHOD2_T(read, void(Context*, R));
+  MOCK_METHOD2_T(write_, void(Context*, W&));
+
+  Future<void> write(Context* ctx, W msg) override {
+    write_(ctx, msg);
+    return makeFuture();
+  }
+};
+
+typedef StrictMock<MockChannelHandler<IOBufQueue&, std::unique_ptr<IOBuf>>>
+MockHandler;
+
+MATCHER_P(IOBufContains, str, "") { return arg->moveToFbString() == str; }
+
+TEST(OutputBufferingHandlerTest, Basic) {
+  MockHandler mockHandler;
+  ChannelPipeline<IOBufQueue&, std::unique_ptr<IOBuf>,
+    ChannelHandlerPtr<MockHandler, false>,
+    OutputBufferingHandler>
+  pipeline(&mockHandler, OutputBufferingHandler{});
+
+  EventBase eb;
+  auto socket = AsyncSocket::newSocket(&eb);
+  pipeline.attachTransport(socket);
+
+  // Buffering should prevent writes until the EB loops, and the writes should
+  // be batched into one write call.
+  auto f1 = pipeline.write(IOBuf::copyBuffer("hello"));
+  auto f2 = pipeline.write(IOBuf::copyBuffer("world"));
+  EXPECT_FALSE(f1.isReady());
+  EXPECT_FALSE(f2.isReady());
+  EXPECT_CALL(mockHandler, write_(_, IOBufContains("helloworld")));
+  eb.loopOnce();
+  EXPECT_TRUE(f1.isReady());
+  EXPECT_TRUE(f2.isReady());
+}
index 0b929c16ff2bde5f0731196c42248aed81d4db53..66b045b53d15a56fd527b46e5a5787d941bc6087 100644 (file)
 
 #pragma once
 
+#include <folly/SocketAddress.h>
+#include <folly/io/async/DelayedDestruction.h>
+#include <folly/io/async/EventBase.h>
+
 namespace folly {
 
 /*