diff --git a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs b/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs index d1eabadef..56a54b198 100644 --- a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs +++ b/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; @@ -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] @@ -46,21 +59,42 @@ public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRema // Root has the main registration. var root = builder.Build(); + var rootInstance = root.Resolve(); // Middle has ExternalRegistration pointing upwards. var middle = root.BeginLifetimeScope(cb => cb.Register(_ => new object())); - middle.Resolve(); + var middleInstance = middle.Resolve(); // Child has ExternalRegistration pointing upwards. var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object())); - child.Resolve(); + var childInstance = child.Resolve(); + + 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(); + Assert.Same(middleInstance, middle.Resolve()); + Assert.Same(rootInstance, root.Resolve()); + + // 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); } } }