Fix test breakage in clang
authorChip Turner <chip@fb.com>
Fri, 8 Aug 2014 10:41:20 +0000 (03:41 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 14 Aug 2014 18:49:04 +0000 (11:49 -0700)
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

index 03b94916789bc8a4ac73aeb2cd95fdb36969459d..f0815d912eca5d4009e6ca21aaf6f9f7d48e565d 100644 (file)
 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<size_t> global_counter(19770326);
 struct Watchdog {
   static std::vector<Watchdog*> 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<Watchdog>::get(&vault);
-  EXPECT_NE(new_s1, s1);
+  EXPECT_NE(new_s1->serial_number, old_serial);
 }
 
 // Some classes to test singleton dependencies.  NeedySingleton has a