proxygen
SingletonTestGlobal.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 
17 #include <memory>
18 #include <vector>
19 
20 #include <folly/Benchmark.h>
21 #include <folly/Singleton.h>
24 
25 /*
26  * This test needs to be in its own file, as a standalone program.
27  * We want to ensure no other singletons are registered, so we can
28  * rely on some expectations about registered and living counts, etc.
29  * All other tests should go in `SingletonTest.cpp`.
30  */
31 
32 using namespace folly;
33 
34 namespace {
35 Singleton<GlobalWatchdog> global_watchdog;
36 } // namespace
37 
38 // Test basic global usage (the default way singletons will generally
39 // be used).
40 TEST(Singleton, BasicGlobalUsage) {
42  EXPECT_GE(SingletonVault::singleton()->registeredSingletonCount(), 1);
43  EXPECT_EQ(SingletonVault::singleton()->livingSingletonCount(), 0);
44 
45  {
46  std::shared_ptr<GlobalWatchdog> wd1 = Singleton<GlobalWatchdog>::try_get();
47  EXPECT_NE(wd1, nullptr);
49  std::shared_ptr<GlobalWatchdog> wd2 = Singleton<GlobalWatchdog>::try_get();
50  EXPECT_NE(wd2, nullptr);
51  EXPECT_EQ(wd1.get(), wd2.get());
53  }
54 
57 }
static SingletonVault * singleton()
Definition: Singleton.h:495
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static std::vector< Watchdog * > & creation_order()
static std::shared_ptr< T > try_get()
Definition: Singleton.h:598
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST(SequencedExecutor, CPUThreadPoolExecutor)