diff --git a/test/Autofac.Specification.Test/Lifetime/DisposalTests.cs b/test/Autofac.Specification.Test/Lifetime/DisposalTests.cs index 2c73e8015..691d32092 100644 --- a/test/Autofac.Specification.Test/Lifetime/DisposalTests.cs +++ b/test/Autofac.Specification.Test/Lifetime/DisposalTests.cs @@ -99,6 +99,53 @@ public void ResolvingFromAnEndedLifetimeProducesObjectDisposedException() Assert.Throws(() => lifetime.Resolve()); } + // Issue #960 + [Fact] + public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains() + { + var builder = new ContainerBuilder(); + builder.RegisterType().InstancePerLifetimeScope(); + + // 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())); + var middleInstance = middle.Resolve(); + + // Child has ExternalRegistration pointing upwards. + var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object())); + 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 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. + 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); + } + private class A : DisposeTracker { } diff --git a/test/Autofac.Specification.Test/Registration/TypeRegistrationTests.cs b/test/Autofac.Specification.Test/Registration/TypeRegistrationTests.cs index a9b004c64..355d748e9 100644 --- a/test/Autofac.Specification.Test/Registration/TypeRegistrationTests.cs +++ b/test/Autofac.Specification.Test/Registration/TypeRegistrationTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using Xunit; namespace Autofac.Specification.Test.Registration @@ -65,6 +67,21 @@ public void AsSelfNonGeneric() context.Resolve(); } + [Fact] + public void OneTypeImplementMultipleInterfaces_OtherObjectsImplementingOneOfThoseInterfaces_CanBeResolved() + { + var builder = new ContainerBuilder(); + builder.RegisterType(typeof(ABC)).As(typeof(IA), typeof(IB)); + builder.RegisterType(typeof(A)).As(typeof(IA)); + + var container = builder.Build(); + var lifetime = container.BeginLifetimeScope(cb => cb.Register(_ => new object())); + Assert.NotNull(lifetime.Resolve()); + + var allImplementationsOfServiceA = lifetime.Resolve>(); + Assert.Equal(2, allImplementationsOfServiceA.Count()); + } + [Fact] public void RegisterTypeAsSupportedAndUnsupportedService() { diff --git a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs b/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs deleted file mode 100644 index 56a54b198..000000000 --- a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Xunit; - -namespace Autofac.Test.Core.Registration -{ - public class ExternalRegistrySourceTests - { - public interface IServiceA - { - } - - public interface IServiceB - { - } - - public class ClassA : IServiceA, IServiceB, IDisposable - { - public bool IsDisposed { get; private set; } - - public void Dispose() - { - IsDisposed = true; - } - } - - public class ClassB : IServiceA, IDisposable - { - public bool IsDisposed { get; private set; } - - public void Dispose() - { - IsDisposed = true; - } - } - - [Fact] - public void OneTypeImplementTwoInterfaces_OtherObjectsImplementingOneOfThoseInterfaces_CanBeResolved() - { - var builder = new ContainerBuilder(); - builder.RegisterType(typeof(ClassA)).As(typeof(IServiceA), typeof(IServiceB)); - builder.RegisterType(typeof(ClassB)).As(typeof(IServiceA)); - - var container = builder.Build(); - var lifetime = container.BeginLifetimeScope(cb => cb.Register(_ => new object())); - lifetime.Resolve(); - - var allImplementationsOfServiceA = lifetime.Resolve>(); - Assert.Equal(2, allImplementationsOfServiceA.Count()); - } - - // Issue #960 - [Fact] - public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains() - { - var builder = new ContainerBuilder(); - builder.RegisterType().InstancePerLifetimeScope(); - - // 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())); - var middleInstance = middle.Resolve(); - - // Child has ExternalRegistration pointing upwards. - var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object())); - 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 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. - 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); - } - } -}