proxygen
ExecutorTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 <atomic>
18 
19 #include <folly/Executor.h>
21 
22 namespace folly {
23 
25  public:
26  void add(Func) override {
27  // this executor does nothing
28  }
29 
30  bool keepAliveAcquire() override {
31  ++refCount;
32  return true;
33  }
34 
35  void keepAliveRelease() override {
36  --refCount;
37  }
38 
39  std::atomic<int> refCount{0};
40 };
41 
42 TEST(ExecutorTest, KeepAliveBasic) {
44 
45  {
46  auto ka = getKeepAliveToken(exec);
47  EXPECT_TRUE(ka);
48  EXPECT_EQ(&exec, ka.get());
49  EXPECT_EQ(1, exec.refCount);
50  }
51 
52  EXPECT_EQ(0, exec.refCount);
53 }
54 
55 TEST(ExecutorTest, KeepAliveMove) {
57 
58  {
59  auto ka = getKeepAliveToken(exec);
60  EXPECT_TRUE(ka);
61  EXPECT_EQ(&exec, ka.get());
62  EXPECT_EQ(1, exec.refCount);
63 
64  auto ka2 = std::move(ka);
65  EXPECT_FALSE(ka);
66  EXPECT_TRUE(ka2);
67  EXPECT_EQ(&exec, ka2.get());
68  EXPECT_EQ(1, exec.refCount);
69  }
70 
71  EXPECT_EQ(0, exec.refCount);
72 }
73 
74 TEST(ExecutorTest, KeepAliveConvert) {
76 
77  {
78  auto ka = getKeepAliveToken(exec);
79  EXPECT_TRUE(ka);
80  EXPECT_EQ(&exec, ka.get());
81  EXPECT_EQ(1, exec.refCount);
82 
83  Executor::KeepAlive<Executor> ka2 = std::move(ka); // conversion
84  EXPECT_FALSE(ka);
85  EXPECT_TRUE(ka2);
86  EXPECT_EQ(&exec, ka2.get());
87  EXPECT_EQ(1, exec.refCount);
88  }
89 
90  EXPECT_EQ(0, exec.refCount);
91 }
92 
93 TEST(ExecutorTest, KeepAliveCopy) {
95 
96  {
97  auto ka = getKeepAliveToken(exec);
98  EXPECT_TRUE(ka);
99  EXPECT_EQ(&exec, ka.get());
100  EXPECT_EQ(1, exec.refCount);
101 
102  auto ka2 = ka.copy();
103  EXPECT_TRUE(ka);
104  EXPECT_TRUE(ka2);
105  EXPECT_EQ(&exec, ka2.get());
106  EXPECT_EQ(2, exec.refCount);
107  }
108 
109  EXPECT_EQ(0, exec.refCount);
110 }
111 
112 } // namespace folly
ExecutorT * get() const
Definition: Executor.h:100
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void keepAliveRelease() override
void add(Func) override
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::atomic< int > refCount
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool keepAliveAcquire() override
TEST(SequencedExecutor, CPUThreadPoolExecutor)