add a UT for Wangle::Future for circular dependency
authorNick Tchervenski <nickolay@fb.com>
Tue, 30 Dec 2014 20:32:06 +0000 (12:32 -0800)
committerViswanath Sivakumar <viswanath@fb.com>
Tue, 13 Jan 2015 19:01:03 +0000 (11:01 -0800)
Summary:
Making sure Wangle can handle circular dependencies.
There was an actual code issue in September that cause a crash in Atlas
Adserver due to this. The issue has been since fixed and Adserver's code
has been changed to avoid such circular dependency. We're adding a unit
test for this case.

Unit test is added as per our conversation on the Wangle group: https://www.facebook.com/groups/715931878455430/permalink/770180369697247/

Test Plan:
Add the same test on a code base from Sep 20th, observe that it fails:
https://phabricator.fb.com/P19352007

Run the unit test on latest code base - it succeeds

Reviewed By: hans@fb.com

Subscribers: atlas2-eng@, fugalh, folly-diffs@, leizhao, mchughj, kit, mpechuk

FB internal diff: D1761006

Tasks: 5384229

Signature: t1:1761006:1420238204:74ffb3fe8b88a25a23ade8e0990e69d405ea7f1e

folly/wangle/futures/test/FutureTest.cpp

index 3e8fc97ab142b53b42892c1a990acdfb80d93301..07f24621da0fa860da7a91a240f3e7412a9796da 100644 (file)
@@ -1178,3 +1178,27 @@ TEST(Future, t5506504) {
 
   waitWithSemaphore(fn());
 }
+
+// Test of handling of a circular dependency. It's never recommended
+// to have one because of possible memory leaks. Here we test that
+// we can handle freeing of the Future while it is running.
+TEST(Future, CircularDependencySharedPtrSelfReset) {
+  Promise<int64_t> promise;
+  auto ptr = std::make_shared<Future<int64_t>>(promise.getFuture());
+
+  ptr->then(
+    [ptr] (folly::wangle::Try<int64_t>&& uid) mutable {
+      EXPECT_EQ(1, ptr.use_count());
+
+      // Leaving no references to ourselves.
+      ptr.reset();
+      EXPECT_EQ(0, ptr.use_count());
+    }
+  );
+
+  EXPECT_EQ(2, ptr.use_count());
+
+  ptr.reset();
+
+  promise.fulfil([]{return 1l;});
+}