@@ -110,6 +110,8 @@ use std::default::Default;
110110use std:: net:: ToSocketAddrs ;
111111use std:: sync:: { Arc , Mutex , RwLock } ;
112112use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
113+ #[ cfg( cycle_tests) ]
114+ use std:: { any:: Any , sync:: Weak } ;
113115
114116pub use balance:: { BalanceDetails , LightningBalance , PendingSweepBalance } ;
115117use bitcoin:: secp256k1:: PublicKey ;
@@ -1763,15 +1765,31 @@ impl Node {
17631765 }
17641766
17651767 #[ cfg( cycle_tests) ]
1766- /// Fetch a reference to the inner NetworkGraph, for Arc cycle detection
1767- pub fn fetch_ref ( & self ) -> std:: sync:: Weak < Graph > {
1768- Arc :: downgrade ( & self . network_graph )
1768+ /// Fetch a [`LeakChecker`] which can be used to ensure that the inner fields of this object
1769+ /// are released.
1770+ ///
1771+ /// Call [`LeakChecker::assert_no_leaks`] after this [`Node`] has been `drop`ped (otherwise it
1772+ /// will assume that all fields have leaked).
1773+ pub fn leak_checker ( & self ) -> LeakChecker {
1774+ let graph = Arc :: downgrade ( & self . network_graph ) as Weak < dyn Any > ;
1775+ LeakChecker ( vec ! [ graph] )
17691776 }
17701777}
17711778
1772- impl Drop for Node {
1773- fn drop ( & mut self ) {
1774- let _ = self . stop ( ) ;
1779+ #[ cfg( cycle_tests) ]
1780+ /// A list of [`Weak`]s which can be used to check that a [`Node`]'s inner fields are being
1781+ /// properly released after the [`Node`] is dropped.
1782+ pub struct LeakChecker ( Vec < Weak < dyn Any > > ) ;
1783+
1784+ #[ cfg( cycle_tests) ]
1785+ impl LeakChecker {
1786+ /// Asserts that all the stored [`Weak`]s point to contents which have been freed.
1787+ ///
1788+ /// This will (obviously) panic if the [`Node`] has not yet been dropped.
1789+ pub fn assert_no_leaks ( & self ) {
1790+ for weak in self . 0 . iter ( ) {
1791+ assert_eq ! ( weak. strong_count( ) , 0 ) ;
1792+ }
17751793 }
17761794}
17771795
0 commit comments