-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathServiceManagerTests.cs
More file actions
525 lines (415 loc) · 16.4 KB
/
ServiceManagerTests.cs
File metadata and controls
525 lines (415 loc) · 16.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using MathCore.IoC;
using MathCore.IoC.ServiceRegistrations;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedType.Local
// ReSharper disable UnusedMember.Local
// ReSharper disable MemberCanBePrivate.Local
namespace MathCore.Tests.IoC;
/// <summary>Тест контейнера инверсии управления</summary>
[TestClass]
public class ServiceManagerTests
{
#region Тестовые сервисы
private interface IService
{
int GetValue();
}
private interface IService2
{
int Value { get; }
}
private class Service_42 : IService
{
public int GetValue() => 42;
}
private class Service_GetHashCode : IService
{
public int GetValue() => GetHashCode();
}
private class Service_GetThreadId : IService
{
private readonly int _ThreadId = Thread.CurrentThread.ManagedThreadId;
public int GetValue() => _ThreadId;
}
private class Service_GetTaskId : IService
{
private readonly int _TaskId = Task.CurrentId ?? -1;
public int GetValue() => _TaskId;
}
private class Service_Value(int value) : IService
{
public int GetValue() => value;
}
private class Service_TwoInterfaces(int value) : IService, IService2
{
public Service_TwoInterfaces() : this(15) { }
int IService.GetValue() => value;
int IService2.Value => value;
}
private class Service_ThrowException<TException> : IService
where TException : Exception, new()
{
public int GetValue() => throw new TException();
}
#endregion
[TestMethod]
public void DefaultManager()
{
var expected = ServiceManager.Default;
var actual = ServiceManager.Default;
Assert.IsNotNull(expected);
Assert.IsNotNull(actual);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void TypeIndexator()
{
var service_manager = new ServiceManager();
service_manager.Register<IService, Service_42>();
var expected1 = service_manager.Get<IService>();
var expected2 = service_manager.Get<IService>();
var actual = service_manager[typeof(IService)];
Assert.IsNotNull(expected1);
Assert.IsNotNull(expected2);
Assert.AreEqual(expected1, actual);
Assert.AreEqual(expected2, actual);
Assert.IsInstanceOfType<IService>(actual);
Assert.IsInstanceOfType<Service_42>(actual);
}
[TestMethod]
public void ServiceRegistration_Default_Simple()
{
var service_manager = new ServiceManager();
service_manager.Register<Service_42>();
var service = service_manager.Get<Service_42>();
Assert.IsNotNull(service);
}
/// <summary>Тестирование регистрации сервиса</summary>
[TestMethod]
public void ServiceRegistration_Default_Singleton()
{
var service_manager = new ServiceManager();
service_manager.Register<IService, Service_42>();
var service = service_manager.Get<IService>();
Assert.IsNotNull(service);
Assert.IsInstanceOfType<IService>(service);
Assert.IsInstanceOfType<Service_42>(service);
var actual = service.GetValue();
const int expected = 42;
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ServiceRegistration_Singleton_Factory()
{
var service_manager = new ServiceManager();
service_manager.Register<IService>(() => new Service_Value(13));
var instance1 = service_manager.Get<IService>();
var instance2 = service_manager.Get<IService>();
Assert.IsTrue(ReferenceEquals(instance1, instance2));
Assert.AreEqual(instance1, instance2);
}
[TestMethod]
public void ServiceRegistration_SingleCall_Factory()
{
var service_manager = new ServiceManager();
var counter = 0;
service_manager.Register<IService>(() => new Service_Value(++counter), ServiceRegistrationMode.SingleCall);
var service = service_manager.Get<IService>();
Assert.IsNotNull(service);
Assert.IsInstanceOfType<IService>(service);
Assert.IsInstanceOfType<Service_Value>(service);
var actual = service.GetValue();
var expected = counter;
Assert.AreEqual(expected, actual);
service = service_manager.Get<IService>();
Assert.IsNotNull(service);
Assert.IsInstanceOfType<IService>(service);
Assert.IsInstanceOfType<Service_Value>(service);
actual = service.GetValue();
expected = counter;
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ServiceRegistration_SingleCall()
{
var service_manager = new ServiceManager();
service_manager.RegisterSingleCall<IService, Service_GetHashCode>();
var instance1 = service_manager.Get<IService>();
var instance2 = service_manager.Get<IService>();
Assert.IsFalse(ReferenceEquals(instance1, instance2));
Assert.AreNotEqual(instance1, instance2);
}
[TestMethod]
public void ServiceRegistration_SingleCall_Simple()
{
var service_manager = new ServiceManager();
service_manager.RegisterSingleCall<Service_42>();
var instance1 = service_manager.Get<Service_42>();
var instance2 = service_manager.Get<Service_42>();
Assert.AreNotEqual(instance1, instance2);
Assert.IsFalse(ReferenceEquals(instance1, instance2));
}
[TestMethod]
public void ServiceRegistration_SingletonByThread()
{
var service_manager = new ServiceManager();
service_manager.Register<IService, Service_GetHashCode>(ServiceRegistrationMode.SingleThread);
var manager = service_manager.ServiceAccessor<IService>();
var main_instance1 = manager.Service;
IService? thread1_instance1 = null;
IService? thread1_instance2 = null;
IService? thread2_instance1 = null;
IService? thread2_instance2 = null;
var starter = new ManualResetEvent(false);
var waiter1 = new ManualResetEvent(false);
var waiter2 = new ManualResetEvent(false);
var thread1 = new Thread(() =>
{
thread1_instance1 = manager.Service;
waiter1.Set();
starter.WaitOne();
thread1_instance2 = manager.Service;
});
var thread2 = new Thread(() =>
{
thread2_instance1 = manager.Service;
waiter2.Set();
starter.WaitOne();
thread2_instance2 = manager.Service;
});
thread1.Start();
thread2.Start();
waiter2.WaitOne();
waiter1.WaitOne();
starter.Set();
thread1.Join();
thread2.Join();
var main_instance2 = manager.Service;
Assert.That.Value(main_instance1).IsReferenceEquals(main_instance2);
Assert.That.Value(thread1_instance1).IsReferenceEquals(thread1_instance2);
Assert.That.Value(thread2_instance1).IsReferenceEquals(thread2_instance2);
Assert.That.Value(main_instance1)
.IsNotReferenceEquals(thread1_instance1)
.IsNotReferenceEquals(thread2_instance1);
Assert.That.Value(thread1_instance1)
.IsNotReferenceEquals(thread2_instance1);
}
[TestMethod]
public void ServiceRegistration_SingletonByThread_Simple()
{
var service_manager = new ServiceManager();
service_manager.Register<Service_GetHashCode>(ServiceRegistrationMode.SingleThread);
var instance = service_manager.Get<Service_GetHashCode>();
Service_GetHashCode? instance2 = null;
var get_service_thread = new Thread(() => instance2 = service_manager.Get<Service_GetHashCode>());
get_service_thread.Start();
get_service_thread.Join();
Assert.IsNotNull(instance2);
var is_same = ReferenceEquals(instance, instance2);
Assert.IsFalse(is_same);
Assert.AreNotEqual(instance, instance2);
}
[TestMethod]
public void ServiceRegistration_SingletonByThread_Factory()
{
var service_manager = new ServiceManager();
service_manager.RegisterSingleThread(() => new Service_GetThreadId());
var manager = service_manager.ServiceAccessor<Service_GetThreadId>();
var main_instance1 = manager.Service;
Service_GetThreadId? thread1_instance1 = null;
Service_GetThreadId? thread1_instance2 = null;
Service_GetThreadId? thread2_instance1 = null;
Service_GetThreadId? thread2_instance2 = null;
var starter = new ManualResetEvent(false);
var waiter1 = new ManualResetEvent(false);
var waiter2 = new ManualResetEvent(false);
var thread1 = new Thread(() =>
{
thread1_instance1 = manager.Service;
waiter1.Set();
starter.WaitOne();
thread1_instance2 = manager.Service;
});
var thread2 = new Thread(() =>
{
thread2_instance1 = manager.Service;
waiter2.Set();
starter.WaitOne();
thread2_instance2 = manager.Service;
});
thread1.Start();
thread2.Start();
waiter2.WaitOne();
waiter1.WaitOne();
starter.Set();
thread1.Join();
thread2.Join();
var main_instance2 = manager.Service;
Assert.That.Value(main_instance1).IsReferenceEquals(main_instance2);
Assert.That.Value(thread1_instance1).IsReferenceEquals(thread1_instance2);
Assert.That.Value(thread2_instance1).IsReferenceEquals(thread2_instance2);
Assert.That.Value(main_instance1)
.IsNotReferenceEquals(thread1_instance1)
.IsNotReferenceEquals(thread2_instance1);
Assert.That.Value(thread1_instance1)
.IsNotReferenceEquals(thread2_instance1);
}
[TestMethod]
public void ServiceRegistration_Singleton()
{
var service_manager = new ServiceManager();
service_manager.Register<IService, Service_GetHashCode>();
var expected = service_manager.Get<IService>();
var actual = service_manager.Get<IService>();
Assert.IsInstanceOfType<Service_GetHashCode>(expected);
Assert.IsInstanceOfType<Service_GetHashCode>(actual);
Assert.IsTrue(ReferenceEquals(expected, actual));
}
[TestMethod]
public void ServiceRegistration_Singleton_Instance()
{
var service_manager = new ServiceManager();
var expected = new Service_GetHashCode();
service_manager.RegisterSingleton<IService>(expected);
var actual = service_manager.Get<IService>();
Assert.IsInstanceOfType<IService>(actual);
Assert.IsInstanceOfType<Service_GetHashCode>(actual);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ServiceRegistered()
{
var service_manager = new ServiceManager();
Assert.IsFalse(service_manager.ServiceRegistered<IService>());
service_manager.Register<IService, Service_42>();
Assert.IsTrue(service_manager.ServiceRegistered<IService>());
}
[TestMethod]
public void ServiceRegistered_ByType()
{
var service_manager = new ServiceManager();
Assert.IsFalse(service_manager.ServiceRegistered<IService>());
service_manager.Register<IService, Service_42>();
Assert.IsTrue(service_manager.ServiceRegistered<IService>());
}
[TestMethod]
public void ServiceRegistration_Singleton_TwoInterfaces()
{
var service_manager = new ServiceManager();
service_manager.RegisterSingleton<IService, Service_TwoInterfaces>();
service_manager.RegisterSingleton<IService2, Service_TwoInterfaces>();
var instance_i_service = service_manager.Get<IService>();
var instance_i_service2 = service_manager.Get<IService2>();
Assert.IsInstanceOfType<IService>(instance_i_service);
Assert.IsInstanceOfType<Service_TwoInterfaces>(instance_i_service);
Assert.IsInstanceOfType<IService2>(instance_i_service2);
Assert.IsInstanceOfType<Service_TwoInterfaces>(instance_i_service);
Assert.IsTrue(ReferenceEquals(instance_i_service, instance_i_service2));
}
[TestMethod]
public void ServiceRegistration_Singleton_Timeout()
{
var service_manager = new ServiceManager();
var timeout = TimeSpan.FromMilliseconds(100);
var registration = service_manager
.RegisterSingleton<IService, Service_42>()
.With(r => r.InstanceActualityTime = timeout);
Assert.AreEqual(timeout, registration.InstanceActualityTime);
var service1 = service_manager.Get<IService>();
var service2 = service_manager.Get<IService>();
Assert.IsNotNull(service1);
Assert.IsNotNull(service2);
Assert.IsInstanceOfType<Service_42>(service1);
Assert.IsInstanceOfType<Service_42>(service2);
Assert.IsTrue(ReferenceEquals(service1, service2));
Thread.Sleep(1000);
var service3 = service_manager.Get<IService>();
Assert.IsNotNull(service3);
Assert.IsInstanceOfType<Service_42>(service3);
//Assert.IsFalse(ReferenceEquals(service1, service3));
}
[TestMethod]
public void ExceptionGeneration_SingleCall()
{
var service_manager = new ServiceManager();
var call_number = 0;
var registration = service_manager
.Register<IService>(
() => throw new ApplicationException((++call_number).ToString()),
ServiceRegistrationMode.SingleCall);
var exception_throwed = false;
Assert.IsNull(registration.LastException);
try
{
service_manager.Get<IService>();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (ApplicationException)
{
exception_throwed = true;
}
#pragma warning restore CA1031 // Do not catch general exception types
Assert.IsTrue(exception_throwed);
Assert.IsNotNull(registration.LastException);
Assert.IsInstanceOfType<ApplicationException>(registration.LastException);
Assert.AreEqual("1", registration.LastException.Message);
exception_throwed = false;
try
{
service_manager.Get<IService>();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (ApplicationException)
{
exception_throwed = true;
}
#pragma warning restore CA1031 // Do not catch general exception types
Assert.IsTrue(exception_throwed);
Assert.IsNotNull(registration.LastException);
Assert.IsInstanceOfType<ApplicationException>(registration.LastException);
Assert.AreEqual("2", registration.LastException.Message);
}
[TestMethod]
public void ExceptionGeneration_Singleton()
{
var service_manager = new ServiceManager();
var call_number = 0;
var registration = service_manager
.Register<IService>(
() => throw new ApplicationException((++call_number).ToString()),
ServiceRegistrationMode.Singleton);
var exception_throwed = false;
Assert.IsNull(registration.LastException);
try
{
service_manager.Get<IService>();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (ApplicationException)
{
exception_throwed = true;
}
#pragma warning restore CA1031 // Do not catch general exception types
Assert.IsTrue(exception_throwed);
var last_exception = registration.LastException;
Assert.IsNotNull(last_exception);
Assert.IsInstanceOfType<ApplicationException>(last_exception);
Assert.AreEqual("1", last_exception.Message);
exception_throwed = false;
try
{
service_manager.Get<IService>();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (ApplicationException)
{
exception_throwed = true;
}
#pragma warning restore CA1031 // Do not catch general exception types
Assert.IsTrue(exception_throwed);
Assert.IsNotNull(registration.LastException);
Assert.IsInstanceOfType<ApplicationException>(registration.LastException);
Assert.AreEqual("1", registration.LastException.Message);
Assert.IsTrue(ReferenceEquals(last_exception, registration.LastException));
}
}