@@ -17,15 +17,16 @@ internal class GarbageCollectionTests
1717 private class Service
1818 {
1919 }
20-
21- private static async Task ForceGarbageCollection ( )
20+
21+ private static async Task ForceGarbageCollectionAsync ( )
2222 {
23- Resources . UnloadUnusedAssets ( ) ;
24- GC . Collect ( ) ;
25- GC . WaitForPendingFinalizers ( ) ;
26- await Task . Yield ( ) ;
27- GC . Collect ( ) ;
28- GC . WaitForPendingFinalizers ( ) ;
23+ for ( var i = 0 ; i < 2 ; i ++ )
24+ {
25+ Resources . UnloadUnusedAssets ( ) ;
26+ GC . Collect ( ) ;
27+ GC . WaitForPendingFinalizers ( ) ;
28+ await Task . Yield ( ) ;
29+ }
2930 }
3031
3132 [ OneTimeSetUp ]
@@ -37,7 +38,7 @@ public void Setup()
3738 }
3839 }
3940
40- [ Test , Retry ( 3 ) ]
41+ [ Test ]
4142 public async Task Singleton_ShouldBeFinalized_WhenOwnerIsDisposed ( )
4243 {
4344 var references = new List < WeakReference > ( ) ;
@@ -51,11 +52,11 @@ void Act()
5152 }
5253
5354 Act ( ) ;
54- await ForceGarbageCollection ( ) ;
55+ await ForceGarbageCollectionAsync ( ) ;
5556 references . Any ( r => r . IsAlive ) . Should ( ) . BeFalse ( ) ;
5657 }
5758
58- [ Test , Retry ( 3 ) ]
59+ [ Test ]
5960 public async Task DisposedScopedContainer_ShouldHaveNoReferencesToItself_AndShouldBeCollectedAndFinalized ( )
6061 {
6162 var references = new List < WeakReference > ( ) ;
@@ -69,11 +70,11 @@ void Act()
6970 }
7071
7172 Act ( ) ;
72- await ForceGarbageCollection ( ) ;
73+ await ForceGarbageCollectionAsync ( ) ;
7374 references . Any ( r => r . IsAlive ) . Should ( ) . BeFalse ( ) ;
7475 }
7576
76- [ Test , Retry ( 3 ) ]
77+ [ Test ]
7778 public async Task Construct_ContainerShouldNotControlConstructedObjectLifeCycle_ByNotKeepingReferenceToIt ( )
7879 {
7980 var references = new List < WeakReference > ( ) ;
@@ -86,11 +87,11 @@ void Act()
8687 }
8788
8889 Act ( ) ;
89- await ForceGarbageCollection ( ) ;
90+ await ForceGarbageCollectionAsync ( ) ;
9091 references . Any ( r => r . IsAlive ) . Should ( ) . BeFalse ( ) ;
9192 }
9293
93- [ Test , Retry ( 3 ) ]
94+ [ Test ]
9495 public async Task ScopedFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
9596 {
9697 WeakReference instanceConstructedByChild ;
@@ -107,12 +108,12 @@ void Act()
107108 }
108109
109110 Act ( ) ;
110- await ForceGarbageCollection ( ) ;
111+ await ForceGarbageCollectionAsync ( ) ;
111112 instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
112113 instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
113114 }
114115
115- [ Test , Retry ( 3 ) ]
116+ [ Test ]
116117 public async Task ScopedFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
117118 {
118119 WeakReference instanceConstructedByChild ;
@@ -129,13 +130,18 @@ void Act()
129130 }
130131
131132 Act ( ) ;
132- await ForceGarbageCollection ( ) ;
133+ await ForceGarbageCollectionAsync ( ) ;
133134 instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
134135 instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
135136 }
136137
137- [ Test , Retry ( 3 ) ]
138- public async Task TransientFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
138+ /// <summary>
139+ /// Because Transient instances are not tracked by the container,
140+ /// so they should be collected as soon as there are no references to them,
141+ /// even if the container that constructed them is still alive.
142+ /// </summary>
143+ [ Test ]
144+ public async Task TransientFromType_ConstructedInstances_ShouldBeCollected_BeforeConstructingContainerIsDisposed ( )
139145 {
140146 WeakReference instanceConstructedByChild ;
141147 WeakReference instanceConstructedByParent ;
@@ -151,13 +157,18 @@ void Act()
151157 }
152158
153159 Act ( ) ;
154- await ForceGarbageCollection ( ) ;
160+ await ForceGarbageCollectionAsync ( ) ;
155161 instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
156- instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
162+ instanceConstructedByParent . IsAlive . Should ( ) . BeFalse ( ) ;
157163 }
158164
159- [ Test , Retry ( 3 ) ]
160- public async Task TransientFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
165+ /// <summary>
166+ /// Because Transient instances are not tracked by the container,
167+ /// so they should be collected as soon as there are no references to them,
168+ /// even if the container that constructed them is still alive.
169+ /// </summary>
170+ [ Test ]
171+ public async Task TransientFromFactory_ConstructedInstances_ShouldBeCollected_BeforeConstructingContainerIsDisposed ( )
161172 {
162173 WeakReference instanceConstructedByChild ;
163174 WeakReference instanceConstructedByParent ;
@@ -173,9 +184,9 @@ void Act()
173184 }
174185
175186 Act ( ) ;
176- await ForceGarbageCollection ( ) ;
187+ await ForceGarbageCollectionAsync ( ) ;
177188 instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
178- instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
189+ instanceConstructedByParent . IsAlive . Should ( ) . BeFalse ( ) ;
179190 }
180191 }
181192}
0 commit comments