-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
GHIssue80_ScopedOrSingleton_extra_constructor_calls.cs
122 lines (99 loc) · 4.24 KB
/
GHIssue80_ScopedOrSingleton_extra_constructor_calls.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
namespace DryIoc.IssuesTests
{
[TestFixture]
public class GHIssue80_ScopedOrSingleton_extra_constructor_calls
{
[SetUp]
public void SetUp()
{
TrackingDisposable.ConstructorCallsCount = 0;
TrackingDisposable.DestructorCallsCount = 0;
}
[Test]
public void Issue80_ScopedOrSingleton_SingletonCheck()
{
var container = new Container();
var id = 1000;
container.RegisterDelegate(_ => ++id);
container.Register<TrackingDisposable>(Reuse.ScopedOrSingleton);
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, TrackingDisposable.ConstructorCallsCount, "expected 1 CONSTRUCTOR CALL, BECAUSE SINGLETON");// value = 3
}
[Test]
public void Issue80_ScopedOrSingleton()
{
var container = new Container();
var id = 1000;
container.RegisterDelegate(c => ++id);
container.Register<TrackingDisposable>(Reuse.ScopedOrSingleton);
var scope = container.OpenScope();
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, container.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, TrackingDisposable.ConstructorCallsCount);
Assert.AreEqual(2, scope.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, scope.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, scope.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, TrackingDisposable.ConstructorCallsCount);
Assert.AreEqual(0, TrackingDisposable.DestructorCallsCount);
scope.Dispose();
Assert.AreEqual(1, TrackingDisposable.DestructorCallsCount);
container.Dispose();
Assert.AreEqual(2, TrackingDisposable.DestructorCallsCount);
}
[Test]
public void Issue80_Scoped()
{
var container = new Container();
var id = 1000;
container.RegisterDelegate(c => ++id);
container.Register<TrackingDisposable>(Reuse.Scoped);
var scope1 = container.OpenScope();
Assert.AreEqual(1, scope1.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, scope1.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, scope1.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(1, TrackingDisposable.ConstructorCallsCount);
var scope2 = container.OpenScope();
Assert.AreEqual(2, scope2.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, scope2.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, scope2.Resolve<TrackingDisposable>().Value);
Assert.AreEqual(2, TrackingDisposable.ConstructorCallsCount);
Assert.AreEqual(0, TrackingDisposable.DestructorCallsCount);
scope1.Dispose();
Assert.AreEqual(1, TrackingDisposable.DestructorCallsCount);
scope2.Dispose();
Assert.AreEqual(2, TrackingDisposable.DestructorCallsCount);
}
}
public class TrackingDisposable : IDisposable
{
public TrackingDisposable(int i)
{
DebugLogger.Log($"~ctor {i}");
}
public static Int32 ConstructorCallsCount;
public static Int32 DestructorCallsCount;
public Int32 Value { get; set; } = ++ConstructorCallsCount;
public void Dispose()
{
DebugLogger.Log($"~dispose {Value}");
++DestructorCallsCount;
}
}
public class DebugLogger
{
public static List<String> Lines { get; set; } = new List<String>();
public static void Log(string s)
{
Lines.Add(s);
Debug.WriteLine(s);
}
public static string String() => string.Join("\r\n", Lines);
}
}