Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / test / SingletonTestGlobal.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 #include <memory>
18 #include <vector>
19
20 #include <folly/Benchmark.h>
21 #include <folly/Singleton.h>
22 #include <folly/portability/GTest.h>
23 #include <folly/test/SingletonTestStructs.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) {
41   EXPECT_EQ(Watchdog::creation_order().size(), 0);
42   EXPECT_EQ(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);
48     EXPECT_EQ(Watchdog::creation_order().size(), 1);
49     std::shared_ptr<GlobalWatchdog> wd2 = Singleton<GlobalWatchdog>::try_get();
50     EXPECT_NE(wd2, nullptr);
51     EXPECT_EQ(wd1.get(), wd2.get());
52     EXPECT_EQ(Watchdog::creation_order().size(), 1);
53   }
54
55   SingletonVault::singleton()->destroyInstances();
56   EXPECT_EQ(Watchdog::creation_order().size(), 0);
57 }