Skip to content

Commit

Permalink
Split external registry source tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Mar 14, 2019
1 parent 4ca309a commit 5df69e4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 100 deletions.
47 changes: 47 additions & 0 deletions test/Autofac.Specification.Test/Lifetime/DisposalTests.cs
Expand Up @@ -99,6 +99,53 @@ public void ResolvingFromAnEndedLifetimeProducesObjectDisposedException()
Assert.Throws<ObjectDisposedException>(() => lifetime.Resolve<object>());
}

// Issue #960
[Fact]
public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains()
{
var builder = new ContainerBuilder();
builder.RegisterType<DisposeTracker>().InstancePerLifetimeScope();

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

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

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

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<DisposeTracker>());
Assert.Same(rootInstance, root.Resolve<DisposeTracker>());

// 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
{
}
Expand Down
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Autofac.Specification.Test.Registration
Expand Down Expand Up @@ -65,6 +67,21 @@ public void AsSelfNonGeneric()
context.Resolve<ABC>();
}

[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<IB>());

var allImplementationsOfServiceA = lifetime.Resolve<IEnumerable<IA>>();
Assert.Equal(2, allImplementationsOfServiceA.Count());
}

[Fact]
public void RegisterTypeAsSupportedAndUnsupportedService()
{
Expand Down
100 changes: 0 additions & 100 deletions test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs

This file was deleted.

0 comments on commit 5df69e4

Please sign in to comment.