1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using FluentAssertions ;
4
+ using NUnit . Framework ;
5
+ using Reflex . Core ;
6
+ using UnityEditor ;
7
+
8
+ namespace Reflex . EditModeTests
9
+ {
10
+ public class ScopedTests
11
+ {
12
+ private class Service : IDisposable
13
+ {
14
+ public bool IsDisposed { get ; private set ; }
15
+
16
+ public void Dispose ( )
17
+ {
18
+ IsDisposed = true ;
19
+ }
20
+ }
21
+
22
+ [ Test ]
23
+ public void ScopedFromType_ShouldReturnAlwaysSameInstance_WhenCalledFromSameContainer ( )
24
+ {
25
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( typeof ( Service ) ) . Build ( ) ;
26
+ var childContainer = parentContainer . Scope ( ) ;
27
+ parentContainer . Resolve < Service > ( ) . Should ( ) . Be ( parentContainer . Resolve < Service > ( ) ) ;
28
+ childContainer . Resolve < Service > ( ) . Should ( ) . Be ( childContainer . Resolve < Service > ( ) ) ;
29
+ }
30
+
31
+ [ Test ]
32
+ public void ScopedFromFactory_ShouldReturnAlwaysSameInstance_WhenCalledFromSameContainer ( )
33
+ {
34
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( _ => new Service ( ) ) . Build ( ) ;
35
+ var childContainer = parentContainer . Scope ( ) ;
36
+ parentContainer . Resolve < Service > ( ) . Should ( ) . Be ( parentContainer . Resolve < Service > ( ) ) ;
37
+ childContainer . Resolve < Service > ( ) . Should ( ) . Be ( childContainer . Resolve < Service > ( ) ) ;
38
+ }
39
+
40
+ [ Test ]
41
+ public void ScopedFromType_NewInstanceShouldBeConstructed_ForEveryNewContainer ( )
42
+ {
43
+ var instances = new HashSet < Service > ( ) ;
44
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( typeof ( Service ) ) . Build ( ) ;
45
+ var childContainer = parentContainer . Scope ( ) ;
46
+ instances . Add ( parentContainer . Resolve < Service > ( ) ) ;
47
+ instances . Add ( childContainer . Resolve < Service > ( ) ) ;
48
+ instances . Add ( parentContainer . Resolve < Service > ( ) ) ;
49
+ instances . Add ( childContainer . Resolve < Service > ( ) ) ;
50
+ instances . Count . Should ( ) . Be ( 2 ) ;
51
+ }
52
+
53
+ [ Test ]
54
+ public void ScopedFromFactory_NewInstanceShouldBeConstructed_ForEveryNewContainer ( )
55
+ {
56
+ var instances = new HashSet < Service > ( ) ;
57
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( _ => new Service ( ) ) . Build ( ) ;
58
+ var childContainer = parentContainer . Scope ( ) ;
59
+ instances . Add ( parentContainer . Resolve < Service > ( ) ) ;
60
+ instances . Add ( childContainer . Resolve < Service > ( ) ) ;
61
+ instances . Add ( parentContainer . Resolve < Service > ( ) ) ;
62
+ instances . Add ( childContainer . Resolve < Service > ( ) ) ;
63
+ instances . Count . Should ( ) . Be ( 2 ) ;
64
+ }
65
+
66
+ [ Test ]
67
+ public void ScopedFromType_ConstructedInstances_ShouldBeDisposed_WithinConstructingContainer ( )
68
+ {
69
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( typeof ( Service ) ) . Build ( ) ;
70
+ var childContainer = parentContainer . Scope ( ) ;
71
+
72
+ var instanceConstructedByChild = childContainer . Resolve < Service > ( ) ;
73
+ var instanceConstructedByParent = parentContainer . Resolve < Service > ( ) ;
74
+
75
+ childContainer . Dispose ( ) ;
76
+
77
+ instanceConstructedByChild . IsDisposed . Should ( ) . BeTrue ( ) ;
78
+ instanceConstructedByParent . IsDisposed . Should ( ) . BeFalse ( ) ;
79
+ }
80
+
81
+ [ Test ]
82
+ public void ScopedFromFactory_ConstructedInstances_ShouldBeDisposed_WithinConstructingContainer ( )
83
+ {
84
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( _ => new Service ( ) ) . Build ( ) ;
85
+ var childContainer = parentContainer . Scope ( ) ;
86
+
87
+ var instanceConstructedByChild = childContainer . Resolve < Service > ( ) ;
88
+ var instanceConstructedByParent = parentContainer . Resolve < Service > ( ) ;
89
+
90
+ childContainer . Dispose ( ) ;
91
+
92
+ instanceConstructedByChild . IsDisposed . Should ( ) . BeTrue ( ) ;
93
+ instanceConstructedByParent . IsDisposed . Should ( ) . BeFalse ( ) ;
94
+ }
95
+
96
+ [ Test , Retry ( 3 ) ]
97
+ public void ScopedFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
98
+ {
99
+ GarbageCollectionTests . MarkAsInconclusiveWhenReflexDebugIsEnabled ( ) ;
100
+
101
+ WeakReference instanceConstructedByChild ;
102
+ WeakReference instanceConstructedByParent ;
103
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( typeof ( Service ) ) . Build ( ) ;
104
+
105
+ void Act ( )
106
+ {
107
+ using ( var childContainer = parentContainer . Scope ( ) )
108
+ {
109
+ instanceConstructedByChild = new WeakReference ( childContainer . Resolve < Service > ( ) ) ;
110
+ instanceConstructedByParent = new WeakReference ( parentContainer . Resolve < Service > ( ) ) ;
111
+ }
112
+ }
113
+
114
+ Act ( ) ;
115
+ GarbageCollectionTests . ForceGarbageCollection ( ) ;
116
+ instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
117
+ instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
118
+ }
119
+
120
+ [ Test , Retry ( 3 ) ]
121
+ public void ScopedFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
122
+ {
123
+ GarbageCollectionTests . MarkAsInconclusiveWhenReflexDebugIsEnabled ( ) ;
124
+
125
+ WeakReference instanceConstructedByChild ;
126
+ WeakReference instanceConstructedByParent ;
127
+ var parentContainer = new ContainerBuilder ( ) . AddScoped ( _ => new Service ( ) ) . Build ( ) ;
128
+
129
+ void Act ( )
130
+ {
131
+ using ( var childContainer = parentContainer . Scope ( ) )
132
+ {
133
+ instanceConstructedByChild = new WeakReference ( childContainer . Resolve < Service > ( ) ) ;
134
+ instanceConstructedByParent = new WeakReference ( parentContainer . Resolve < Service > ( ) ) ;
135
+ }
136
+ }
137
+
138
+ Act ( ) ;
139
+ GarbageCollectionTests . ForceGarbageCollection ( ) ;
140
+ instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
141
+ instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
142
+ }
143
+ }
144
+ }
0 commit comments