2 * Copyright 2016 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>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
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`.
34 using namespace folly;
37 Singleton<GlobalWatchdog> global_watchdog;
40 // Test basic global usage (the default way singletons will generally
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);
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);
57 SingletonVault::singleton()->destroyInstances();
58 EXPECT_EQ(Watchdog::creation_order().size(), 0);