From e3e6edad974fe6a418ed7a503fa904a4052bd65d Mon Sep 17 00:00:00 2001 From: Chip Turner Date: Fri, 8 Aug 2014 03:41:20 -0700 Subject: [PATCH] Fix test breakage in clang Summary: clang would re-use the same memory location for recreated objects, so instead of checking pointers, we can use a serial number. Test Plan: runtests with fbconfig --clang Reviewed By: lins@fb.com Subscribers: mathieubaudet, lins, anca FB internal diff: D1485907 --- folly/experimental/test/SingletonTest.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/folly/experimental/test/SingletonTest.cpp b/folly/experimental/test/SingletonTest.cpp index 03b94916..f0815d91 100644 --- a/folly/experimental/test/SingletonTest.cpp +++ b/folly/experimental/test/SingletonTest.cpp @@ -24,10 +24,14 @@ using namespace folly; // A simple class that tracks how often instances of the class and -// subclasses are created, and the ordering. +// subclasses are created, and the ordering. Also tracks a global +// unique counter for each object. +std::atomic global_counter(19770326); struct Watchdog { static std::vector creation_order; - Watchdog() { creation_order.push_back(this); } + Watchdog() : serial_number(++global_counter) { + creation_order.push_back(this); + } ~Watchdog() { if (creation_order.back() != this) { @@ -36,6 +40,8 @@ struct Watchdog { creation_order.pop_back(); } + const size_t serial_number; + Watchdog(const Watchdog&) = delete; Watchdog& operator=(const Watchdog&) = delete; Watchdog(Watchdog&&) noexcept = default; @@ -185,12 +191,16 @@ TEST(Singleton, SharedPtrUsage) { EXPECT_EQ(shared_s1.use_count(), 2); locked_s1.reset(); EXPECT_EQ(shared_s1.use_count(), 1); + + // Track serial number rather than pointer since the memory could be + // re-used when we create new_s1. + auto old_serial = shared_s1->serial_number; shared_s1.reset(); locked_s1 = weak_s1.lock(); EXPECT_TRUE(weak_s1.expired()); Watchdog* new_s1 = Singleton::get(&vault); - EXPECT_NE(new_s1, s1); + EXPECT_NE(new_s1->serial_number, old_serial); } // Some classes to test singleton dependencies. NeedySingleton has a -- 2.34.1