Skip to content

Commit

Permalink
Additional assertions in new unit test for issue #960.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmg committed Feb 18, 2019
1 parent cb91ecd commit 831973f
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand All @@ -14,12 +15,24 @@ public interface IServiceB
{
}

public class ClassA : IServiceA, IServiceB
public class ClassA : IServiceA, IServiceB, IDisposable
{
public bool IsDisposed { get; private set; }

public void Dispose()
{
IsDisposed = true;
}
}

public class ClassB : IServiceA
public class ClassB : IServiceA, IDisposable
{
public bool IsDisposed { get; private set; }

public void Dispose()
{
IsDisposed = true;
}
}

[Fact]
Expand All @@ -46,21 +59,42 @@ public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRema

// Root has the main registration.
var root = builder.Build();
var rootInstance = root.Resolve<ClassA>();

// Middle has ExternalRegistration pointing upwards.
var middle = root.BeginLifetimeScope(cb => cb.Register(_ => new object()));
middle.Resolve<ClassA>();
var middleInstance = middle.Resolve<ClassA>();

// Child has ExternalRegistration pointing upwards.
var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object()));
child.Resolve<ClassA>();
var childInstance = child.Resolve<ClassA>();

Assert.NotSame(rootInstance, middleInstance);
Assert.NotSame(middleInstance, childInstance);
Assert.NotSame(rootInstance, childInstance);

// This should only dispose the registration in child, not the one in middle.
// This should only dispose the registration in child, not the one in middle or root.
child.Dispose();
Assert.True(childInstance.IsDisposed);
Assert.False(middleInstance.IsDisposed);
Assert.False(rootInstance.IsDisposed);

// We check by trying to use the registration in middle.
// If too much got disposed, this will throw ObjectDisposedException.
middle.Resolve<ClassA>();
Assert.Same(middleInstance, middle.Resolve<ClassA>());
Assert.Same(rootInstance, root.Resolve<ClassA>());

// Middle and child should now be disposed.
middle.Dispose();
Assert.True(childInstance.IsDisposed);
Assert.True(middleInstance.IsDisposed);
Assert.False(rootInstance.IsDisposed);

// Now all should be disposed.
root.Dispose();
Assert.True(childInstance.IsDisposed);
Assert.True(middleInstance.IsDisposed);
Assert.True(rootInstance.IsDisposed);
}
}
}

0 comments on commit 831973f

Please sign in to comment.