Skip to content

Commit

Permalink
Align with the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrnikitin committed Jan 31, 2024
1 parent 5e34a59 commit 4b9f00c
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@ public class Issue771_NSubstituteLeakTests
{
public class Foo;

public interface IInterface
{
public void Method(Foo foo);
}
private readonly IInterface _interfaceMock = Substitute.For<IInterface>();

[Test]
public void NSubstituteDoesNotLeak()
public async Task NSubstituteDoesNotLeak()
{
WeakReference<Foo> weakFoo;
{
var foo = new Foo();
weakFoo = new WeakReference<Foo>(foo);
_interfaceMock.Method(foo);
_interfaceMock.ClearReceivedCalls();
}

GC.Collect();
GC.WaitForPendingFinalizers();
await GcCollect();

Assert.False(weakFoo.TryGetTarget(out _));
}

private static async Task GcCollect()
{
for (var i = 0; i < 3; i++)
{
await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}


0 comments on commit 4b9f00c

Please sign in to comment.