From 22ffed1623680442b43b929efb7da575ba79d842 Mon Sep 17 00:00:00 2001 From: Nick Tchervenski Date: Tue, 30 Dec 2014 12:32:06 -0800 Subject: [PATCH] add a UT for Wangle::Future for circular dependency 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 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/folly/wangle/futures/test/FutureTest.cpp b/folly/wangle/futures/test/FutureTest.cpp index 3e8fc97a..07f24621 100644 --- a/folly/wangle/futures/test/FutureTest.cpp +++ b/folly/wangle/futures/test/FutureTest.cpp @@ -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 promise; + auto ptr = std::make_shared>(promise.getFuture()); + + ptr->then( + [ptr] (folly::wangle::Try&& 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;}); +} -- 2.34.1