proxygen
EventBaseLocalTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-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  */
16 
19 
20 struct Foo {
21  Foo(int n_, std::function<void()> dtorFn_)
22  : n(n_), dtorFn(std::move(dtorFn_)) {}
23  ~Foo() {
24  dtorFn();
25  }
26 
27  int n;
28  std::function<void()> dtorFn;
29 };
30 
31 TEST(EventBaseLocalTest, Basic) {
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 }
61 
62 TEST(EventBaseLocalTest, getOrCreate) {
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 }
74 
75 using IntPtr = std::unique_ptr<int>;
76 
77 TEST(EventBaseLocalTest, getOrCreateNoncopyable) {
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 }
87 
88 TEST(EventBaseLocalTest, emplaceNoncopyable) {
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)
std::unique_ptr< int > IntPtr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void emplace(EventBase &evb, T *ptr)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
T & getOrCreateFn(EventBase &evb, Func fn)
TEST(EventBaseLocalTest, Basic)
std::function< void()> dtorFn
T & getOrCreate(EventBase &evb, Args &&...args)
Foo(int n_, std::function< void()> dtorFn_)