Skip to content

Commit c62b281

Browse files
possible fix for GC tests
1 parent 4c6c5e0 commit c62b281

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

Assets/Reflex.EditModeTests/GarbageCollectionTests.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using FluentAssertions;
56
using NUnit.Framework;
67
using Reflex.Core;
@@ -17,11 +18,14 @@ private class Service
1718
{
1819
}
1920

20-
public static void ForceGarbageCollection()
21+
public static async Task ForceGarbageCollection()
2122
{
2223
Resources.UnloadUnusedAssets();
2324
GC.Collect();
2425
GC.WaitForPendingFinalizers();
26+
await Task.Yield();
27+
GC.Collect();
28+
GC.WaitForPendingFinalizers();
2529
}
2630

2731
[OneTimeSetUp]
@@ -34,7 +38,7 @@ public void Setup()
3438
}
3539

3640
[Test, Retry(3)]
37-
public void Singleton_ShouldBeFinalized_WhenOwnerIsDisposed()
41+
public async Task Singleton_ShouldBeFinalized_WhenOwnerIsDisposed()
3842
{
3943
var references = new List<WeakReference>();
4044

@@ -47,12 +51,12 @@ void Act()
4751
}
4852

4953
Act();
50-
ForceGarbageCollection();
54+
await ForceGarbageCollection();
5155
references.Any(r => r.IsAlive).Should().BeFalse();
5256
}
5357

5458
[Test, Retry(3)]
55-
public void DisposedScopedContainer_ShouldHaveNoReferencesToItself_AndShouldBeCollectedAndFinalized()
59+
public async Task DisposedScopedContainer_ShouldHaveNoReferencesToItself_AndShouldBeCollectedAndFinalized()
5660
{
5761
var references = new List<WeakReference>();
5862

@@ -65,12 +69,12 @@ void Act()
6569
}
6670

6771
Act();
68-
ForceGarbageCollection();
72+
await ForceGarbageCollection();
6973
references.Any(r => r.IsAlive).Should().BeFalse();
7074
}
7175

7276
[Test, Retry(3)]
73-
public void Construct_ContainerShouldNotControlConstructedObjectLifeCycle_ByNotKeepingReferenceToIt()
77+
public async Task Construct_ContainerShouldNotControlConstructedObjectLifeCycle_ByNotKeepingReferenceToIt()
7478
{
7579
var references = new List<WeakReference>();
7680
var container = new ContainerBuilder().Build();
@@ -82,7 +86,7 @@ void Act()
8286
}
8387

8488
Act();
85-
ForceGarbageCollection();
89+
await ForceGarbageCollection();
8690
references.Any(r => r.IsAlive).Should().BeFalse();
8791
}
8892
}

Assets/Reflex.EditModeTests/ScopedTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using FluentAssertions;
45
using NUnit.Framework;
56
using Reflex.Core;
@@ -94,7 +95,7 @@ public void ScopedFromFactory_ConstructedInstances_ShouldBeDisposed_WithinConstr
9495
}
9596

9697
[Test, Retry(3)]
97-
public void ScopedFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
98+
public async Task ScopedFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
9899
{
99100
WeakReference instanceConstructedByChild;
100101
WeakReference instanceConstructedByParent;
@@ -110,13 +111,13 @@ void Act()
110111
}
111112

112113
Act();
113-
GarbageCollectionTests.ForceGarbageCollection();
114+
await GarbageCollectionTests.ForceGarbageCollection();
114115
instanceConstructedByChild.IsAlive.Should().BeFalse();
115116
instanceConstructedByParent.IsAlive.Should().BeTrue();
116117
}
117118

118119
[Test, Retry(3)]
119-
public void ScopedFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
120+
public async Task ScopedFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
120121
{
121122
WeakReference instanceConstructedByChild;
122123
WeakReference instanceConstructedByParent;
@@ -132,7 +133,7 @@ void Act()
132133
}
133134

134135
Act();
135-
GarbageCollectionTests.ForceGarbageCollection();
136+
await GarbageCollectionTests.ForceGarbageCollection();
136137
instanceConstructedByChild.IsAlive.Should().BeFalse();
137138
instanceConstructedByParent.IsAlive.Should().BeTrue();
138139
}

Assets/Reflex.EditModeTests/TransientTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using FluentAssertions;
34
using NUnit.Framework;
45
using Reflex.Core;
@@ -49,7 +50,7 @@ public void TransientFromFactory_ConstructedInstances_ShouldBeDisposed_WithinCon
4950
}
5051

5152
[Test, Retry(3)]
52-
public void TransientFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
53+
public async Task TransientFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
5354
{
5455
WeakReference instanceConstructedByChild;
5556
WeakReference instanceConstructedByParent;
@@ -65,13 +66,13 @@ void Act()
6566
}
6667

6768
Act();
68-
GarbageCollectionTests.ForceGarbageCollection();
69+
await GarbageCollectionTests.ForceGarbageCollection();
6970
instanceConstructedByChild.IsAlive.Should().BeFalse();
7071
instanceConstructedByParent.IsAlive.Should().BeTrue();
7172
}
7273

7374
[Test, Retry(3)]
74-
public void TransientFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
75+
public async Task TransientFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
7576
{
7677
WeakReference instanceConstructedByChild;
7778
WeakReference instanceConstructedByParent;
@@ -87,7 +88,7 @@ void Act()
8788
}
8889

8990
Act();
90-
GarbageCollectionTests.ForceGarbageCollection();
91+
await GarbageCollectionTests.ForceGarbageCollection();
9192
instanceConstructedByChild.IsAlive.Should().BeFalse();
9293
instanceConstructedByParent.IsAlive.Should().BeTrue();
9394
}

0 commit comments

Comments
 (0)