Skip to content

Commit

Permalink
more robust tests for #429
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Oct 10, 2021
1 parent fd50148 commit bc1362f
Showing 1 changed file with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -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<DisposableViewModel>(Reuse.Transient);

ResolveManyTimes(container, typeof(DisposableViewModel));
xs = ResolveManyTimes<DisposableViewModel>(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<DisposableViewModel>(Reuse.Transient);

xs = ResolveManyTimes<DisposableViewModel>(container);
}

foreach (var x in xs)
Assert.IsFalse(x.IsDisposed);
}

internal class NotDisposableViewModel
private static T[] ResolveManyTimes<T>(IResolver container, int times = 10)
{
var xs = new T[times];
for (var i = 0; i < times; i++)
{
xs[i] = container.Resolve<T>();
}

return xs;
}

internal class DisposableViewModel : IDisposable
{
public void Dispose()
{
}
public bool IsDisposed;
public void Dispose() => IsDisposed = true;
}
}
}

0 comments on commit bc1362f

Please sign in to comment.