-
-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathStartableTests.cs
More file actions
329 lines (281 loc) · 10.1 KB
/
Copy pathStartableTests.cs
File metadata and controls
329 lines (281 loc) · 10.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Autofac.Builder;
using Autofac.Core;
namespace Autofac.Specification.Test.Features;
public class StartableTests
{
private interface IMyService
{
}
private interface IStartableDependency
{
}
[Fact]
public void AutoActivate_CanBeAddedToChildLifetimeScope()
{
// Issue #567
var singletonCount = 0;
var container = new ContainerBuilder().Build();
using (var scope = container.BeginLifetimeScope(b => b.RegisterType<MyComponent>().As<IMyService>().SingleInstance().AutoActivate().OnActivated(e => singletonCount++)))
{
Assert.Equal(1, singletonCount);
}
}
[Fact]
public void AutoActivate_InvalidLifetimeConflictsWithAutoStart()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent2>().InstancePerMatchingLifetimeScope("foo").AutoActivate();
Assert.Throws<DependencyResolutionException>(() => builder.Build());
}
[Fact]
public void AutoActivate_MultipleAutoStartFlagsOnlyStartTheComponentOnce()
{
var instanceCount = 0;
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent2>().AutoActivate().AutoActivate().AutoActivate().OnActivated(e => instanceCount++);
builder.Build();
Assert.Equal(1, instanceCount);
}
[Fact]
public void AutoActivate_RegistrationsAddedToChildDoNotDoubleActivateParent()
{
// Issue #567
var parentCount = 0;
var childCount = 0;
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent2>().AutoActivate().OnActivated(e => parentCount++);
var container = builder.Build();
using (var scope = container.BeginLifetimeScope(b => b.RegisterType<MyComponent>().AutoActivate().OnActivated(e => childCount++)))
{
using (var scope2 = scope.BeginLifetimeScope(b => b.RegisterInstance("x")))
{
Assert.Equal(1, childCount);
Assert.Equal(1, parentCount);
}
}
}
[Fact]
public void AutoActivate_ResolvesComponentsAutomatically()
{
var singletonCount = 0;
var instanceCount = 0;
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent>().As<IMyService>().SingleInstance().AutoActivate().OnActivated(e => singletonCount++);
builder.RegisterType<MyComponent2>().AutoActivate().OnActivated(e => instanceCount++);
builder.Build();
Assert.Equal(1, singletonCount);
Assert.Equal(1, instanceCount);
}
[Fact]
public void Startable_WhenChildScopeBegins_NewStartableComponentsAreStarted()
{
var startable = new Startable();
var builder = new ContainerBuilder();
var container = builder.Build();
using var scope = container.BeginLifetimeScope(b => b.RegisterInstance(startable).As<IStartable>());
Assert.True(startable.StartCount > 0);
}
[Fact]
public void Startable_WhenNoStartIsSpecified_StartableComponentsAreIgnoredInChildLifetimeScope()
{
var startable = new Startable();
var builder = new ContainerBuilder();
var container = builder.Build(ContainerBuildOptions.IgnoreStartableComponents);
using var scope = container.BeginLifetimeScope(b => b.RegisterInstance(startable).As<IStartable>());
Assert.False(startable.StartCount > 0);
}
[Fact]
public void Startable_WhenNoStartIsSpecified_StartableComponentsAreIgnoredInContainer()
{
var startable = new Startable();
var builder = new ContainerBuilder();
builder.RegisterInstance(startable).As<IStartable>();
builder.Build(ContainerBuildOptions.IgnoreStartableComponents);
Assert.False(startable.StartCount > 0);
}
[Fact]
public void Startable_WhenStartableCreatesChildScope_NoExceptionIsThrown()
{
// Issue #916
var builder = new ContainerBuilder();
builder.RegisterType<StartableCreatesLifetimeScope>().As<IStartable>().SingleInstance();
// Assert.DoesNotThrow, basically.
var container = builder.Build();
Assert.NotNull(container);
}
[Fact]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposal is part of the test.")]
public void Startable_WhenStartableThrows_DependenciesAreDisposed()
{
// Issue #1392 - disposable instances resolved to satisfy a startable should be
// disposed when the startable's Start() method throws.
var dependency = new DisposableDependency();
var builder = new ContainerBuilder();
builder.RegisterInstance(dependency);
builder.RegisterType<ThrowingStartable>().As<IStartable>().SingleInstance();
Assert.Throws<DependencyResolutionException>(() => builder.Build());
Assert.True(dependency.IsDisposed);
}
[Fact]
public void Startable_WhenStartIsSpecified_StartableComponentsAreStarted()
{
var startable = new Startable();
var builder = new ContainerBuilder();
builder.RegisterInstance(startable).As<IStartable>();
builder.Build();
Assert.True(startable.StartCount > 0);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Startable_WhenTheContainerIsBuilt_StartableComponentsAreStartedInDependencyOrder(bool ignoreStartableComponents)
{
var builder = new ContainerBuilder();
builder.RegisterType<StartableTakesDependency>().AsSelf()
.SingleInstance().As<IStartable>();
builder.RegisterType<ComponentTakesStartableDependency>()
.WithParameter("expectStarted", !ignoreStartableComponents)
.AsSelf()
.As<IStartable>();
var container = builder.Build(ignoreStartableComponents ? ContainerBuildOptions.IgnoreStartableComponents : ContainerBuildOptions.None);
container.Resolve<ComponentTakesStartableDependency>();
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Startable_WhenTheContainerIsBuilt_StartableComponentsThatDependOnAutoActivateComponents_AreNotStartedTwice(bool isSingleton)
{
var builder = new ContainerBuilder();
var expectedStartCount = isSingleton ? 1 : 2;
var dependencyRegistration = builder.RegisterType<StartableDependency>().As<IStartableDependency>().AutoActivate();
if (isSingleton)
{
dependencyRegistration.SingleInstance();
}
builder.RegisterType<StartableTakesDependency>().AsSelf().As<IStartable>();
StartableDependency.Count = 0;
builder.Build();
Assert.Equal(expectedStartCount, StartableDependency.Count);
}
[Fact]
public void AutoActivate_DoesNotHideDefaultSelfService()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent2>().AutoActivate();
using var container = builder.Build();
Assert.True(container.IsRegistered<MyComponent2>());
}
[Fact]
public void AutoActivate_RegisterInstanceActivationWorksWhenDefaultServiceOverloaded()
{
var instanceCount = 0;
var builder = new ContainerBuilder();
builder.RegisterInstance(new MyComponent2()).As<object>().OnActivated(_ => instanceCount++);
builder.RegisterType<object>();
builder.Build();
Assert.Equal(1, instanceCount);
}
private class ComponentTakesStartableDependency : IStartable
{
public ComponentTakesStartableDependency(StartableTakesDependency dependency, bool expectStarted)
{
Assert.Equal(expectStarted, dependency.WasStarted);
}
public void Start()
{
}
}
private sealed class MyComponent : IMyService
{
}
private sealed class MyComponent2
{
}
private class Startable : IStartable
{
public int StartCount
{
get; private set;
}
public void Start()
{
StartCount++;
}
}
// Issue #916
private class StartableCreatesLifetimeScope : IStartable
{
private readonly ILifetimeScope _scope;
public StartableCreatesLifetimeScope(ILifetimeScope scope)
{
_scope = scope;
}
public void Start()
{
using (var nested = _scope.BeginLifetimeScope("tag", b => { }))
{
// Intentionally empty - testing scope creation during Start.
}
using (var nested = _scope.BeginLifetimeScope(b => { }))
{
// Intentionally empty - testing scope creation during Start.
}
using (var nested = _scope.BeginLifetimeScope("tag"))
{
// Intentionally empty - testing scope creation during Start.
}
using (var nested = _scope.BeginLifetimeScope())
{
// Intentionally empty - testing scope creation during Start.
}
}
}
private class StartableDependency : IStartableDependency
{
public StartableDependency()
{
Count++;
}
public static int Count
{
get; set;
}
}
private class StartableTakesDependency : IStartable
{
public StartableTakesDependency(IStartableDependency[] dependencies)
{
}
public bool WasStarted
{
get; private set;
}
public void Start()
{
WasStarted = true;
}
}
private class DisposableDependency : IDisposable
{
public bool IsDisposed
{
get; private set;
}
public void Dispose()
{
IsDisposed = true;
}
}
private class ThrowingStartable : IStartable
{
public ThrowingStartable(DisposableDependency dependency)
{
}
public void Start()
{
throw new InvalidOperationException("Startable failed.");
}
}
}