Repository navigation
Expand file tree
/
Copy pathServiceProviderFactory.Validation.Tests.cs
More file actions
260 lines (243 loc) · 12.2 KB
/
Copy pathServiceProviderFactory.Validation.Tests.cs
File metadata and controls
260 lines (243 loc) · 12.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using Microsoft.Extensions.DependencyInjection;
namespace Mammoth.Extensions.DependencyInjection.Tests;
[TestClass]
public class ServiceProviderFactoryValidationTests
{
[TestMethod]
[DataRow(false, false)]
[DataRow(false, true)]
[DataRow(true, false)]
[DataRow(true, true)]
public void DiagnosticsRequireBuildValidationBeforeTouchingRegistrations(bool explicitlyDisabled, bool factoryEntryPoint)
{
IServiceCollection services = new ServiceCollection();
var activations = 0;
services.AddSingleton<First>(_ => { activations++; return new First(); });
services.AddTransient<CycleA>();
services.AddTransient<CycleB>();
var original = services.ToArray();
var options = new ExtendedServiceProviderOptions { DetectIncorrectUsageOfTransientDisposables = true };
if (explicitlyDisabled) options.ValidateOnBuild = false;
var error = Assert.ThrowsExactly<ArgumentException>(() =>
{
if (factoryEntryPoint)
{
IServiceProviderFactory<IServiceCollection> factory = new ServiceProviderFactory(options);
factory.CreateServiceProvider(factory.CreateBuilder(services));
}
else ServiceProviderFactory.CreateServiceProvider(services, options);
});
Assert.AreEqual("options", error.ParamName);
StringAssert.Contains(error.Message, "ValidateOnBuild");
Assert.AreEqual(0, activations);
Assert.IsFalse(options.ValidateOnBuild, "Do not silently change caller options.");
CollectionAssert.AreEqual(original, services.ToArray());
options.ValidateOnBuild = true;
var cycle = Assert.ThrowsExactly<AggregateException>(() => ServiceProviderFactory.CreateServiceProvider(services, options));
StringAssert.Contains(cycle.ToString(), "circular dependency");
Assert.AreEqual(0, activations);
CollectionAssert.AreEqual(original, services.ToArray());
}
public static IEnumerable<object[]> ConstructorCycles()
{
foreach (var mode in new[] { "native", "disabled", "enabled" })
foreach (var lifetime in new[] { ServiceLifetime.Transient, ServiceLifetime.Scoped, ServiceLifetime.Singleton })
foreach (var keyed in new[] { false, true })
foreach (var self in new[] { false, true })
yield return [mode, lifetime, keyed, self];
}
[TestMethod]
[DynamicData(nameof(ConstructorCycles))]
public void NativeBuildValidationRejectsConstructorCycles(string mode, ServiceLifetime lifetime, bool keyed, bool self)
{
IServiceCollection services = new ServiceCollection();
var types = self ? new[] { typeof(SelfCycle) } : new[] { typeof(CycleA), typeof(CycleB) };
foreach (var type in types)
services.Add(keyed ? ServiceDescriptor.DescribeKeyed(type, "cycle", type, lifetime)
: ServiceDescriptor.Describe(type, type, lifetime));
var error = Assert.ThrowsExactly<AggregateException>(() => Build(services, mode));
StringAssert.Contains(error.ToString(), "circular dependency");
StringAssert.Contains(error.ToString(), types[0].Name);
if (!self) StringAssert.Contains(error.ToString(), nameof(CycleB));
}
public static IEnumerable<object[]> InvalidGraphs()
{
foreach (var mode in new[] { "native", "disabled", "enabled" })
foreach (var keyed in new[] { false, true })
foreach (var graph in new[] { "missing", "ambiguous", "capture", "indirect-capture" })
yield return [mode, keyed, graph];
}
[TestMethod]
[DynamicData(nameof(InvalidGraphs))]
public void ValidateOnBuildRejectsInvalidOriginalGraph(string mode, bool keyed, string graph)
{
IServiceCollection services = new ServiceCollection();
var type = graph switch
{
"missing" => typeof(NeedsMissing),
"ambiguous" => typeof(Ambiguous),
"capture" => typeof(CapturesScoped),
_ => typeof(IndirectCapture)
};
services.AddSingleton<First>();
services.AddSingleton<Second>();
services.AddScoped<Scoped>();
services.AddTransient<CapturesScoped>();
services.AddKeyedScoped<Scoped>("dependency");
services.AddKeyedTransient<KeyedCapture>("dependency");
if (keyed)
{
type = graph switch
{
"missing" => typeof(KeyedMissing),
"capture" => typeof(KeyedCapture),
"indirect-capture" => typeof(KeyedIndirectCapture),
_ => type
};
services.Add(ServiceDescriptor.DescribeKeyed(type, "consumer", type,
graph.Contains("capture") ? ServiceLifetime.Singleton : ServiceLifetime.Transient));
}
else
services.Add(ServiceDescriptor.Describe(type, type,
graph.Contains("capture") ? ServiceLifetime.Singleton : ServiceLifetime.Transient));
var error = Assert.ThrowsExactly<AggregateException>(() => Build(services, mode));
StringAssert.Contains(error.ToString(), type.Name);
StringAssert.Contains(error.ToString(), graph switch
{
"missing" => "Unable to resolve service",
"ambiguous" => "ambiguous",
_ => "Cannot consume scoped service"
});
}
[TestMethod]
[DataRow("native")]
[DataRow("disabled")]
[DataRow("enabled")]
public void ValidationDoesNotActivateFactoriesConstructorsOrDisposeCallerInstances(string mode)
{
var counts = new Counts();
var instance = new CallerOwned();
IServiceCollection services = new ServiceCollection();
services.AddSingleton(counts);
services.AddSingleton(instance);
services.AddKeyedSingleton("instance", instance);
services.AddSingleton<Constructed>();
services.AddKeyedSingleton<Constructed>("constructed");
services.AddSingleton(_ => { counts.Factories++; return new First(); });
services.AddKeyedSingleton("factory", (_, _) => { counts.Factories++; return new Second(); });
services.AddTransient<MetadataConsumer>();
// Native validation deliberately treats user factories and open generics as opaque.
services.AddTransient(typeof(OpenGeneric<>));
using (var provider = Build(services, mode))
{
Assert.AreEqual(0, counts.Constructors);
Assert.AreEqual(0, counts.Factories);
Assert.AreEqual(0, instance.Disposals);
Assert.IsNotNull(provider.GetRequiredService<MetadataConsumer>().Types);
Assert.AreSame(instance, provider.GetRequiredService<CallerOwned>());
Assert.AreSame(instance, provider.GetRequiredKeyedService<CallerOwned>("instance"));
var first = provider.GetRequiredService<Constructed>();
Assert.AreSame(first, provider.GetRequiredService<Constructed>());
provider.GetRequiredKeyedService<Constructed>("constructed");
provider.GetRequiredService<First>();
provider.GetRequiredKeyedService<Second>("factory");
Assert.AreEqual(2, counts.Constructors);
Assert.AreEqual(2, counts.Factories);
}
Assert.AreEqual(0, instance.Disposals);
}
[TestMethod]
[DataRow("native", false)]
[DataRow("disabled", false)]
[DataRow("native", true)]
[DataRow("disabled", true)]
[DataRow("enabled", true)]
public void ValidationOptionsRemainIndependent(string mode, bool validateOnBuild)
{
IServiceCollection services = new ServiceCollection();
services.AddScoped<Scoped>();
services.AddSingleton<CapturesScoped>();
if (!validateOnBuild)
services.AddTransient<NeedsMissing>();
using var provider = Build(services, mode, validateOnBuild, validateScopes: false);
Assert.IsNotNull(provider.GetRequiredService<CapturesScoped>());
}
[TestMethod]
[DataRow("native")]
[DataRow("disabled")]
[DataRow("enabled")]
public void ValidationLeavesUserFactoriesOpaque(string mode)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<NeedsMissing>(_ => throw new InvalidOperationException("factory invoked"));
services.AddKeyedSingleton<KeyedMissing>("consumer", (_, _) => throw new InvalidOperationException("keyed factory invoked"));
using var provider = Build(services, mode);
}
[TestMethod]
[DataRow("native")]
[DataRow("disabled")]
public void ScopeValidationAloneDoesNotEnableBuildValidation(string mode)
{
IServiceCollection services = new ServiceCollection();
services.AddTransient<NeedsMissing>();
using var provider = Build(services, mode, validateOnBuild: false, validateScopes: true);
}
[TestMethod]
[DataRow("native")]
[DataRow("disabled")]
[DataRow("enabled")]
public void ValidScopedAndKeyedGraphsStillResolveAfterValidation(string mode)
{
IServiceCollection services = new ServiceCollection();
services.AddScoped<Scoped>();
services.AddScoped<CapturesScoped>();
services.AddKeyedScoped<Scoped>("dependency");
services.AddKeyedScoped<KeyedCapture>("consumer");
using var provider = Build(services, mode);
using var scope = provider.CreateScope();
Assert.AreSame(scope.ServiceProvider.GetRequiredService<Scoped>(),
scope.ServiceProvider.GetRequiredService<CapturesScoped>().Dependency);
Assert.AreSame(scope.ServiceProvider.GetRequiredKeyedService<Scoped>("dependency"),
scope.ServiceProvider.GetRequiredKeyedService<KeyedCapture>("consumer").Dependency);
}
private static ServiceProvider Build(IServiceCollection services, string mode,
bool validateOnBuild = true, bool validateScopes = true)
{
if (mode == "native")
{
// Native control needs the metadata type explicitly; Mammoth supplies it itself.
services.AddSingleton(new ServiceTypes());
return services.BuildServiceProvider(new ServiceProviderOptions
{ ValidateOnBuild = validateOnBuild, ValidateScopes = validateScopes });
}
return ServiceProviderFactory.CreateServiceProvider(services, new ExtendedServiceProviderOptions
{
ValidateOnBuild = validateOnBuild,
ValidateScopes = validateScopes,
DetectIncorrectUsageOfTransientDisposables = mode == "enabled"
});
}
public interface IMissing;
public sealed class NeedsMissing(IMissing missing) { public IMissing Missing { get; } = missing; }
public sealed class KeyedMissing([FromKeyedServices("missing")] IMissing missing) { public IMissing Missing { get; } = missing; }
public sealed class First;
public sealed class Second;
public sealed class Scoped;
public sealed class CapturesScoped(Scoped dependency) { public Scoped Dependency { get; } = dependency; }
public sealed class IndirectCapture(CapturesScoped dependency) { public CapturesScoped Dependency { get; } = dependency; }
public sealed class KeyedCapture([FromKeyedServices("dependency")] Scoped dependency) { public Scoped Dependency { get; } = dependency; }
public sealed class KeyedIndirectCapture([FromKeyedServices("dependency")] KeyedCapture dependency) { public KeyedCapture Dependency { get; } = dependency; }
public sealed class Ambiguous
{
public Ambiguous(First first) { }
public Ambiguous(Second second) { }
}
public sealed class Counts { public int Constructors; public int Factories; }
public sealed class Constructed { public Constructed(Counts counts) { counts.Constructors++; } }
public sealed class CallerOwned : IDisposable { public int Disposals; public void Dispose() => Disposals++; }
public sealed class MetadataConsumer(ServiceTypes types) { public ServiceTypes Types { get; } = types; }
public sealed class OpenGeneric<T>(IMissing missing) { public IMissing Missing { get; } = missing; }
public sealed class SelfCycle { public SelfCycle([FromKeyedServices] SelfCycle dependency) { } }
public sealed class CycleA { public CycleA([FromKeyedServices] CycleB dependency) { } }
public sealed class CycleB { public CycleB([FromKeyedServices] CycleA dependency) { } }
}