proxygen
ObservingClientPipelineTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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  */
18 #include "wangle/channel/Handler.h"
20 
21 #include <glog/logging.h>
23 
24 using namespace wangle;
25 using namespace folly;
26 using namespace testing;
27 
30 
33 
36  bool operator==(const TestRoutingData& other) const {
37  return this->data == other.data;
38  }
39  bool operator<(const TestRoutingData& other) const {
40  return this->data < other.data;
41  }
42 };
43 
44 class TestPipelineFactory : public PipelineFactory<BytesPipeline> {
45  public:
47  std::shared_ptr<AsyncTransportWrapper> /* unused */) override {
48  pipelines_++;
49  auto pipeline = BytesPipeline::create();
50  pipeline->addBack(new BytesToBytesHandler());
51  pipeline->finalize();
52  return pipeline;
53  }
54  std::atomic<int> pipelines_{0};
55 };
56 
58  : public TestPipelineFactory,
60  std::shared_ptr<folly::IOBuf>, TestRoutingData> {
61  public:
64  nullptr, nullptr) {
65  }
66 
68  std::shared_ptr<folly::AsyncTransportWrapper> socket,
69  const TestRoutingData& routingData,
71  std::shared_ptr<TransportInfo> /* unused */) override {
72  routingData_ = routingData;
73  auto pipeline = TestObsPipeline::create();
74  pipeline->addBack(AsyncSocketHandler(socket));
75  pipeline->finalize();
76  routingPipelines_++;
77  return pipeline;
78  }
79 
81  std::shared_ptr<AsyncTransportWrapper> sock) override {
82  // Should not be called.
83  ADD_FAILURE() << "Should not be called, "
84  << "this function is typically called from "
85  << "makePipeline that has been overridden in this "
86  << "test to call a different version of newPipeline.";
88  }
89 
91  std::atomic<int> routingPipelines_{0};
92 };
93 
95  public:
97  const TestRoutingData& routingData,
98  const std::shared_ptr<CustomPipelineFactory>& factory)
99  : routingData_(routingData),
100  factory_(factory) {
101  }
102 
104  std::shared_ptr<folly::AsyncTransportWrapper> socket) override {
105  setPipeline(factory_->newPipeline(
106  socket, routingData_, nullptr, nullptr));
107  }
108 
110  std::shared_ptr<CustomPipelineFactory> factory_;
111 };
112 
113 TEST(ObservingClientPipelineTest, CustomPipelineMaker) {
114  TestServer server;
115  auto factory = std::make_shared<TestPipelineFactory>();
116  server.childPipeline(factory);
117  server.bind(0);
118  auto base = EventBaseManager::get()->getEventBase();
119 
120  SocketAddress address;
121  server.getSockets()[0]->getAddress(&address);
122 
123  TestRoutingData routingData;
124  routingData.data = "Test";
125  auto clientPipelineFactory = std::make_shared<CustomPipelineFactory>();
126  auto client =
127  std::make_unique<CustomPipelineMakerTestClient>(
128  routingData, clientPipelineFactory);
129 
130  client->connect(address, std::chrono::milliseconds(0));
131  base->loop();
132  server.stop();
133  server.join();
134 
135  EXPECT_EQ(1, clientPipelineFactory->routingPipelines_);
136  EXPECT_EQ(routingData, clientPipelineFactory->routingData_);
137  EXPECT_EQ(0, clientPipelineFactory->pipelines_);
138 }
EventBase * getEventBase() const
TEST(Wangle, ClientServerTest)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
STL namespace.
HandlerAdapter< folly::IOBufQueue &, std::unique_ptr< folly::IOBuf > > BytesToBytesHandler
Definition: Handler.h:173
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
CustomPipelineMakerTestClient(const TestRoutingData &routingData, const std::shared_ptr< CustomPipelineFactory > &factory)
bool operator==(const TestRoutingData &other) const
#define nullptr
Definition: http_parser.c:41
BytesPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
static EventBaseManager * get()
TestObsPipeline::Ptr newPipeline(std::shared_ptr< folly::AsyncTransportWrapper > socket, const TestRoutingData &routingData, RoutingDataHandler< TestRoutingData > *, std::shared_ptr< TransportInfo >) override
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
void makePipeline(std::shared_ptr< folly::AsyncTransportWrapper > socket) override
BytesPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper >) override
const folly::SocketAddress & getAddress() const
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
std::shared_ptr< CustomPipelineFactory > factory_
#define ADD_FAILURE()
Definition: gtest.h:1808
bool operator<(const TestRoutingData &other) const