2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <folly/Singleton.h>
21 #include <folly/Benchmark.h>
22 #include <folly/test/SingletonTestStructs.h>
23 #include <folly/portability/GTest.h>
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`.
32 using namespace folly;
35 Singleton<GlobalWatchdog> global_watchdog;
38 // Test basic global usage (the default way singletons will generally
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);
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);
55 SingletonVault::singleton()->destroyInstances();
56 EXPECT_EQ(Watchdog::creation_order().size(), 0);