-
-
Notifications
You must be signed in to change notification settings - Fork 833
/
ExternalRegistrySourceTests.cs
100 lines (82 loc) · 3.43 KB
/
ExternalRegistrySourceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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<IServiceB>();
var allImplementationsOfServiceA = lifetime.Resolve<IEnumerable<IServiceA>>();
Assert.Equal(2, allImplementationsOfServiceA.Count());
}
// Issue #960
[Fact]
public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains()
{
var builder = new ContainerBuilder();
builder.RegisterType<ClassA>().InstancePerLifetimeScope();
// 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()));
var middleInstance = middle.Resolve<ClassA>();
// Child has ExternalRegistration pointing upwards.
var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object()));
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 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<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);
}
}
}