Fix folly/test:singleton_test - Singleton.SharedPtrUsage
authorAndrii Grynenko <andrii@fb.com>
Mon, 10 Apr 2017 17:44:31 +0000 (10:44 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 10 Apr 2017 17:52:08 +0000 (10:52 -0700)
Reviewed By: yfeldblum

Differential Revision: D4856138

fbshipit-source-id: 19eee4f8c4108ef168d82d5e9b1f2d65ff9b03bd

folly/test/SingletonTest.cpp

index 782f039..7ed6f10 100644 (file)
@@ -202,7 +202,18 @@ TEST(Singleton, SharedPtrUsage) {
   auto& vault = *SingletonVault::singleton<SharedPtrUsageTag>();
 
   EXPECT_EQ(vault.registeredSingletonCount(), 0);
-  SingletonSharedPtrUsage<Watchdog> watchdog_singleton;
+  std::vector<std::unique_ptr<Watchdog>> watchdog_instances;
+  SingletonSharedPtrUsage<Watchdog> watchdog_singleton(
+      [&] {
+        watchdog_instances.push_back(std::make_unique<Watchdog>());
+        return watchdog_instances.back().get();
+      },
+      [&](Watchdog* ptr) {
+        // Make sure that only second instance is destroyed. First instance is
+        // expected to be leaked.
+        EXPECT_EQ(watchdog_instances[1].get(), ptr);
+        watchdog_instances[1].reset();
+      });
   EXPECT_EQ(vault.registeredSingletonCount(), 1);
 
   SingletonSharedPtrUsage<ChildWatchdog> child_watchdog_singleton;