proxygen
GenericFilterTest.cpp File Reference
#include <deque>
#include <folly/Memory.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
#include <map>
#include <proxygen/lib/utils/FilterChain.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

class  TesterInterface
 
class  TesterInterface::Callback
 
class  MockTester
 
class  MockTesterCallback
 
class  TestFilter< Owned >
 
class  TestFilterNoCallback< Owned >
 
class  TestFilterNoCall< Owned >
 
class  TestFilterNoCallbackNoCall< Owned >
 
class  GenericFilterTest< Owned >
 
class  TestFilterOddDeleteDo
 
class  TestFilterOddDeleteOn< Owned >
 

Namespaces

 detail
 

Typedefs

using OwnedGenericFilterTest = GenericFilterTest< true >
 
using UnownedGenericFilterTest = GenericFilterTest< false >
 

Functions

template<typename T >
Tdetail::get_pointer (const unique_ptr< T > &ptr)
 
template<typename T >
Tdetail::get_pointer (T *ptr)
 
template<bool Owned>
std::enable_if< Owned, unique_ptr< MockTester > >::type getTester ()
 
template<bool Owned>
std::enable_if<!Owned, MockTester * >::type getTester ()
 
 TEST_F (OwnedGenericFilterTest, EmptyChain)
 
 TEST_F (OwnedGenericFilterTest, SingleElemChain)
 
 TEST_F (OwnedGenericFilterTest, MultiElemChain)
 
 TEST_F (OwnedGenericFilterTest, MultiElemMultiAdd)
 
 TEST_F (OwnedGenericFilterTest, Wants)
 
 TEST_F (OwnedGenericFilterTest, WantsMultiAdd)
 
 TEST_F (OwnedGenericFilterTest, WantsMultiAddHard)
 
 TEST_F (OwnedGenericFilterTest, ChangeCallback)
 
 TEST_F (UnownedGenericFilterTest, All)
 
 TEST_F (OwnedGenericFilterTest, SetNullCb)
 
 TEST_F (UnownedGenericFilterTest, DeleteDo)
 
 TEST_F (UnownedGenericFilterTest, DeleteOn)
 
 TEST_F (OwnedGenericFilterTest, DeleteChain)
 
 TEST_F (OwnedGenericFilterTest, GetChainEnd)
 
 TEST_F (OwnedGenericFilterTest, SetDestination)
 
 TEST_F (OwnedGenericFilterTest, Foreach)
 

Typedef Documentation

Definition at line 241 of file GenericFilterTest.cpp.

Definition at line 242 of file GenericFilterTest.cpp.

Function Documentation

template<bool Owned>
std::enable_if<Owned, unique_ptr<MockTester> >::type getTester ( )

Definition at line 122 of file GenericFilterTest.cpp.

122  {
123  return std::make_unique<MockTester>();
124 }
template<bool Owned>
std::enable_if<!Owned, MockTester*>::type getTester ( )

Definition at line 127 of file GenericFilterTest.cpp.

127  {
128  return new MockTester();
129 }
TEST_F ( OwnedGenericFilterTest  ,
EmptyChain   
)

Definition at line 244 of file GenericFilterTest.cpp.

References folly::basicTest().

244  {
245  basicTest();
246 }
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
SingleElemChain   
)

Definition at line 248 of file GenericFilterTest.cpp.

References folly::basicTest(), EXPECT_EQ, folly::pushmi::operators::filter, and folly::gen::move.

248  {
249  auto filterUnique = std::make_unique<TestFilter<true>>();
250  auto filter = filterUnique.get();
251  chain().addFilters(std::move(filterUnique));
252  EXPECT_EQ(filter->do_, 0);
253  EXPECT_EQ(filter->on_, 0);
254  basicTest();
255  EXPECT_EQ(filter->do_, 1);
256  EXPECT_EQ(filter->on_, 1);
257 }
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
MultiElemChain   
)

Definition at line 259 of file GenericFilterTest.cpp.

References folly::basicTest(), TestFilter< Owned >::do_, EXPECT_EQ, folly::gen::move, and TestFilter< Owned >::on_.

259  {
260  auto f1 = std::make_unique<TestFilter<true>>();
261  auto f2 = std::make_unique<TestFilter<true>>();
262  auto f3 = std::make_unique<TestFilter<true>>();
263  TestFilter<true>* fp1 = f1.get();
264  TestFilter<true>* fp2 = f2.get();
265  TestFilter<true>* fp3 = f3.get();
266  chain().addFilters(std::move(f1), std::move(f2), std::move(f3));
267  basicTest();
268  EXPECT_EQ(fp1->do_, 1);
269  EXPECT_EQ(fp1->on_, 1);
270  EXPECT_EQ(fp2->do_, 1);
271  EXPECT_EQ(fp2->on_, 1);
272  EXPECT_EQ(fp3->do_, 1);
273  EXPECT_EQ(fp3->on_, 1);
274 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
MultiElemMultiAdd   
)

Definition at line 276 of file GenericFilterTest.cpp.

References folly::basicTest(), EXPECT_EQ, folly::pushmi::operators::filter, i, and folly::gen::move.

276  {
277  std::deque<TestFilter<true>*> filters;
278  for (unsigned i = 0; i < 10; ++i) {
279  auto filter = std::make_unique<TestFilter<true>>();
280  filters.push_back(filter.get());
281  chain().addFilters(std::move(filter));
282  }
283  basicTest();
284  for (auto filter: filters) {
285  EXPECT_EQ(filter->do_, 1);
286  EXPECT_EQ(filter->on_, 1);
287  }
288 }
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
Wants   
)

Definition at line 290 of file GenericFilterTest.cpp.

References folly::basicTest(), TestFilter< Owned >::do_, EXPECT_EQ, folly::gen::move, and TestFilter< Owned >::on_.

290  {
291  auto f1 = std::make_unique<TestFilter<true>>();
292  auto f2 = std::make_unique<TestFilterNoCallback<true>>();
293  auto f3 = std::make_unique<TestFilterNoCall<true>>();
294  auto f4 = std::make_unique<TestFilterNoCallbackNoCall<true>>();
295  TestFilter<true>* fp1 = f1.get();
296  TestFilter<true>* fp2 = f2.get();
297  TestFilter<true>* fp3 = f3.get();
298  TestFilter<true>* fp4 = f4.get();
299  chain().addFilters(std::move(f1), std::move(f2),
300  std::move(f3), std::move(f4));
301  basicTest();
302  EXPECT_EQ(fp1->do_, 1);
303  EXPECT_EQ(fp1->on_, 1);
304  // Only calls
305  EXPECT_EQ(fp2->do_, 1);
306  EXPECT_EQ(fp2->on_, 0);
307  // Only callbacks
308  EXPECT_EQ(fp3->do_, 0);
309  EXPECT_EQ(fp3->on_, 1);
310  // No callbacks or calls
311  EXPECT_EQ(fp4->do_, 0);
312  EXPECT_EQ(fp4->on_, 0);
313 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
WantsMultiAdd   
)

Definition at line 315 of file GenericFilterTest.cpp.

References folly::basicTest(), TestFilter< Owned >::do_, EXPECT_EQ, folly::gen::move, and TestFilter< Owned >::on_.

315  {
316  auto f1 = std::make_unique<TestFilterNoCallback<true>>();
317  auto f2 = std::make_unique<TestFilterNoCall<true>>();
318  TestFilter<true>* fp1 = f1.get();
319  TestFilter<true>* fp2 = f2.get();
320  chain().addFilters(std::move(f1));
321  basicTest();
322 
323  EXPECT_EQ(fp1->do_, 1);
324  EXPECT_EQ(fp1->on_, 0);
325  EXPECT_EQ(fp2->do_, 0);
326  EXPECT_EQ(fp2->on_, 0);
327 
328  chain().addFilters(std::move(f2));
329  basicTest();
330 
331  EXPECT_EQ(fp1->do_, 2);
332  EXPECT_EQ(fp1->on_, 0);
333  EXPECT_EQ(fp2->do_, 0);
334  EXPECT_EQ(fp2->on_, 1);
335 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
TEST_F ( OwnedGenericFilterTest  ,
WantsMultiAddHard   
)

Definition at line 337 of file GenericFilterTest.cpp.

References EXPECT_EQ, f, and i.

337  {
338  const unsigned NUM_FILTERS = 5000;
339  auto filters = getRandomFilters(NUM_FILTERS);
340  // Now check the counts on each filter. Filters are pushed to the front
341  // of the chain, so filters towards the front have low call/callback counts
342  for (unsigned i = 0; i < NUM_FILTERS; ++i) {
343  auto f = filters[i];
344  if (f->kWantsCalls_) {
345  EXPECT_EQ(f->do_, i + 1);
346  } else {
347  EXPECT_EQ(f->do_, 0);
348  }
349  if (f->kWantsCallbacks_) {
350  EXPECT_EQ(f->on_, i + 1);
351  } else {
352  EXPECT_EQ(f->on_, 0);
353  }
354  }
355 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( OwnedGenericFilterTest  ,
ChangeCallback   
)

Definition at line 357 of file GenericFilterTest.cpp.

References folly::basicTest(), TestFilter< Owned >::do_, EXPECT_CALL, EXPECT_EQ, f, folly::gen::move, and TestFilter< Owned >::on_.

357  {
358  // The call-only filter in the chain doesn't want callbacks, so doing
359  // chain()->setCallback() is an error! Instead, must use chain().setCallback()
360  auto f = std::make_unique<TestFilterNoCallback<true>>();
361  MockTesterCallback callback2;
362 
363  TestFilter<true>* fp = f.get();
364  chain().addFilters(std::move(f));
365  basicTest();
366 
367  EXPECT_EQ(fp->do_, 1);
368  EXPECT_EQ(fp->on_, 0);
369 
370  chain().setCallback(&callback2);
371  EXPECT_EQ(actor_->cb_, &callback2);
372  EXPECT_CALL(callback2, onA());
373  actor_->cb_->onA();
374 
375  EXPECT_EQ(fp->on_, 0);
376 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void basicTest()
#define EXPECT_CALL(obj, call)
TEST_F ( UnownedGenericFilterTest  ,
All   
)

Definition at line 378 of file GenericFilterTest.cpp.

References EXPECT_EQ, f, and i.

378  {
379  const unsigned NUM_FILTERS = 5000;
380  auto filters = getRandomFilters(NUM_FILTERS);
381  // Now check the counts on each filter
382  unsigned i = 0;
383  for (auto f: filters) {
384  if (f->kWantsCalls_) {
385  EXPECT_EQ(f->do_, i + 1);
386  } else {
387  EXPECT_EQ(f->do_, 0);
388  }
389  if (f->kWantsCallbacks_) {
390  EXPECT_EQ(f->on_, i + 1);
391  } else {
392  EXPECT_EQ(f->on_, 0);
393  }
394  delete f;
395  ++i;
396  }
397  delete actor_;
398 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( OwnedGenericFilterTest  ,
SetNullCb   
)

Definition at line 400 of file GenericFilterTest.cpp.

References ASSERT_EQ, and f.

400  {
401  // Some objects have a special behavior when the callback is set to
402  // nullptr. So in this case, we need to make sure it propagates
403  auto filters = getRandomFilters(100);
404  chain().setCallback(nullptr);
405  CHECK(nullptr == actor_->cb_);
406 
407  testFilters(filters, nullptr);
408 
409  MockTesterCallback head;
410  chain().setCallback(&head);
411 
412  testFilters(filters, &head);
413 
414  TesterInterface::Callback* cb = &head;
415  for (auto f: filters) {
416  if (f->kWantsCallbacks_) {
417  cb = f;
418  }
419  }
420  // The actor's callback should be the last filter in the chain that
421  // wants callbacks
422  ASSERT_EQ(actor_->cb_, cb);
423 }
auto f
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F ( UnownedGenericFilterTest  ,
DeleteDo   
)

Definition at line 446 of file GenericFilterTest.cpp.

References folly::basicTest(), EXPECT_CALL, EXPECT_EQ, and i.

446  {
447  // Test where a filter in the middle of the chain deletes itself early
448  int deletions = 0;
449 
450  for (unsigned i = 0; i < 4; ++i) {
451  chain().addFilters(new TestFilterOddDeleteDo(&deletions));
452  }
453 
454  for (unsigned i = 0; i < 2; ++i) {
455  // First time around, the odd id's get deleted
456  // Second time should just forward the calls normally
457  EXPECT_CALL(*actor_, doA());
458  chain()->doA();
459  EXPECT_EQ(deletions, (i + 1) * 2);
460  }
461  basicTest();
462  delete actor_;
463 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void basicTest()
#define EXPECT_CALL(obj, call)
TEST_F ( UnownedGenericFilterTest  ,
DeleteOn   
)

Definition at line 485 of file GenericFilterTest.cpp.

References folly::basicTest(), callback_, EXPECT_CALL, EXPECT_EQ, and i.

485  {
486  // Test where a filter in the middle of the chain deletes itself early
487  int deletions = 0;
488 
489  for (unsigned i = 0; i < 4; ++i) {
490  chain().addFilters(new TestFilterOddDeleteOn<>(&deletions));
491  }
492 
493  for (unsigned i = 0; i < 2; ++i) {
494  // First time around, the odd id's get deleted
495  // Second time should just forward the calls normally
496  EXPECT_CALL(callback_, onA());
497  actor_->cb_->onA();
498  EXPECT_EQ(deletions, (i + 1) * 2);
499  }
500  basicTest();
501  delete actor_;
502 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void basicTest()
#define EXPECT_CALL(obj, call)
folly::Function< void()> callback_
TEST_F ( OwnedGenericFilterTest  ,
DeleteChain   
)

Definition at line 504 of file GenericFilterTest.cpp.

References EXPECT_EQ, i, and folly::make_unique().

504  {
505  // Add some filters to the chain and reset the chain. Make sure all the
506  // filters are deleted.
507  const unsigned NUM_FILTERS = 1000;
508  int deletions = 0;
509  for (unsigned i = 0; i < NUM_FILTERS; ++i) {
510  chain().addFilters(
512  }
513  chain_.reset();
514  EXPECT_EQ(deletions, NUM_FILTERS);
515 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
TEST_F ( OwnedGenericFilterTest  ,
GetChainEnd   
)

Definition at line 517 of file GenericFilterTest.cpp.

References EXPECT_EQ, and i.

517  {
518  for (unsigned i = 1; i < 100; ++i) {
519  auto filters = getRandomFilters(i);
520  EXPECT_EQ(actor_, &chain().getChainEnd());
521  }
522 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( OwnedGenericFilterTest  ,
SetDestination   
)

Definition at line 524 of file GenericFilterTest.cpp.

References EXPECT_CALL, and folly::gen::move.

524  {
525  auto filters = getRandomFilters(20);
526  EXPECT_CALL(*actor_, doA());
527  chain()->doA();
528  auto tester2 = getTester<true>();
529  actor_ = tester2.get();
530  auto oldTester = chain().setDestination(std::move(tester2));
531  EXPECT_CALL(*actor_, doA());
532  chain()->doA();
533 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
TEST_F ( OwnedGenericFilterTest  ,
Foreach   
)

Definition at line 535 of file GenericFilterTest.cpp.

References count, EXPECT_EQ, folly::pushmi::operators::filter, and TesterInterface::setCallback().

535  {
536  auto filters = getRandomFilters(20);
537  size_t count = 0;
538  chain().foreach([&count] (GenericFilter<TesterInterface,
541  std::default_delete<TesterInterface> >* filter) {
542  if (dynamic_cast<TestFilter<true>*>(filter)) {
543  count++;
544  }
545  });
546  EXPECT_EQ(count, 20);
547 }
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
virtual void setCallback(Callback *cb)=0
int * count