1
+ using System ;
2
+ using FluentAssertions ;
3
+ using NUnit . Framework ;
4
+ using Reflex . Core ;
5
+
6
+ namespace Reflex . EditModeTests
7
+ {
8
+ public class TransientTests
9
+ {
10
+ private class Service : IDisposable
11
+ {
12
+ public bool IsDisposed { get ; private set ; }
13
+
14
+ public void Dispose ( )
15
+ {
16
+ IsDisposed = true ;
17
+ }
18
+ }
19
+
20
+ [ Test ]
21
+ public void TransientFromType_ConstructedInstances_ShouldBeDisposed_WithinConstructingContainer ( )
22
+ {
23
+ var parentContainer = new ContainerBuilder ( ) . AddTransient ( typeof ( Service ) ) . Build ( ) ;
24
+ var childContainer = parentContainer . Scope ( ) ;
25
+
26
+ var instanceConstructedByChild = childContainer . Resolve < Service > ( ) ;
27
+ var instanceConstructedByParent = parentContainer . Resolve < Service > ( ) ;
28
+
29
+ childContainer . Dispose ( ) ;
30
+
31
+ instanceConstructedByChild . IsDisposed . Should ( ) . BeTrue ( ) ;
32
+ instanceConstructedByParent . IsDisposed . Should ( ) . BeFalse ( ) ;
33
+ }
34
+
35
+ [ Test ]
36
+ public void TransientFromFactory_ConstructedInstances_ShouldBeDisposed_WithinConstructingContainer ( )
37
+ {
38
+ var parentContainer = new ContainerBuilder ( ) . AddTransient ( _ => new Service ( ) ) . Build ( ) ;
39
+ var childContainer = parentContainer . Scope ( ) ;
40
+
41
+ var instanceConstructedByChild = childContainer . Resolve < Service > ( ) ;
42
+ var instanceConstructedByParent = parentContainer . Resolve < Service > ( ) ;
43
+
44
+ childContainer . Dispose ( ) ;
45
+
46
+ instanceConstructedByChild . IsDisposed . Should ( ) . BeTrue ( ) ;
47
+ instanceConstructedByParent . IsDisposed . Should ( ) . BeFalse ( ) ;
48
+ }
49
+
50
+ [ Test , Retry ( 3 ) ]
51
+ public void TransientFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
52
+ {
53
+ WeakReference instanceConstructedByChild ;
54
+ WeakReference instanceConstructedByParent ;
55
+ var parentContainer = new ContainerBuilder ( ) . AddTransient ( typeof ( Service ) ) . Build ( ) ;
56
+
57
+ void Act ( )
58
+ {
59
+ using ( var childContainer = parentContainer . Scope ( ) )
60
+ {
61
+ instanceConstructedByChild = new WeakReference ( childContainer . Resolve < Service > ( ) ) ;
62
+ instanceConstructedByParent = new WeakReference ( parentContainer . Resolve < Service > ( ) ) ;
63
+ }
64
+ }
65
+
66
+ Act ( ) ;
67
+ GarbageCollectionTests . ForceGarbageCollection ( ) ;
68
+ instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
69
+ instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
70
+ }
71
+
72
+ [ Test , Retry ( 3 ) ]
73
+ public void TransientFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed ( )
74
+ {
75
+ WeakReference instanceConstructedByChild ;
76
+ WeakReference instanceConstructedByParent ;
77
+ var parentContainer = new ContainerBuilder ( ) . AddTransient ( c => new Service ( ) ) . Build ( ) ;
78
+
79
+ void Act ( )
80
+ {
81
+ using ( var childContainer = parentContainer . Scope ( ) )
82
+ {
83
+ instanceConstructedByChild = new WeakReference ( childContainer . Resolve < Service > ( ) ) ;
84
+ instanceConstructedByParent = new WeakReference ( parentContainer . Resolve < Service > ( ) ) ;
85
+ }
86
+ }
87
+
88
+ Act ( ) ;
89
+ GarbageCollectionTests . ForceGarbageCollection ( ) ;
90
+ instanceConstructedByChild . IsAlive . Should ( ) . BeFalse ( ) ;
91
+ instanceConstructedByParent . IsAlive . Should ( ) . BeTrue ( ) ;
92
+ }
93
+ }
94
+ }
0 commit comments