Fix RequestContext held too long issue in EventBase
[folly.git] / folly / io / async / test / EventBaseLocalTest.cpp
1 /*
2  * Copyright 2004-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
17 #include <folly/io/async/EventBaseLocal.h>
18 #include <folly/portability/GTest.h>
19
20 struct Foo {
21   Foo(int n, std::function<void()> dtorFn):
22     n(n), dtorFn(std::move(dtorFn)) {}
23   ~Foo() { dtorFn(); }
24
25   int n;
26   std::function<void()> dtorFn;
27 };
28
29 TEST(EventBaseLocalTest, Basic) {
30   int dtorCnt = 0;
31   folly::EventBase evb1;
32
33   {
34     folly::EventBaseLocal<Foo> foo;
35
36     EXPECT_EQ(foo.get(evb1), nullptr);
37
38     foo.emplace(evb1, new Foo(5, [&] () { ++dtorCnt; }));
39
40     EXPECT_EQ(foo.get(evb1)->n, 5);
41
42     {
43       folly::EventBase evb2;
44       foo.emplace(evb2, new Foo(6, [&] () { ++dtorCnt; }));
45       EXPECT_EQ(foo.get(evb2)->n, 6);
46       foo.erase(evb2);
47       EXPECT_EQ(dtorCnt, 1); // should dtor a Foo when we erase
48       EXPECT_EQ(foo.get(evb2), nullptr);
49       foo.emplace(evb2, 7, [&] () { ++dtorCnt; });
50       EXPECT_EQ(foo.get(evb2)->n, 7);
51     }
52
53     EXPECT_EQ(dtorCnt, 2); // should dtor a Foo when evb2 destructs
54
55   }
56   EXPECT_EQ(dtorCnt, 2); // should schedule Foo destructor, when foo destructs
57   evb1.loop();
58   EXPECT_EQ(dtorCnt, 3); // Foo will be destroyed in EventBase loop
59 }
60
61 TEST(EventBaseLocalTest, getOrCreate) {
62   folly::EventBase evb1;
63   folly::EventBaseLocal<int> ints;
64
65   EXPECT_EQ(ints.getOrCreate(evb1), 0);
66   EXPECT_EQ(ints.getOrCreate(evb1, 5), 0);
67
68   folly::EventBase evb2;
69   EXPECT_EQ(ints.getOrCreate(evb2, 5), 5);
70   ints.erase(evb2);
71   auto creator = []() { return new int(4); };
72   EXPECT_EQ(ints.getOrCreateFn(evb2, creator), 4);
73 }
74
75 using IntPtr = std::unique_ptr<int>;
76
77 TEST(EventBaseLocalTest, getOrCreateNoncopyable) {
78   folly::EventBase evb1;
79   folly::EventBaseLocal<IntPtr> ints;
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;
90   folly::EventBaseLocal<IntPtr> ints;
91   ints.emplace(evb, std::make_unique<int>(42));
92   EXPECT_EQ(42, **ints.get(evb));
93 }