diff --git a/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue429_Memory_leak_on_MS_DI_with_Disposable_Transient.cs b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue429_Memory_leak_on_MS_DI_with_Disposable_Transient.cs index d9998ef4..896fcdb4 100644 --- a/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue429_Memory_leak_on_MS_DI_with_Disposable_Transient.cs +++ b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue429_Memory_leak_on_MS_DI_with_Disposable_Transient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using NUnit.Framework; using Microsoft.Extensions.DependencyInjection; @@ -8,35 +9,53 @@ namespace DryIoc.Microsoft.DependencyInjection.Specification.Tests public class GHIssue429_Memory_leak_on_MS_DI_with_Disposable_Transient { [Test] - public void Test1() + public void Test_with_MS_DI_rules() { + DisposableViewModel[] xs = null; using (var container = new Container() - .WithDependencyInjectionAdapter(new ServiceCollection()) - .With(rules => rules.WithoutTrackingDisposableTransients())) + .WithDependencyInjectionAdapter(new ServiceCollection())) { container.Register(Reuse.Transient); - ResolveManyTimes(container, typeof(DisposableViewModel)); + xs = ResolveManyTimes(container); } + + foreach (var x in xs) + Assert.IsTrue(x.IsDisposed); } - private static void ResolveManyTimes(IResolver container, Type typeToResolve) + [Test] + public void Test_without_disposable_transient() { - for (var i = 0; i < 100; i++) + DisposableViewModel[] xs = null; + using (var container = new Container() + .WithDependencyInjectionAdapter(new ServiceCollection()) + .With(rules => rules.WithoutTrackingDisposableTransients())) { - container.Resolve(typeToResolve); + container.Register(Reuse.Transient); + + xs = ResolveManyTimes(container); } + + foreach (var x in xs) + Assert.IsFalse(x.IsDisposed); } - internal class NotDisposableViewModel + private static T[] ResolveManyTimes(IResolver container, int times = 10) { + var xs = new T[times]; + for (var i = 0; i < times; i++) + { + xs[i] = container.Resolve(); + } + + return xs; } internal class DisposableViewModel : IDisposable { - public void Dispose() - { - } + public bool IsDisposed; + public void Dispose() => IsDisposed = true; } } }