Repository navigation
Expand file tree
/
Copy pathServiceCollectionExtensions.Decorators.Disposal.Tests.cs
More file actions
172 lines (164 loc) · 7.21 KB
/
Copy pathServiceCollectionExtensions.Decorators.Disposal.Tests.cs
File metadata and controls
172 lines (164 loc) · 7.21 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using Microsoft.Extensions.DependencyInjection;
namespace Mammoth.Extensions.DependencyInjection.Tests;
[TestClass]
public class DecoratorDisposalTests
{
[TestMethod]
[DataRow(ServiceLifetime.Transient, false, false)]
[DataRow(ServiceLifetime.Scoped, false, false)]
[DataRow(ServiceLifetime.Singleton, false, false)]
[DataRow(ServiceLifetime.Transient, true, false)]
[DataRow(ServiceLifetime.Scoped, true, false)]
[DataRow(ServiceLifetime.Singleton, true, false)]
[DataRow(ServiceLifetime.Transient, false, true)]
[DataRow(ServiceLifetime.Scoped, false, true)]
[DataRow(ServiceLifetime.Singleton, false, true)]
[DataRow(ServiceLifetime.Transient, true, true)]
[DataRow(ServiceLifetime.Scoped, true, true)]
[DataRow(ServiceLifetime.Singleton, true, true)]
public void ContainerOwnsInnerAndIntermediateDecorators(ServiceLifetime lifetime, bool keyed, bool typeRegistration)
{
IServiceCollection services = new ServiceCollection();
object? key = keyed ? "work" : null;
if (typeRegistration)
services.Add(new ServiceDescriptor(typeof(Work), key, typeof(Work), lifetime));
else if (keyed)
services.Add(new ServiceDescriptor(typeof(Work), key, (_, actualKey) =>
{
Assert.AreEqual(key, actualKey);
return new Work();
}, lifetime));
else
services.Add(new ServiceDescriptor(typeof(Work), _ => new Work(), lifetime));
services.Decorate<Work, WorkDecorator>();
services.Decorate<Work, WorkDecorator>();
var provider = services.BuildServiceProvider();
var scope = provider.CreateScope();
var outer = (WorkDecorator)scope.ServiceProvider.GetRequiredKeyedService<Work>(key);
var middle = (WorkDecorator)outer.Inner;
var inner = middle.Inner;
var second = scope.ServiceProvider.GetRequiredKeyedService<Work>(key);
if (lifetime == ServiceLifetime.Transient)
Assert.AreNotSame(outer, second);
else
Assert.AreSame(outer, second);
scope.Dispose();
Assert.AreEqual(lifetime == ServiceLifetime.Singleton ? 0 : 1, inner.DisposeCount);
provider.Dispose();
Assert.AreEqual(1, inner.DisposeCount);
Assert.AreEqual(1, middle.DisposeCount);
Assert.AreEqual(1, outer.DisposeCount);
}
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public void CallerOwnedInstanceIsNotDisposed(bool keyed)
{
IServiceCollection services = new ServiceCollection();
var inner = new Work();
object? key = keyed ? "work" : null;
services.Add(new ServiceDescriptor(typeof(Work), key, inner));
services.Decorate<Work, WorkDecorator>();
services.Decorate<Work, WorkDecorator>();
var provider = services.BuildServiceProvider();
var outer = (WorkDecorator)provider.GetRequiredKeyedService<Work>(key);
provider.Dispose();
Assert.AreSame(inner, ((WorkDecorator)outer.Inner).Inner);
Assert.AreEqual(0, inner.DisposeCount);
Assert.AreEqual(1, ((WorkDecorator)outer.Inner).DisposeCount);
Assert.AreEqual(1, outer.DisposeCount);
}
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public async Task AsyncInnerRemainsOwned(bool keyed)
{
IServiceCollection services = new ServiceCollection();
var inner = new AsyncWork();
object? key = keyed ? "work" : null;
if (keyed)
services.AddKeyedScoped<IAsyncWork>(key, (_, _) => inner);
else
services.AddScoped<IAsyncWork>(_ => inner);
services.Decorate<IAsyncWork, AsyncDecorator>();
services.Decorate<IAsyncWork, AsyncDecorator>();
var provider = services.BuildServiceProvider();
var scope = provider.CreateAsyncScope();
var outer = (AsyncDecorator)scope.ServiceProvider.GetRequiredKeyedService<IAsyncWork>(key);
await scope.DisposeAsync();
await provider.DisposeAsync();
Assert.AreEqual(1, inner.DisposeCount);
Assert.AreEqual(1, ((AsyncDecorator)outer.Inner).DisposeCount);
Assert.AreEqual(1, outer.DisposeCount);
}
[TestMethod]
public void ThrowingDecoratorDoesNotLoseInnerOwnership()
{
IServiceCollection services = new ServiceCollection();
var inner = new Work();
services.AddScoped<Work>(_ => inner);
services.Decorate<Work, ThrowingDecorator>();
using var provider = services.BuildServiceProvider();
var scope = provider.CreateScope();
Assert.ThrowsExactly<InvalidOperationException>(() => scope.ServiceProvider.GetRequiredService<Work>());
scope.Dispose();
Assert.AreEqual(1, inner.DisposeCount);
}
[TestMethod]
public void ImplementationTypeRegistrationDoesNotCollideWithUnrelatedRegistration()
{
IServiceCollection services = new ServiceCollection();
var unrelated = new Work();
services.AddSingleton(unrelated);
services.AddScoped<IWork, Work>();
services.Decorate<IWork, InterfaceDecorator>();
using var provider = ServiceProviderFactory.CreateServiceProvider(services);
using var scope = provider.CreateScope();
var decorated = (InterfaceDecorator)scope.ServiceProvider.GetRequiredService<IWork>();
Assert.AreSame(unrelated, scope.ServiceProvider.GetRequiredService<Work>());
Assert.AreNotSame(unrelated, decorated.Inner);
Assert.HasCount(1, scope.ServiceProvider.GetAllServices<IWork>().ToArray());
}
[TestMethod]
public void NativeFactoryControlDisposesInner()
{
var inner = new Work();
var services = new ServiceCollection();
services.AddScoped<Work>(_ => inner);
using var provider = services.BuildServiceProvider();
var scope = provider.CreateScope();
Assert.AreSame(inner, scope.ServiceProvider.GetRequiredService<Work>());
scope.Dispose();
Assert.AreEqual(1, inner.DisposeCount);
}
public interface IWork { }
public class Work : IWork, IDisposable
{
public int DisposeCount { get; private set; }
public void Dispose() => DisposeCount++;
}
public sealed class WorkDecorator(Work inner) : Work
{
public Work Inner { get; } = inner;
}
public sealed class ThrowingDecorator : Work
{
public ThrowingDecorator(Work inner) => throw new InvalidOperationException("Decorator failed");
}
public sealed class InterfaceDecorator(IWork inner) : IWork
{
public IWork Inner { get; } = inner;
}
public interface IAsyncWork { }
public sealed class AsyncWork : IAsyncWork, IAsyncDisposable
{
public int DisposeCount { get; private set; }
public ValueTask DisposeAsync() { DisposeCount++; return default; }
}
public sealed class AsyncDecorator(IAsyncWork inner) : IAsyncWork, IAsyncDisposable
{
public IAsyncWork Inner { get; } = inner;
public int DisposeCount { get; private set; }
public ValueTask DisposeAsync() { DisposeCount++; return default; }
}
}