Fix SimpleBarrier
[folly.git] / folly / test / SingletonTestGlobal.cpp
1 /*
2  * Copyright 2016 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/Singleton.h>
21 #include <folly/Benchmark.h>
22 #include <folly/test/SingletonTestStructs.h>
23 #include <folly/portability/GTest.h>
24
25 #include <glog/logging.h>
26
27 /*
28  * This test needs to be in its own file, as a standalone program.
29  * We want to ensure no other singletons are registered, so we can
30  * rely on some expectations about registered and living counts, etc.
31  * All other tests should go in `SingletonTest.cpp`.
32  */
33
34 using namespace folly;
35
36 namespace {
37 Singleton<GlobalWatchdog> global_watchdog;
38 }
39
40 // Test basic global usage (the default way singletons will generally
41 // be used).
42 TEST(Singleton, BasicGlobalUsage) {
43   EXPECT_EQ(Watchdog::creation_order().size(), 0);
44   EXPECT_EQ(SingletonVault::singleton()->registeredSingletonCount(), 1);
45   EXPECT_EQ(SingletonVault::singleton()->livingSingletonCount(), 0);
46
47   {
48     std::shared_ptr<GlobalWatchdog> wd1 = Singleton<GlobalWatchdog>::try_get();
49     EXPECT_NE(wd1, nullptr);
50     EXPECT_EQ(Watchdog::creation_order().size(), 1);
51     std::shared_ptr<GlobalWatchdog> wd2 = Singleton<GlobalWatchdog>::try_get();
52     EXPECT_NE(wd2, nullptr);
53     EXPECT_EQ(wd1.get(), wd2.get());
54     EXPECT_EQ(Watchdog::creation_order().size(), 1);
55   }
56
57   SingletonVault::singleton()->destroyInstances();
58   EXPECT_EQ(Watchdog::creation_order().size(), 0);
59 }