Commit b2f445a2 authored by Nicholas Ormrod's avatar Nicholas Ormrod Committed by Sara Golemon

Revert "Using type-tags for test SingletonVaults"

Summary: This reverts commit 315cfed207895455ecd359d0c1b9d98f28ed0519.

Test Plan:
fbconfig -r folly && fbmake dbg

Reviewed By: robbert@fb.com

Subscribers: sdwilsh, folly-diffs@, yfeldblum

FB internal diff: D1831932

Signature: t1:1831932:1423258329:0962b939a93bbf1722a9e1c90090dcc024b63765

Blame Revision: D1823663
parent 777002cb
...@@ -86,6 +86,11 @@ void SingletonVault::reenableInstances() { ...@@ -86,6 +86,11 @@ void SingletonVault::reenableInstances() {
state_ = SingletonVaultState::Running; state_ = SingletonVaultState::Running;
} }
SingletonVault* SingletonVault::singleton() {
static SingletonVault* vault = new SingletonVault();
return vault;
}
void SingletonVault::scheduleDestroyInstances() { void SingletonVault::scheduleDestroyInstances() {
RequestContext::getStaticContext(); RequestContext::getStaticContext();
......
...@@ -399,17 +399,7 @@ class SingletonVault { ...@@ -399,17 +399,7 @@ class SingletonVault {
// A well-known vault; you can actually have others, but this is the // A well-known vault; you can actually have others, but this is the
// default. // default.
static SingletonVault* singleton() { static SingletonVault* singleton();
return singleton<>();
}
// Gets singleton vault for any Tag. Non-default tag should be used in unit
// tests only.
template <typename VaultTag = detail::DefaultTag>
static SingletonVault* singleton() {
static SingletonVault* vault = new SingletonVault();
return vault;
}
private: private:
// The two stages of life for a vault, as mentioned in the class comment. // The two stages of life for a vault, as mentioned in the class comment.
...@@ -544,9 +534,7 @@ class SingletonVault { ...@@ -544,9 +534,7 @@ class SingletonVault {
// singletons. Create instances of this class in the global scope of // singletons. Create instances of this class in the global scope of
// type Singleton<T> to register your singleton for later access via // type Singleton<T> to register your singleton for later access via
// Singleton<T>::get(). // Singleton<T>::get().
template <typename T, template <typename T, typename Tag = detail::DefaultTag>
typename Tag = detail::DefaultTag,
typename VaultTag = detail::DefaultTag /* for testing */>
class Singleton { class Singleton {
public: public:
typedef std::function<T*(void)> CreateFunc; typedef std::function<T*(void)> CreateFunc;
...@@ -555,9 +543,9 @@ class Singleton { ...@@ -555,9 +543,9 @@ class Singleton {
// Generally your program life cycle should be fine with calling // Generally your program life cycle should be fine with calling
// get() repeatedly rather than saving the reference, and then not // get() repeatedly rather than saving the reference, and then not
// call get() during process shutdown. // call get() during process shutdown.
static T* get() { static T* get(SingletonVault* vault = nullptr /* for testing */) {
return static_cast<T*>( return static_cast<T*>(
SingletonVault::singleton<VaultTag>()->get_ptr(typeDescriptor())); (vault ?: SingletonVault::singleton())->get_ptr(typeDescriptor()));
} }
// Same as get, but should be preffered to it in the same compilation // Same as get, but should be preffered to it in the same compilation
...@@ -566,7 +554,7 @@ class Singleton { ...@@ -566,7 +554,7 @@ class Singleton {
if (LIKELY(entry_->state == detail::SingletonEntryState::Living)) { if (LIKELY(entry_->state == detail::SingletonEntryState::Living)) {
return reinterpret_cast<T*>(entry_->instance_ptr); return reinterpret_cast<T*>(entry_->instance_ptr);
} else { } else {
return get(); return get(vault_);
} }
} }
...@@ -574,9 +562,10 @@ class Singleton { ...@@ -574,9 +562,10 @@ class Singleton {
// singleton, you can try to do so with a weak_ptr. Avoid this when // singleton, you can try to do so with a weak_ptr. Avoid this when
// possible but the inability to lock the weak pointer can be a // possible but the inability to lock the weak pointer can be a
// signal that the vault has been destroyed. // signal that the vault has been destroyed.
static std::weak_ptr<T> get_weak() { static std::weak_ptr<T> get_weak(
SingletonVault* vault = nullptr /* for testing */) {
auto weak_void_ptr = auto weak_void_ptr =
(SingletonVault::singleton<VaultTag>())->get_weak(typeDescriptor()); (vault ?: SingletonVault::singleton())->get_weak(typeDescriptor());
// This is ugly and inefficient, but there's no other way to do it, because // This is ugly and inefficient, but there's no other way to do it, because
// there's no static_pointer_cast for weak_ptr. // there's no static_pointer_cast for weak_ptr.
...@@ -599,7 +588,7 @@ class Singleton { ...@@ -599,7 +588,7 @@ class Singleton {
} }
return std::static_pointer_cast<T>(shared_void_ptr); return std::static_pointer_cast<T>(shared_void_ptr);
} else { } else {
return get_weak(); return get_weak(vault_);
} }
} }
...@@ -610,19 +599,26 @@ class Singleton { ...@@ -610,19 +599,26 @@ class Singleton {
T* operator->() { return ptr(); } T* operator->() { return ptr(); }
explicit Singleton(std::nullptr_t _ = nullptr, explicit Singleton(std::nullptr_t _ = nullptr,
Singleton::TeardownFunc t = nullptr) : Singleton::TeardownFunc t = nullptr,
Singleton ([]() { return new T; }, std::move(t)) { SingletonVault* vault = nullptr) :
Singleton ([]() { return new T; },
std::move(t),
vault) {
} }
explicit Singleton(Singleton::CreateFunc c, explicit Singleton(Singleton::CreateFunc c,
Singleton::TeardownFunc t = nullptr) { Singleton::TeardownFunc t = nullptr,
SingletonVault* vault = nullptr) {
if (c == nullptr) { if (c == nullptr) {
throw std::logic_error( throw std::logic_error(
"nullptr_t should be passed if you want T to be default constructed"); "nullptr_t should be passed if you want T to be default constructed");
} }
auto vault = SingletonVault::singleton<VaultTag>(); if (vault == nullptr) {
vault = SingletonVault::singleton();
}
vault_ = vault;
entry_ = entry_ =
&(vault->registerSingleton(typeDescriptor(), c, getTeardownFunc(t))); &(vault->registerSingleton(typeDescriptor(), c, getTeardownFunc(t)));
} }
...@@ -638,18 +634,22 @@ class Singleton { ...@@ -638,18 +634,22 @@ class Singleton {
* regular singletons. * regular singletons.
*/ */
static void make_mock(std::nullptr_t c = nullptr, static void make_mock(std::nullptr_t c = nullptr,
typename Singleton<T>::TeardownFunc t = nullptr) { typename Singleton<T>::TeardownFunc t = nullptr,
make_mock([]() { return new T; }, t); SingletonVault* vault = nullptr /* for testing */ ) {
make_mock([]() { return new T; }, t, vault);
} }
static void make_mock(CreateFunc c, static void make_mock(CreateFunc c,
typename Singleton<T>::TeardownFunc t = nullptr) { typename Singleton<T>::TeardownFunc t = nullptr,
SingletonVault* vault = nullptr /* for testing */ ) {
if (c == nullptr) { if (c == nullptr) {
throw std::logic_error( throw std::logic_error(
"nullptr_t should be passed if you want T to be default constructed"); "nullptr_t should be passed if you want T to be default constructed");
} }
auto vault = SingletonVault::singleton<VaultTag>(); if (vault == nullptr) {
vault = SingletonVault::singleton();
}
vault->registerMockSingleton( vault->registerMockSingleton(
typeDescriptor(), typeDescriptor(),
...@@ -680,6 +680,7 @@ class Singleton { ...@@ -680,6 +680,7 @@ class Singleton {
// We rely on the fact that Singleton destructor won't reset this pointer, so // We rely on the fact that Singleton destructor won't reset this pointer, so
// it can be "safely" used even after static Singleton object is destroyed. // it can be "safely" used even after static Singleton object is destroyed.
detail::SingletonEntry* entry_; detail::SingletonEntry* entry_;
SingletonVault* vault_;
}; };
} }
...@@ -83,34 +83,30 @@ TEST(Singleton, MissingSingleton) { ...@@ -83,34 +83,30 @@ TEST(Singleton, MissingSingleton) {
std::out_of_range); std::out_of_range);
} }
struct BasicUsageTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonBasicUsage = Singleton <T, Tag, BasicUsageTag>;
// Exercise some basic codepaths ensuring registration order and // Exercise some basic codepaths ensuring registration order and
// destruction order happen as expected, that instances are created // destruction order happen as expected, that instances are created
// when expected, etc etc. // when expected, etc etc.
TEST(Singleton, BasicUsage) { TEST(Singleton, BasicUsage) {
auto& vault = *SingletonVault::singleton<BasicUsageTag>(); SingletonVault vault;
EXPECT_EQ(vault.registeredSingletonCount(), 0); EXPECT_EQ(vault.registeredSingletonCount(), 0);
SingletonBasicUsage<Watchdog> watchdog_singleton; Singleton<Watchdog> watchdog_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
SingletonBasicUsage<ChildWatchdog> child_watchdog_singleton; Singleton<ChildWatchdog> child_watchdog_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
vault.registrationComplete(); vault.registrationComplete();
Watchdog* s1 = SingletonBasicUsage<Watchdog>::get(); Watchdog* s1 = Singleton<Watchdog>::get(&vault);
EXPECT_NE(s1, nullptr); EXPECT_NE(s1, nullptr);
Watchdog* s2 = SingletonBasicUsage<Watchdog>::get(); Watchdog* s2 = Singleton<Watchdog>::get(&vault);
EXPECT_NE(s2, nullptr); EXPECT_NE(s2, nullptr);
EXPECT_EQ(s1, s2); EXPECT_EQ(s1, s2);
auto s3 = SingletonBasicUsage<ChildWatchdog>::get(); auto s3 = Singleton<ChildWatchdog>::get(&vault);
EXPECT_NE(s3, nullptr); EXPECT_NE(s3, nullptr);
EXPECT_NE(s2, s3); EXPECT_NE(s2, s3);
...@@ -122,38 +118,28 @@ TEST(Singleton, BasicUsage) { ...@@ -122,38 +118,28 @@ TEST(Singleton, BasicUsage) {
EXPECT_EQ(vault.livingSingletonCount(), 0); EXPECT_EQ(vault.livingSingletonCount(), 0);
} }
struct DirectUsageTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonDirectUsage = Singleton <T, Tag, DirectUsageTag>;
TEST(Singleton, DirectUsage) { TEST(Singleton, DirectUsage) {
auto& vault = *SingletonVault::singleton<DirectUsageTag>(); SingletonVault vault;
EXPECT_EQ(vault.registeredSingletonCount(), 0); EXPECT_EQ(vault.registeredSingletonCount(), 0);
// Verify we can get to the underlying singletons via directly using // Verify we can get to the underlying singletons via directly using
// the singleton definition. // the singleton definition.
SingletonDirectUsage<Watchdog> watchdog; Singleton<Watchdog> watchdog(nullptr, nullptr, &vault);
struct TestTag {}; struct TestTag {};
SingletonDirectUsage<Watchdog, TestTag> named_watchdog; Singleton<Watchdog, TestTag> named_watchdog(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
vault.registrationComplete(); vault.registrationComplete();
EXPECT_NE(watchdog.ptr(), nullptr); EXPECT_NE(watchdog.ptr(), nullptr);
EXPECT_EQ(watchdog.ptr(), SingletonDirectUsage<Watchdog>::get()); EXPECT_EQ(watchdog.ptr(), Singleton<Watchdog>::get(&vault));
EXPECT_NE(watchdog.ptr(), named_watchdog.ptr()); EXPECT_NE(watchdog.ptr(), named_watchdog.ptr());
EXPECT_EQ(watchdog->livingWatchdogCount(), 2); EXPECT_EQ(watchdog->livingWatchdogCount(), 2);
EXPECT_EQ((*watchdog).livingWatchdogCount(), 2); EXPECT_EQ((*watchdog).livingWatchdogCount(), 2);
vault.destroyInstances();
} }
struct NamedUsageTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonNamedUsage = Singleton <T, Tag, NamedUsageTag>;
TEST(Singleton, NamedUsage) { TEST(Singleton, NamedUsage) {
auto& vault = *SingletonVault::singleton<NamedUsageTag>(); SingletonVault vault;
EXPECT_EQ(vault.registeredSingletonCount(), 0); EXPECT_EQ(vault.registeredSingletonCount(), 0);
...@@ -161,105 +147,96 @@ TEST(Singleton, NamedUsage) { ...@@ -161,105 +147,96 @@ TEST(Singleton, NamedUsage) {
struct Watchdog1 {}; struct Watchdog1 {};
struct Watchdog2 {}; struct Watchdog2 {};
typedef detail::DefaultTag Watchdog3; typedef detail::DefaultTag Watchdog3;
SingletonNamedUsage<Watchdog, Watchdog1> watchdog1_singleton; Singleton<Watchdog, Watchdog1> watchdog1_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
SingletonNamedUsage<Watchdog, Watchdog2> watchdog2_singleton; Singleton<Watchdog, Watchdog2> watchdog2_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
SingletonNamedUsage<Watchdog, Watchdog3> watchdog3_singleton; Singleton<Watchdog, Watchdog3> watchdog3_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 3); EXPECT_EQ(vault.registeredSingletonCount(), 3);
vault.registrationComplete(); vault.registrationComplete();
// Verify our three singletons are distinct and non-nullptr. // Verify our three singletons are distinct and non-nullptr.
Watchdog* s1 = SingletonNamedUsage<Watchdog, Watchdog1>::get(); Watchdog* s1 = Singleton<Watchdog, Watchdog1>::get(&vault);
EXPECT_EQ(s1, watchdog1_singleton.ptr()); EXPECT_EQ(s1, watchdog1_singleton.ptr());
Watchdog* s2 = SingletonNamedUsage<Watchdog, Watchdog2>::get(); Watchdog* s2 = Singleton<Watchdog, Watchdog2>::get(&vault);
EXPECT_EQ(s2, watchdog2_singleton.ptr()); EXPECT_EQ(s2, watchdog2_singleton.ptr());
EXPECT_NE(s1, s2); EXPECT_NE(s1, s2);
Watchdog* s3 = SingletonNamedUsage<Watchdog, Watchdog3>::get(); Watchdog* s3 = Singleton<Watchdog, Watchdog3>::get(&vault);
EXPECT_EQ(s3, watchdog3_singleton.ptr()); EXPECT_EQ(s3, watchdog3_singleton.ptr());
EXPECT_NE(s3, s1); EXPECT_NE(s3, s1);
EXPECT_NE(s3, s2); EXPECT_NE(s3, s2);
// Verify the "default" singleton is the same as the DefaultTag-tagged // Verify the "default" singleton is the same as the DefaultTag-tagged
// singleton. // singleton.
Watchdog* s4 = SingletonNamedUsage<Watchdog>::get(); Watchdog* s4 = Singleton<Watchdog>::get(&vault);
EXPECT_EQ(s4, watchdog3_singleton.ptr()); EXPECT_EQ(s4, watchdog3_singleton.ptr());
vault.destroyInstances();
} }
struct NaughtyUsageTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonNaughtyUsage = Singleton <T, Tag, NaughtyUsageTag>;
struct NaughtyUsageTag2 {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonNaughtyUsage2 = Singleton <T, Tag, NaughtyUsageTag2>;
// Some pathological cases such as getting unregistered singletons, // Some pathological cases such as getting unregistered singletons,
// double registration, etc. // double registration, etc.
TEST(Singleton, NaughtyUsage) { TEST(Singleton, NaughtyUsage) {
auto& vault = *SingletonVault::singleton<NaughtyUsageTag>(); SingletonVault vault(SingletonVault::Type::Strict);
vault.registrationComplete(); vault.registrationComplete();
// Unregistered. // Unregistered.
EXPECT_THROW(Singleton<Watchdog>::get(), std::out_of_range); EXPECT_THROW(Singleton<Watchdog>::get(), std::out_of_range);
EXPECT_THROW(SingletonNaughtyUsage<Watchdog>::get(), std::out_of_range); EXPECT_THROW(Singleton<Watchdog>::get(&vault), std::out_of_range);
vault.destroyInstances();
auto& vault2 = *SingletonVault::singleton<NaughtyUsageTag2>(); // Registring singletons after registrationComplete called.
EXPECT_THROW([&vault]() {
Singleton<Watchdog> watchdog_singleton(
nullptr, nullptr, &vault);
}(),
std::logic_error);
EXPECT_THROW(SingletonNaughtyUsage2<Watchdog>::get(), std::logic_error); SingletonVault vault_2(SingletonVault::Type::Strict);
SingletonNaughtyUsage2<Watchdog> watchdog_singleton; EXPECT_THROW(Singleton<Watchdog>::get(&vault_2), std::logic_error);
Singleton<Watchdog> watchdog_singleton(nullptr, nullptr, &vault_2);
// double registration // double registration
EXPECT_THROW([]() { EXPECT_THROW([&vault_2]() {
SingletonNaughtyUsage2<Watchdog> watchdog_singleton; Singleton<Watchdog> watchdog_singleton(
nullptr, nullptr, &vault_2);
}(), }(),
std::logic_error); std::logic_error);
vault2.destroyInstances(); vault_2.destroyInstances();
// double registration after destroy // double registration after destroy
EXPECT_THROW([]() { EXPECT_THROW([&vault_2]() {
SingletonNaughtyUsage2<Watchdog> watchdog_singleton; Singleton<Watchdog> watchdog_singleton(
nullptr, nullptr, &vault_2);
}(), }(),
std::logic_error); std::logic_error);
} }
struct SharedPtrUsageTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonSharedPtrUsage = Singleton <T, Tag, SharedPtrUsageTag>;
TEST(Singleton, SharedPtrUsage) { TEST(Singleton, SharedPtrUsage) {
auto& vault = *SingletonVault::singleton<SharedPtrUsageTag>(); SingletonVault vault;
EXPECT_EQ(vault.registeredSingletonCount(), 0); EXPECT_EQ(vault.registeredSingletonCount(), 0);
SingletonSharedPtrUsage<Watchdog> watchdog_singleton; Singleton<Watchdog> watchdog_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
SingletonSharedPtrUsage<ChildWatchdog> child_watchdog_singleton; Singleton<ChildWatchdog> child_watchdog_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
struct ATag {}; struct ATag {};
SingletonSharedPtrUsage<Watchdog, ATag> named_watchdog_singleton; Singleton<Watchdog, ATag> named_watchdog_singleton(nullptr, nullptr, &vault);
vault.registrationComplete(); vault.registrationComplete();
Watchdog* s1 = SingletonSharedPtrUsage<Watchdog>::get(); Watchdog* s1 = Singleton<Watchdog>::get(&vault);
EXPECT_NE(s1, nullptr); EXPECT_NE(s1, nullptr);
Watchdog* s2 = SingletonSharedPtrUsage<Watchdog>::get(); Watchdog* s2 = Singleton<Watchdog>::get(&vault);
EXPECT_NE(s2, nullptr); EXPECT_NE(s2, nullptr);
EXPECT_EQ(s1, s2); EXPECT_EQ(s1, s2);
auto weak_s1 = SingletonSharedPtrUsage<Watchdog>::get_weak(); auto weak_s1 = Singleton<Watchdog>::get_weak(&vault);
auto shared_s1 = weak_s1.lock(); auto shared_s1 = weak_s1.lock();
EXPECT_EQ(shared_s1.get(), s1); EXPECT_EQ(shared_s1.get(), s1);
EXPECT_EQ(shared_s1.use_count(), 2); EXPECT_EQ(shared_s1.use_count(), 2);
{ {
auto named_weak_s1 = auto named_weak_s1 = Singleton<Watchdog, ATag>::get_weak(&vault);
SingletonSharedPtrUsage<Watchdog, ATag>::get_weak();
auto locked = named_weak_s1.lock(); auto locked = named_weak_s1.lock();
EXPECT_NE(locked.get(), shared_s1.get()); EXPECT_NE(locked.get(), shared_s1.get());
} }
...@@ -291,16 +268,16 @@ TEST(Singleton, SharedPtrUsage) { ...@@ -291,16 +268,16 @@ TEST(Singleton, SharedPtrUsage) {
locked_s1 = weak_s1.lock(); locked_s1 = weak_s1.lock();
EXPECT_TRUE(weak_s1.expired()); EXPECT_TRUE(weak_s1.expired());
auto empty_s1 = SingletonSharedPtrUsage<Watchdog>::get_weak(); auto empty_s1 = Singleton<Watchdog>::get_weak(&vault);
EXPECT_FALSE(empty_s1.lock()); EXPECT_FALSE(empty_s1.lock());
vault.reenableInstances(); vault.reenableInstances();
// Singleton should be re-created only after reenableInstances() was called. // Singleton should be re-created only after reenableInstances() was called.
Watchdog* new_s1 = SingletonSharedPtrUsage<Watchdog>::get(); Watchdog* new_s1 = Singleton<Watchdog>::get(&vault);
EXPECT_NE(new_s1->serial_number, old_serial); EXPECT_NE(new_s1->serial_number, old_serial);
auto new_s1_weak = SingletonSharedPtrUsage<Watchdog>::get_weak(); auto new_s1_weak = Singleton<Watchdog>::get_weak(&vault);
auto new_s1_shared = new_s1_weak.lock(); auto new_s1_shared = new_s1_weak.lock();
std::thread t([new_s1_shared]() mutable { std::thread t([new_s1_shared]() mutable {
std::this_thread::sleep_for(std::chrono::seconds{2}); std::this_thread::sleep_for(std::chrono::seconds{2});
...@@ -321,49 +298,41 @@ TEST(Singleton, SharedPtrUsage) { ...@@ -321,49 +298,41 @@ TEST(Singleton, SharedPtrUsage) {
// Some classes to test singleton dependencies. NeedySingleton has a // Some classes to test singleton dependencies. NeedySingleton has a
// dependency on NeededSingleton, which happens during its // dependency on NeededSingleton, which happens during its
// construction. // construction.
struct NeedyTag {}; SingletonVault needy_vault;
template <typename T, typename Tag = detail::DefaultTag>
using SingletonNeedy = Singleton <T, Tag, NeedyTag>;
struct NeededSingleton {}; struct NeededSingleton {};
struct NeedySingleton { struct NeedySingleton {
NeedySingleton() { NeedySingleton() {
auto unused = SingletonNeedy<NeededSingleton>::get(); auto unused = Singleton<NeededSingleton>::get(&needy_vault);
EXPECT_NE(unused, nullptr); EXPECT_NE(unused, nullptr);
} }
}; };
// Ensure circular dependencies fail -- a singleton that needs itself, whoops. // Ensure circular dependencies fail -- a singleton that needs itself, whoops.
struct SelfNeedyTag {}; SingletonVault self_needy_vault;
template <typename T, typename Tag = detail::DefaultTag>
using SingletonSelfNeedy = Singleton <T, Tag, SelfNeedyTag>;
struct SelfNeedySingleton { struct SelfNeedySingleton {
SelfNeedySingleton() { SelfNeedySingleton() {
auto unused = SingletonSelfNeedy<SelfNeedySingleton>::get(); auto unused = Singleton<SelfNeedySingleton>::get(&self_needy_vault);
EXPECT_NE(unused, nullptr); EXPECT_NE(unused, nullptr);
} }
}; };
TEST(Singleton, SingletonDependencies) { TEST(Singleton, SingletonDependencies) {
SingletonNeedy<NeededSingleton> needed_singleton; Singleton<NeededSingleton> needed_singleton(nullptr, nullptr, &needy_vault);
SingletonNeedy<NeedySingleton> needy_singleton; Singleton<NeedySingleton> needy_singleton(nullptr, nullptr, &needy_vault);
auto& needy_vault = *SingletonVault::singleton<NeedyTag>();
needy_vault.registrationComplete(); needy_vault.registrationComplete();
EXPECT_EQ(needy_vault.registeredSingletonCount(), 2); EXPECT_EQ(needy_vault.registeredSingletonCount(), 2);
EXPECT_EQ(needy_vault.livingSingletonCount(), 0); EXPECT_EQ(needy_vault.livingSingletonCount(), 0);
auto needy = SingletonNeedy<NeedySingleton>::get(); auto needy = Singleton<NeedySingleton>::get(&needy_vault);
EXPECT_EQ(needy_vault.livingSingletonCount(), 2); EXPECT_EQ(needy_vault.livingSingletonCount(), 2);
SingletonSelfNeedy<SelfNeedySingleton> self_needy_singleton; Singleton<SelfNeedySingleton> self_needy_singleton(
auto& self_needy_vault = *SingletonVault::singleton<SelfNeedyTag>(); nullptr, nullptr, &self_needy_vault);
self_needy_vault.registrationComplete(); self_needy_vault.registrationComplete();
EXPECT_THROW([]() { EXPECT_THROW([]() {
SingletonSelfNeedy<SelfNeedySingleton>::get(); Singleton<SelfNeedySingleton>::get(&self_needy_vault);
}(), }(),
std::out_of_range); std::out_of_range);
} }
...@@ -376,21 +345,17 @@ class Slowpoke : public Watchdog { ...@@ -376,21 +345,17 @@ class Slowpoke : public Watchdog {
Slowpoke() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } Slowpoke() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }
}; };
struct ConcurrencyTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonConcurrency = Singleton <T, Tag, ConcurrencyTag>;
TEST(Singleton, SingletonConcurrency) { TEST(Singleton, SingletonConcurrency) {
auto& vault = *SingletonVault::singleton<ConcurrencyTag>(); SingletonVault vault;
SingletonConcurrency<Slowpoke> slowpoke_singleton; Singleton<Slowpoke> slowpoke_singleton(nullptr, nullptr, &vault);
vault.registrationComplete(); vault.registrationComplete();
std::mutex gatekeeper; std::mutex gatekeeper;
gatekeeper.lock(); gatekeeper.lock();
auto func = [&gatekeeper]() { auto func = [&vault, &gatekeeper]() {
gatekeeper.lock(); gatekeeper.lock();
gatekeeper.unlock(); gatekeeper.unlock();
auto unused = SingletonConcurrency<Slowpoke>::get(); auto unused = Singleton<Slowpoke>::get(&vault);
}; };
EXPECT_EQ(vault.livingSingletonCount(), 0); EXPECT_EQ(vault.livingSingletonCount(), 0);
...@@ -408,18 +373,14 @@ TEST(Singleton, SingletonConcurrency) { ...@@ -408,18 +373,14 @@ TEST(Singleton, SingletonConcurrency) {
EXPECT_EQ(vault.livingSingletonCount(), 1); EXPECT_EQ(vault.livingSingletonCount(), 1);
} }
struct ConcurrencyStressTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonConcurrencyStress = Singleton <T, Tag, ConcurrencyStressTag>;
TEST(Singleton, SingletonConcurrencyStress) { TEST(Singleton, SingletonConcurrencyStress) {
auto& vault = *SingletonVault::singleton<ConcurrencyStressTag>(); SingletonVault vault;
SingletonConcurrencyStress<Slowpoke> slowpoke_singleton; Singleton<Slowpoke> slowpoke_singleton(nullptr, nullptr, &vault);
std::vector<std::thread> ts; std::vector<std::thread> ts;
for (size_t i = 0; i < 100; ++i) { for (size_t i = 0; i < 100; ++i) {
ts.emplace_back([&]() { ts.emplace_back([&]() {
slowpoke_singleton.get_weak().lock(); slowpoke_singleton.get_weak(&vault).lock();
}); });
} }
...@@ -451,28 +412,23 @@ int* getNormalSingleton() { ...@@ -451,28 +412,23 @@ int* getNormalSingleton() {
return &normal_singleton_value; return &normal_singleton_value;
} }
struct MockTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonMock = Singleton <T, Tag, MockTag>;
// Verify that existing Singleton's can be overridden // Verify that existing Singleton's can be overridden
// using the make_mock functionality. // using the make_mock functionality.
TEST(Singleton, MockTest) { TEST(Singleton, make_mock) {
auto& vault = *SingletonVault::singleton<MockTag>(); SingletonVault vault(SingletonVault::Type::Strict);
Singleton<Watchdog> watchdog_singleton(nullptr, nullptr, &vault);
SingletonMock<Watchdog> watchdog_singleton;
vault.registrationComplete(); vault.registrationComplete();
// Registring singletons after registrationComplete called works // Registring singletons after registrationComplete called works
// with make_mock (but not with Singleton ctor). // with make_mock (but not with Singleton ctor).
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
int serial_count_first = SingletonMock<Watchdog>::get()->serial_number; int serial_count_first = Singleton<Watchdog>::get(&vault)->serial_number;
// Override existing mock using make_mock. // Override existing mock using make_mock.
SingletonMock<Watchdog>::make_mock(); Singleton<Watchdog>::make_mock(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
int serial_count_mock = SingletonMock<Watchdog>::get()->serial_number; int serial_count_mock = Singleton<Watchdog>::get(&vault)->serial_number;
// If serial_count value is the same, then singleton was not replaced. // If serial_count value is the same, then singleton was not replaced.
EXPECT_NE(serial_count_first, serial_count_mock); EXPECT_NE(serial_count_first, serial_count_mock);
...@@ -494,25 +450,34 @@ BENCHMARK_RELATIVE(MeyersSingleton, n) { ...@@ -494,25 +450,34 @@ BENCHMARK_RELATIVE(MeyersSingleton, n) {
} }
} }
struct BenchmarkTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonBenchmark = Singleton <T, Tag, BenchmarkTag>;
SingletonBenchmark<BenchmarkSingleton> benchmark_singleton;
BENCHMARK_RELATIVE(FollySingletonSlow, n) { BENCHMARK_RELATIVE(FollySingletonSlow, n) {
SingletonVault benchmark_vault;
Singleton<BenchmarkSingleton> benchmark_singleton(
nullptr, nullptr, &benchmark_vault);
benchmark_vault.registrationComplete();
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
doNotOptimizeAway(SingletonBenchmark<BenchmarkSingleton>::get()); doNotOptimizeAway(Singleton<BenchmarkSingleton>::get(&benchmark_vault));
} }
} }
BENCHMARK_RELATIVE(FollySingletonFast, n) { BENCHMARK_RELATIVE(FollySingletonFast, n) {
SingletonVault benchmark_vault;
Singleton<BenchmarkSingleton> benchmark_singleton(
nullptr, nullptr, &benchmark_vault);
benchmark_vault.registrationComplete();
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
doNotOptimizeAway(benchmark_singleton.get_fast()); doNotOptimizeAway(benchmark_singleton.get_fast());
} }
} }
BENCHMARK_RELATIVE(FollySingletonFastWeak, n) { BENCHMARK_RELATIVE(FollySingletonFastWeak, n) {
SingletonVault benchmark_vault;
Singleton<BenchmarkSingleton> benchmark_singleton(
nullptr, nullptr, &benchmark_vault);
benchmark_vault.registrationComplete();
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
benchmark_singleton.get_weak_fast(); benchmark_singleton.get_weak_fast();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment