proxygen
PipelineTest.cpp File Reference
#include <wangle/channel/Handler.h>
#include <wangle/channel/Pipeline.h>
#include <wangle/channel/StaticPipeline.h>
#include <wangle/channel/AsyncSocketHandler.h>
#include <wangle/channel/OutputBufferingHandler.h>
#include <wangle/channel/test/MockHandler.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <boost/thread/barrier.hpp>

Go to the source code of this file.

Classes

class  IntHandler2
 
class  ConcreteHandler< Rin, Rout, Win, Wout >
 

Typedefs

typedef StrictMock< MockHandlerAdapter< int, int > > IntHandler
 
typedef HandlerAdapter< std::string, std::stringStringHandler
 
typedef ConcreteHandler< int, std::stringIntToStringHandler
 
typedef ConcreteHandler< std::string, int > StringToIntHandler
 

Functions

 ACTION (FireRead)
 
 ACTION (FireReadEOF)
 
 ACTION (FireReadException)
 
 ACTION (FireWrite)
 
 ACTION (FireClose)
 
 TEST (PipelineTest, RealHandlersCompile)
 
 TEST (PipelineTest, FireActions)
 
 TEST (PipelineTest, ReachEndOfPipeline)
 
 TEST (PipelineTest, TurnAround)
 
 TEST (PipelineTest, DynamicFireActions)
 
 TEST (PipelineTest, DynamicAttachDetachOrder)
 
 TEST (PipelineTest, GetContext)
 
 TEST (PipelineTest, HandlerInMultiplePipelines)
 
 TEST (PipelineTest, HandlerInPipelineTwice)
 
 TEST (PipelineTest, NoDetachOnOwner)
 
 TEST (Pipeline, MissingInboundOrOutbound)
 
 TEST (Pipeline, DynamicConstruction)
 
 TEST (Pipeline, RemovePointer)
 
 TEST (Pipeline, RemoveFront)
 
 TEST (Pipeline, RemoveBack)
 
 TEST (Pipeline, RemoveType)
 
 TEST (Pipeline, Concurrent)
 
 TEST (PipelineTest, NumHandler)
 
 TEST (PipelineTest, HandlerReuse)
 

Typedef Documentation

Definition at line 31 of file PipelineTest.cpp.

Definition at line 274 of file PipelineTest.cpp.

Definition at line 275 of file PipelineTest.cpp.

Function Documentation

ACTION ( FireRead  )

Definition at line 34 of file PipelineTest.cpp.

34  {
35  arg0->fireRead(arg1);
36 }
ACTION ( FireReadEOF  )

Definition at line 38 of file PipelineTest.cpp.

38  {
39  arg0->fireReadEOF();
40 }
ACTION ( FireReadException  )

Definition at line 42 of file PipelineTest.cpp.

42  {
43  arg0->fireReadException(arg1);
44 }
ACTION ( FireWrite  )

Definition at line 46 of file PipelineTest.cpp.

46  {
47  arg0->fireWrite(arg1);
48 }
ACTION ( FireClose  )

Definition at line 50 of file PipelineTest.cpp.

50  {
51  arg0->fireClose();
52 }
TEST ( PipelineTest  ,
RealHandlersCompile   
)

Definition at line 55 of file PipelineTest.cpp.

References EXPECT_TRUE, folly::AsyncSocket::newSocket(), and folly::netops::socket().

55  {
56  EventBase eb;
57  auto socket = AsyncSocket::newSocket(&eb);
58  // static
59  {
62  OutputBufferingHandler>::create(
63  AsyncSocketHandler(socket),
65  EXPECT_TRUE(pipeline->getHandler<AsyncSocketHandler>());
66  EXPECT_TRUE(pipeline->getHandler<OutputBufferingHandler>());
67  }
68  // dynamic
69  {
70  auto pipeline = Pipeline<IOBufQueue&, std::unique_ptr<IOBuf>>::create();
71  (*pipeline)
72  .addBack(AsyncSocketHandler(socket))
73  .addBack(OutputBufferingHandler())
74  .finalize();
75  EXPECT_TRUE(pipeline->getHandler<AsyncSocketHandler>());
76  EXPECT_TRUE(pipeline->getHandler<OutputBufferingHandler>());
77  }
78 }
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( PipelineTest  ,
FireActions   
)

Definition at line 81 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, and EXPECT_NO_THROW.

81  {
82  IntHandler handler1;
83  IntHandler2 handler2;
84 
85  {
86  InSequence sequence;
87  EXPECT_CALL(handler2, attachPipeline(_));
88  EXPECT_CALL(handler1, attachPipeline(_));
89  }
90 
92  &handler1, &handler2);
93 
94  EXPECT_CALL(handler1, read_(_, _)).WillOnce(FireRead());
95  EXPECT_CALL(handler2, read_(_, _)).Times(1);
96  pipeline->read(1);
97 
98  EXPECT_CALL(handler1, readEOF(_)).WillOnce(FireReadEOF());
99  EXPECT_CALL(handler2, readEOF(_)).Times(1);
100  pipeline->readEOF();
101 
102  EXPECT_CALL(handler1, readException(_, _)).WillOnce(FireReadException());
103  EXPECT_CALL(handler2, readException(_, _)).Times(1);
104  pipeline->readException(make_exception_wrapper<std::runtime_error>("blah"));
105 
106  EXPECT_CALL(handler2, write_(_, _)).WillOnce(FireWrite());
107  EXPECT_CALL(handler1, write_(_, _)).Times(1);
108  EXPECT_NO_THROW(pipeline->write(1).value());
109 
110  EXPECT_CALL(handler2, close_(_)).WillOnce(FireClose());
111  EXPECT_CALL(handler1, close_(_)).Times(1);
112  EXPECT_NO_THROW(pipeline->close().value());
113 
114  {
115  InSequence sequence;
116  EXPECT_CALL(handler1, detachPipeline(_));
117  EXPECT_CALL(handler2, detachPipeline(_));
118  }
119 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
ReachEndOfPipeline   
)

Definition at line 123 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_NO_THROW, and handler().

123  {
125  EXPECT_CALL(handler, attachPipeline(_));
126  auto pipeline = StaticPipeline<int, int, IntHandler>::create(&handler);
127 
128  EXPECT_CALL(handler, read_(_, _)).WillOnce(FireRead());
129  pipeline->read(1);
130 
131  EXPECT_CALL(handler, readEOF(_)).WillOnce(FireReadEOF());
132  pipeline->readEOF();
133 
134  EXPECT_CALL(handler, readException(_, _)).WillOnce(FireReadException());
135  pipeline->readException(make_exception_wrapper<std::runtime_error>("blah"));
136 
137  EXPECT_CALL(handler, write_(_, _)).WillOnce(FireWrite());
138  EXPECT_NO_THROW(pipeline->write(1).value());
139 
140  EXPECT_CALL(handler, close_(_)).WillOnce(FireClose());
141  EXPECT_NO_THROW(pipeline->close().value());
142 
143  EXPECT_CALL(handler, detachPipeline(_));
144 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
void handler(int, siginfo_t *, void *)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
TurnAround   
)

Definition at line 147 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

147  {
148  IntHandler handler1;
149  IntHandler2 handler2;
150 
151  {
152  InSequence sequence;
153  EXPECT_CALL(handler2, attachPipeline(_));
154  EXPECT_CALL(handler1, attachPipeline(_));
155  }
156 
158  &handler1, &handler2);
159 
160  EXPECT_CALL(handler1, read_(_, _)).WillOnce(FireRead());
161  EXPECT_CALL(handler2, read_(_, _)).WillOnce(FireWrite());
162  EXPECT_CALL(handler1, write_(_, _)).Times(1);
163  pipeline->read(1);
164 
165  {
166  InSequence sequence;
167  EXPECT_CALL(handler1, detachPipeline(_));
168  EXPECT_CALL(handler2, detachPipeline(_));
169  }
170 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
DynamicFireActions   
)

Definition at line 172 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_NO_THROW, and EXPECT_TRUE.

172  {
173  IntHandler handler1, handler2, handler3;
174  EXPECT_CALL(handler2, attachPipeline(_));
175  auto pipeline = StaticPipeline<int, int, IntHandler>::create(&handler2);
176 
177 
178  {
179  InSequence sequence;
180  EXPECT_CALL(handler3, attachPipeline(_));
181  EXPECT_CALL(handler1, attachPipeline(_));
182  }
183 
184  (*pipeline)
185  .addFront(&handler1)
186  .addBack(&handler3)
187  .finalize();
188 
189  EXPECT_TRUE(pipeline->getHandler<IntHandler>(0));
190  EXPECT_TRUE(pipeline->getHandler<IntHandler>(1));
191  EXPECT_TRUE(pipeline->getHandler<IntHandler>(2));
192 
193  EXPECT_CALL(handler1, read_(_, _)).WillOnce(FireRead());
194  EXPECT_CALL(handler2, read_(_, _)).WillOnce(FireRead());
195  EXPECT_CALL(handler3, read_(_, _)).Times(1);
196  pipeline->read(1);
197 
198  EXPECT_CALL(handler3, write_(_, _)).WillOnce(FireWrite());
199  EXPECT_CALL(handler2, write_(_, _)).WillOnce(FireWrite());
200  EXPECT_CALL(handler1, write_(_, _)).Times(1);
201  EXPECT_NO_THROW(pipeline->write(1).value());
202 
203  {
204  InSequence sequence;
205  EXPECT_CALL(handler1, detachPipeline(_));
206  EXPECT_CALL(handler2, detachPipeline(_));
207  EXPECT_CALL(handler3, detachPipeline(_));
208  }
209 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
DynamicAttachDetachOrder   
)

Definition at line 211 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

211  {
212  IntHandler handler1, handler2;
213  auto pipeline = Pipeline<int, int>::create();
214  {
215  InSequence sequence;
216  EXPECT_CALL(handler2, attachPipeline(_));
217  EXPECT_CALL(handler1, attachPipeline(_));
218  }
219  (*pipeline)
220  .addBack(&handler1)
221  .addBack(&handler2)
222  .finalize();
223  {
224  InSequence sequence;
225  EXPECT_CALL(handler1, detachPipeline(_));
226  EXPECT_CALL(handler2, detachPipeline(_));
227  }
228 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
GetContext   
)

Definition at line 230 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_TRUE, and handler().

230  {
232  EXPECT_CALL(handler, attachPipeline(_));
233  auto pipeline = StaticPipeline<int, int, IntHandler>::create(&handler);
234  EXPECT_TRUE(handler.getContext());
235  EXPECT_CALL(handler, detachPipeline(_));
236 }
void handler(int, siginfo_t *, void *)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( PipelineTest  ,
HandlerInMultiplePipelines   
)

Definition at line 238 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_FALSE, and handler().

238  {
240  EXPECT_CALL(handler, attachPipeline(_)).Times(2);
241  auto pipeline1 = StaticPipeline<int, int, IntHandler>::create(&handler);
242  auto pipeline2 = StaticPipeline<int, int, IntHandler>::create(&handler);
243  EXPECT_FALSE(handler.getContext());
244  EXPECT_CALL(handler, detachPipeline(_)).Times(2);
245 }
void handler(int, siginfo_t *, void *)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( PipelineTest  ,
HandlerInPipelineTwice   
)

Definition at line 247 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_FALSE, and handler().

247  {
248  auto handler = std::make_shared<IntHandler>();
249  EXPECT_CALL(*handler, attachPipeline(_)).Times(2);
250  auto pipeline = Pipeline<int, int>::create();
251  pipeline->addBack(handler);
252  pipeline->addBack(handler);
253  pipeline->finalize();
254  EXPECT_FALSE(handler->getContext());
255  EXPECT_CALL(*handler, detachPipeline(_)).Times(2);
256 }
void handler(int, siginfo_t *, void *)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( PipelineTest  ,
NoDetachOnOwner   
)

Definition at line 258 of file PipelineTest.cpp.

References testing::_, EXPECT_CALL, and handler().

258  {
260  EXPECT_CALL(handler, attachPipeline(_));
261  auto pipeline = StaticPipeline<int, int, IntHandler>::create(&handler);
262  pipeline->setOwner(&handler);
263 }
void handler(int, siginfo_t *, void *)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( Pipeline  ,
MissingInboundOrOutbound   
)

Definition at line 277 of file PipelineTest.cpp.

References EXPECT_THROW.

277  {
278  auto pipeline = Pipeline<int, int>::create();
279  (*pipeline)
281  .finalize();
282  EXPECT_THROW(pipeline->read(0), std::invalid_argument);
283  EXPECT_THROW(pipeline->readEOF(), std::invalid_argument);
284  EXPECT_THROW(
285  pipeline->readException(exception_wrapper(std::runtime_error("blah"))),
286  std::invalid_argument);
287  EXPECT_THROW(pipeline->write(0), std::invalid_argument);
288  EXPECT_THROW(pipeline->close(), std::invalid_argument);
289 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST ( Pipeline  ,
DynamicConstruction   
)

Definition at line 291 of file PipelineTest.cpp.

References EXPECT_NO_THROW.

291  {
292  {
294  pipeline->addBack(StringHandler());
295  pipeline->addBack(StringHandler());
296 
297  // Exercise both addFront and addBack. Final pipeline is
298  // StI <-> ItS <-> StS <-> StS <-> StI <-> ItS
300  (*pipeline)
301  .addFront(IntToStringHandler{})
302  .addFront(StringToIntHandler{})
303  .addBack(StringToIntHandler{})
304  .addBack(IntToStringHandler{})
305  .finalize());
306  }
307 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
HandlerAdapter< std::string, std::string > StringHandler
TEST ( Pipeline  ,
RemovePointer   
)

Definition at line 309 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

309  {
310  IntHandler handler1, handler2;
311  EXPECT_CALL(handler1, attachPipeline(_));
312  EXPECT_CALL(handler2, attachPipeline(_));
313  auto pipeline = Pipeline<int, int>::create();
314  (*pipeline)
315  .addBack(&handler1)
316  .addBack(&handler2)
317  .finalize();
318 
319  EXPECT_CALL(handler1, detachPipeline(_));
320  (*pipeline)
321  .remove(&handler1)
322  .finalize();
323 
324  EXPECT_CALL(handler2, read_(_, _));
325  pipeline->read(1);
326 
327  EXPECT_CALL(handler2, detachPipeline(_));
328 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( Pipeline  ,
RemoveFront   
)

Definition at line 330 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

330  {
331  IntHandler handler1, handler2;
332  EXPECT_CALL(handler1, attachPipeline(_));
333  EXPECT_CALL(handler2, attachPipeline(_));
334  auto pipeline = Pipeline<int, int>::create();
335  (*pipeline)
336  .addBack(&handler1)
337  .addBack(&handler2)
338  .finalize();
339 
340  EXPECT_CALL(handler1, detachPipeline(_));
341  (*pipeline)
342  .removeFront()
343  .finalize();
344 
345  EXPECT_CALL(handler2, read_(_, _));
346  pipeline->read(1);
347 
348  EXPECT_CALL(handler2, detachPipeline(_));
349 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( Pipeline  ,
RemoveBack   
)

Definition at line 351 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

351  {
352  IntHandler handler1, handler2;
353  EXPECT_CALL(handler1, attachPipeline(_));
354  EXPECT_CALL(handler2, attachPipeline(_));
355  auto pipeline = Pipeline<int, int>::create();
356  (*pipeline)
357  .addBack(&handler1)
358  .addBack(&handler2)
359  .finalize();
360 
361  EXPECT_CALL(handler2, detachPipeline(_));
362  (*pipeline)
363  .removeBack()
364  .finalize();
365 
366  EXPECT_CALL(handler1, read_(_, _));
367  pipeline->read(1);
368 
369  EXPECT_CALL(handler1, detachPipeline(_));
370 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( Pipeline  ,
RemoveType   
)

Definition at line 372 of file PipelineTest.cpp.

References testing::_, and EXPECT_CALL.

372  {
373  IntHandler handler1;
374  IntHandler2 handler2;
375  EXPECT_CALL(handler1, attachPipeline(_));
376  EXPECT_CALL(handler2, attachPipeline(_));
377  auto pipeline = Pipeline<int, int>::create();
378  (*pipeline)
379  .addBack(&handler1)
380  .addBack(&handler2)
381  .finalize();
382 
383  EXPECT_CALL(handler1, detachPipeline(_));
384  (*pipeline)
385  .remove<IntHandler>()
386  .finalize();
387 
388  EXPECT_CALL(handler2, read_(_, _));
389  pipeline->read(1);
390 
391  EXPECT_CALL(handler2, detachPipeline(_));
392 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST ( Pipeline  ,
Concurrent   
)

Definition at line 397 of file PipelineTest.cpp.

References b, i, and folly::pushmi::detail::t.

397  {
398  NiceMock<MockHandlerAdapter<int, int>> handler1, handler2;
399  auto pipeline = Pipeline<int, int>::create();
400  (*pipeline)
401  .addBack(&handler1)
402  .addBack(&handler2)
403  .finalize();
404  boost::barrier b{2};
405  auto spam = [&]{
406  for (int i = 0; i < 100000; i++) {
407  b.wait();
408  pipeline->read(i);
409  }
410  };
411  std::thread t(spam);
412  spam();
413  t.join();
414 }
char b
TEST ( PipelineTest  ,
NumHandler   
)

Definition at line 416 of file PipelineTest.cpp.

References EXPECT_EQ.

416  {
417  NiceMock<MockHandlerAdapter<int, int>> handler1, handler2;
418  auto pipeline = Pipeline<int, int>::create();
419  EXPECT_EQ(0, pipeline->numHandlers());
420 
421  pipeline->addBack(&handler1);
422  EXPECT_EQ(1, pipeline->numHandlers());
423 
424  pipeline->addBack(&handler2);
425  EXPECT_EQ(2, pipeline->numHandlers());
426 
427  pipeline->finalize();
428  EXPECT_EQ(2, pipeline->numHandlers());
429 
430  pipeline->remove(&handler1);
431  EXPECT_EQ(1, pipeline->numHandlers());
432 
433  pipeline->remove(&handler2);
434  EXPECT_EQ(0, pipeline->numHandlers());
435 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( PipelineTest  ,
HandlerReuse   
)

Definition at line 438 of file PipelineTest.cpp.

References ASSERT_EQ, and EXPECT_NE.

438  {
439  NiceMock<MockHandlerAdapter<int, int>> handler1, handler2, handler3;
440  auto pipeline1 = Pipeline<int, int>::create();
441 
442  // pipeline1 contains the first two handlers
443  (*pipeline1)
444  .addBack(&handler1)
445  .addBack(&handler2)
446  .finalize();
447  pipeline1->read(42);
448  EXPECT_NE(nullptr, handler2.getContext());
449 
450  // Close and detach the back handler (#2)
451  pipeline1->close();
452  pipeline1->removeBack();
453  ASSERT_EQ(nullptr, handler2.getContext());
454 
455  auto pipeline2 = Pipeline<int, int>::create();
456  (*pipeline2)
457  .addBack(&handler2)
458  .addBack(&handler3)
459  .finalize();
460  pipeline2->read(24);
461  EXPECT_NE(nullptr, handler2.getContext());
462 
463  // detach both
464  pipeline2->remove(&handler2);
465  pipeline2->remove(&handler3);
466  ASSERT_EQ(nullptr, handler2.getContext());
467  ASSERT_EQ(nullptr, handler3.getContext());
468 
469  auto pipeline3 = Pipeline<int, int>::create();
470  (*pipeline3)
471  .addBack(&handler2)
472  .addBack(&handler3)
473  .finalize();
474  pipeline3->read(1);
475  EXPECT_NE(nullptr, handler2.getContext());
476 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926