proxygen
EventBaseLocalTest.cpp File Reference

Go to the source code of this file.

Classes

class  Foo
 

Typedefs

using IntPtr = std::unique_ptr< int >
 

Functions

 TEST (EventBaseLocalTest, Basic)
 
 TEST (EventBaseLocalTest, getOrCreate)
 
 TEST (EventBaseLocalTest, getOrCreateNoncopyable)
 
 TEST (EventBaseLocalTest, emplaceNoncopyable)
 

Typedef Documentation

using IntPtr = std::unique_ptr<int>

Definition at line 75 of file EventBaseLocalTest.cpp.

Function Documentation

TEST ( EventBaseLocalTest  ,
Basic   
)

Definition at line 31 of file EventBaseLocalTest.cpp.

References folly::EventBaseLocal< T >::emplace(), folly::detail::EventBaseLocalBase::erase(), EXPECT_EQ, testing::gmock_matchers_test::foo, folly::EventBaseLocal< T >::get(), and folly::EventBase::loop().

31  {
32  int dtorCnt = 0;
33  folly::EventBase evb1;
34 
35  {
37 
38  EXPECT_EQ(foo.get(evb1), nullptr);
39 
40  foo.emplace(evb1, new Foo(5, [&]() { ++dtorCnt; }));
41 
42  EXPECT_EQ(foo.get(evb1)->n, 5);
43 
44  {
45  folly::EventBase evb2;
46  foo.emplace(evb2, new Foo(6, [&]() { ++dtorCnt; }));
47  EXPECT_EQ(foo.get(evb2)->n, 6);
48  foo.erase(evb2);
49  EXPECT_EQ(dtorCnt, 1); // should dtor a Foo when we erase
50  EXPECT_EQ(foo.get(evb2), nullptr);
51  foo.emplace(evb2, 7, [&]() { ++dtorCnt; });
52  EXPECT_EQ(foo.get(evb2)->n, 7);
53  }
54 
55  EXPECT_EQ(dtorCnt, 2); // should dtor a Foo when evb2 destructs
56  }
57  EXPECT_EQ(dtorCnt, 2); // should schedule Foo destructor, when foo destructs
58  evb1.loop();
59  EXPECT_EQ(dtorCnt, 3); // Foo will be destroyed in EventBase loop
60 }
T * get(EventBase &evb)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void emplace(EventBase &evb, T *ptr)
TEST ( EventBaseLocalTest  ,
getOrCreate   
)

Definition at line 62 of file EventBaseLocalTest.cpp.

References folly::detail::EventBaseLocalBase::erase(), EXPECT_EQ, folly::EventBaseLocal< T >::getOrCreate(), and folly::EventBaseLocal< T >::getOrCreateFn().

62  {
63  folly::EventBase evb1;
65 
66  EXPECT_EQ(ints.getOrCreate(evb1), 0);
67  EXPECT_EQ(ints.getOrCreate(evb1, 5), 0);
68 
69  folly::EventBase evb2;
70  EXPECT_EQ(ints.getOrCreate(evb2, 5), 5);
71  ints.erase(evb2);
72  EXPECT_EQ(4, ints.getOrCreateFn(evb2, []() { return new int(4); }));
73 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
T & getOrCreateFn(EventBase &evb, Func fn)
T & getOrCreate(EventBase &evb, Args &&...args)
TEST ( EventBaseLocalTest  ,
getOrCreateNoncopyable   
)

Definition at line 77 of file EventBaseLocalTest.cpp.

References EXPECT_EQ, and folly::EventBaseLocal< T >::getOrCreate().

77  {
78  folly::EventBase evb1;
80 
81  EXPECT_EQ(ints.getOrCreate(evb1), IntPtr());
82  EXPECT_EQ(ints.getOrCreate(evb1, std::make_unique<int>(5)), IntPtr());
83 
84  folly::EventBase evb2;
85  EXPECT_EQ(*ints.getOrCreate(evb2, std::make_unique<int>(5)), 5);
86 }
std::unique_ptr< int > IntPtr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
T & getOrCreate(EventBase &evb, Args &&...args)
TEST ( EventBaseLocalTest  ,
emplaceNoncopyable   
)

Definition at line 88 of file EventBaseLocalTest.cpp.

References folly::EventBaseLocal< T >::emplace(), EXPECT_EQ, and folly::EventBaseLocal< T >::get().

88  {
89  folly::EventBase evb;
91  ints.emplace(evb, std::make_unique<int>(42));
92  EXPECT_EQ(42, **ints.get(evb));
93 }
T * get(EventBase &evb)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void emplace(EventBase &evb, T *ptr)