-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathUnitTestGCTest.cs
204 lines (162 loc) · 6.48 KB
/
UnitTestGCTest.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using nanoFramework.TestFramework;
namespace NFUnitTestSystemLib
{
[TestClass]
public class UnitTestGCTest
{
#pragma warning disable S1215 // this is intended to test the GC
#pragma warning disable S1854 // this is intended to test the GC
#pragma warning disable S2696 // this is intended to test the GC
#pragma warning disable S3971 // this is intended to test the GC
internal class FinalizeObject
{
public static FinalizeObject m_currentInstance = null;
~FinalizeObject()
{
if (!m_hasFinalized1)
{
// First finalization
// Put this object back into a root by creating
// a reference to it.
FinalizeObject.m_currentInstance = this;
// Indicate that this instance has finalized once.
m_hasFinalized1 = true;
// Place a reference to this object back in the
// finalization queue.
GC.ReRegisterForFinalize(this);
}
else
{
// Second finalization
m_hasFinalized2 = true;
}
}
}
static bool m_hasFinalized1 = false;
static bool m_hasFinalized2 = false;
static bool m_Test1Result = false;
[TestMethod]
public void SystemGC1_Test()
{
/// <summary>
/// 1. Create a FinalizeObject.
/// 2. Release the reference
/// 3. Allow for GC
/// 4. Run ReRegisterForFinalize
/// 5. Allow for GC
/// 6. Verify that object has been collected
/// </summary>
///
OutputHelper.WriteLine("Tests ReRegisterForFinalize");
OutputHelper.WriteLine("Create a FinalizeObject.");
FinalizeObject mfo = new FinalizeObject();
m_hasFinalized1 = false;
m_hasFinalized2 = false;
// Release reference
OutputHelper.WriteLine("Release reference");
mfo = null;
OutputHelper.WriteLine("Allow GC");
GC.Collect();
int sleepTime = 1000;
int slept = 0;
while (!m_hasFinalized1 && slept < sleepTime)
{
// force GC run caused by memory allocation
var dummyArray = new byte[1024 * 1024 * 1];
System.Threading.Thread.Sleep(10);
slept += 10;
}
OutputHelper.WriteLine($"GC took {slept}");
// At this point mfo will have gone through the first Finalize.
// There should now be a reference to mfo in the static
// FinalizeObject.m_currentInstance field. Setting this value
// to null and forcing another garbage collection will now
// cause the object to Finalize permanently.
OutputHelper.WriteLine("Reregister and allow for GC");
FinalizeObject.m_currentInstance = null;
GC.Collect();
sleepTime = 1000;
slept = 0;
while (!m_hasFinalized2 && slept < sleepTime)
{
// force GC run caused by memory allocation
var dummyArray = new byte[1024 * 1024 * 1];
System.Threading.Thread.Sleep(10);
slept += 10;
}
OutputHelper.WriteLine($"GC took {slept}");
m_Test1Result = m_hasFinalized2;
Assert.IsTrue(m_hasFinalized2);
}
[TestMethod]
public void SystemGC2_Test()
{
/// <summary>
/// 1. Create a FinalizeObject.
/// 2. Release the reference
/// 3. SupressFinalize
/// 3. Allow for GC
/// 6. Verify that object has not been collected
/// </summary>
///
OutputHelper.WriteLine("Tests SuppressFinalize");
OutputHelper.WriteLine("Create a FinalizeObject");
FinalizeObject mfo = new FinalizeObject();
m_hasFinalized1 = false;
m_hasFinalized2 = false;
OutputHelper.WriteLine("Releasing");
GC.SuppressFinalize(mfo);
mfo = null;
OutputHelper.WriteLine("Allow GC");
GC.Collect();
int sleepTime = 1000;
int slept = 0;
while (!m_hasFinalized1 && slept < sleepTime)
{
// force GC run caused by memory allocation
_ = new byte[1024 * 1024 * 1];
System.Threading.Thread.Sleep(10);
slept += 10;
}
OutputHelper.WriteLine($"GC took {slept}");
Assert.IsFalse(m_hasFinalized1);
}
[TestMethod]
public void SystemGC3_Test()
{
/// <summary>
/// 1. Create a FinalizeObject.
/// 2. Release the reference
/// 3. SupressFinalize
/// 3. Allow for GC
/// 6. Verify that object has not been collected
/// </summary>
///
OutputHelper.Write("Tests WaitForPendingFinalizers, dependant on test 1");
OutputHelper.WriteLine("will fail if test 1 fails.");
Assert.IsTrue(m_Test1Result, "Can't run this test as SystemGC1_Test has failed.");
OutputHelper.WriteLine("Create a FinalizeObject");
FinalizeObject mfo = new FinalizeObject();
m_hasFinalized1 = false;
m_hasFinalized2 = false;
OutputHelper.WriteLine("Releasing");
mfo = null;
OutputHelper.WriteLine("Wait for GC");
GC.Collect();
GC.WaitForPendingFinalizers();
OutputHelper.WriteLine("Releasing again");
FinalizeObject.m_currentInstance = null;
OutputHelper.WriteLine("Wait for GC");
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsTrue(m_hasFinalized2);
}
}
#pragma warning restore S1215 // "GC.Collect" should not be called
#pragma warning restore S1854 // Unused assignments should be removed
#pragma warning restore S2696 // Instance members should not write to "static" fields
#pragma warning restore S3971 // "GC.SuppressFinalize" should not be called
}