-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonGenericServiceFactoryTests.cs
More file actions
55 lines (43 loc) · 1.73 KB
/
SingletonGenericServiceFactoryTests.cs
File metadata and controls
55 lines (43 loc) · 1.73 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
using System;
using Couchbase.AnalyticsClient.Internal.DI;
using Moq;
using Xunit;
namespace Couchbase.Analytics.UnitTests.Internal.DI;
public interface IFakeGenericService<T> : IDisposable { }
public class FakeGenericService<T> : IFakeGenericService<T>
{
public static Action? OnDispose { get; set; }
public FakeGenericService() { }
public void Dispose()
{
OnDispose?.Invoke();
}
}
public class SingletonGenericServiceFactoryTests
{
[Fact]
public void SingletonGenericServiceFactory_Dispose_DisposesAllCachedPermutations()
{
// Arrange
var factory = new SingletonGenericServiceFactory(typeof(FakeGenericService<>));
var mockServiceProvider = new Mock<IServiceProvider>();
factory.Initialize(mockServiceProvider.Object);
// Populate the concurrent dictionary with different permutations of the generic
factory.CreateService(typeof(IFakeGenericService<int>));
factory.CreateService(typeof(IFakeGenericService<string>));
factory.CreateService(typeof(IFakeGenericService<double>));
int disposeCount = 0;
FakeGenericService<int>.OnDispose = () => disposeCount++;
FakeGenericService<string>.OnDispose = () => disposeCount++;
FakeGenericService<double>.OnDispose = () => disposeCount++;
// Act
factory.Dispose();
// Assert
// The factory should naturally iterate and call Dispose exactly 3 times (once per instantiated T)
Assert.Equal(3, disposeCount);
// Cleanup statics
FakeGenericService<int>.OnDispose = null;
FakeGenericService<string>.OnDispose = null;
FakeGenericService<double>.OnDispose = null;
}
}