Skip to content

Commit

Permalink
- collect harder
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Oct 1, 2024
1 parent 5c62ed6 commit be001e2
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Core/tests/UnitTests/WeakListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public class WeakListTests
public async Task ObjectsAreEvicted_GetEnumerator()
{
var expected = new object();
var list = new WeakList<object> { expected, new object() };
var evict = new object();
WeakReference weakReference = new WeakReference(evict);
var list = new WeakList<object> { expected, evict };
evict = null;
list.CleanupThreshold = 1;

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();

Assert.False(await weakReference.WaitForCollect(), "Object failed to be collected");

foreach (var item in list)
{
Expand All @@ -31,12 +32,13 @@ public async Task ObjectsAreEvicted_GetEnumerator()
public async Task ObjectsAreEvicted_Remove()
{
var expected = new object();
var list = new WeakList<object> { expected, new object() };
var evict = new object();
WeakReference weakReference = new WeakReference(evict);
var list = new WeakList<object> { expected, evict };
evict = null;
list.CleanupThreshold = 1;

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.False(await weakReference.WaitForCollect(), "Object failed to be collected");

list.Remove(expected);

Expand All @@ -47,14 +49,16 @@ public async Task ObjectsAreEvicted_Remove()
public async Task ObjectsAreEvicted_Add()
{
var expected = new object();
var list = new WeakList<object> { expected, new object() };
var evict = new object();
WeakReference weakReference = new WeakReference(evict);
var list = new WeakList<object> { expected, evict };
evict = null;
list.CleanupThreshold = 1;

Assert.False(await weakReference.WaitForCollect(), "Object failed to be collected");

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();

list.Add(new object());
var triggerCleanup = new object();
list.Add(triggerCleanup);

Assert.Equal(2, list.Count);
Assert.Equal(expected, list.First());
Expand Down

0 comments on commit be001e2

Please sign in to comment.