2017
[folly.git] / folly / io / async / test / EventBaseLocalTest.cpp
1 /*
2  * Copyright 2017 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 /*
17  * Licensed to the Apache Software Foundation (ASF) under one
18  * or more contributor license agreements. See the NOTICE file
19  * distributed with this work for additional information
20  * regarding copyright ownership. The ASF licenses this file
21  * to you under the Apache License, Version 2.0 (the
22  * "License"); you may not use this file except in compliance
23  * with the License. You may obtain a copy of the License at
24  *
25  *   http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing,
28  * software distributed under the License is distributed on an
29  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
30  * KIND, either express or implied. See the License for the
31  * specific language governing permissions and limitations
32  * under the License.
33  */
34
35 #include <folly/io/async/EventBaseLocal.h>
36 #include <folly/portability/GTest.h>
37
38 struct Foo {
39   Foo(int n, std::function<void()> dtorFn):
40     n(n), dtorFn(std::move(dtorFn)) {}
41   ~Foo() { dtorFn(); }
42
43   int n;
44   std::function<void()> dtorFn;
45 };
46
47 TEST(EventBaseLocalTest, Basic) {
48   int dtorCnt = 0;
49   folly::EventBase evb1;
50
51   {
52     folly::EventBaseLocal<Foo> foo;
53
54     EXPECT_EQ(foo.get(evb1), nullptr);
55
56     foo.emplace(evb1, new Foo(5, [&] () { ++dtorCnt; }));
57
58     EXPECT_EQ(foo.get(evb1)->n, 5);
59
60     {
61       folly::EventBase evb2;
62       foo.emplace(evb2, new Foo(6, [&] () { ++dtorCnt; }));
63       EXPECT_EQ(foo.get(evb2)->n, 6);
64       foo.erase(evb2);
65       EXPECT_EQ(dtorCnt, 1); // should dtor a Foo when we erase
66       EXPECT_EQ(foo.get(evb2), nullptr);
67       foo.emplace(evb2, 7, [&] () { ++dtorCnt; });
68       EXPECT_EQ(foo.get(evb2)->n, 7);
69     }
70
71     EXPECT_EQ(dtorCnt, 2); // should dtor a Foo when evb2 destructs
72
73   }
74   EXPECT_EQ(dtorCnt, 3); // should dtor a Foo when foo destructs
75 }
76
77 TEST(EventBaseLocalTest, getOrCreate) {
78   folly::EventBase evb1;
79   folly::EventBaseLocal<int> ints;
80
81   EXPECT_EQ(ints.getOrCreate(evb1), 0);
82   EXPECT_EQ(ints.getOrCreate(evb1, 5), 0);
83
84   folly::EventBase evb2;
85   EXPECT_EQ(ints.getOrCreate(evb2, 5), 5);
86   ints.erase(evb2);
87   auto creator = []() { return new int(4); };
88   EXPECT_EQ(ints.getOrCreateFn(evb2, creator), 4);
89 }