FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/TestFinalizer.cs at master · sdpython/pythonnet · GitHub
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
sdpython
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestFinalizer.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
231 lines (212 loc) · 7.95 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestFinalizer.cs
Copy path
File metadata and controls
231 lines (212 loc) · 7.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Linq
;
using
System
.
Runtime
.
CompilerServices
;
using
System
.
Threading
;
namespace
Python
.
EmbeddingTest
{
public
class
TestFinalizer
{
private
int
_oldThreshold
;
[
SetUp
]
public
void
SetUp
(
)
{
_oldThreshold
=
Finalizer
.
Instance
.
Threshold
;
PythonEngine
.
Initialize
(
)
;
Exceptions
.
Clear
(
)
;
}
[
TearDown
]
public
void
TearDown
(
)
{
Finalizer
.
Instance
.
Threshold
=
_oldThreshold
;
PythonEngine
.
Shutdown
(
)
;
}
private
static
void
FullGCCollect
(
)
{
GC
.
Collect
(
)
;
GC
.
WaitForPendingFinalizers
(
)
;
}
[
Test
]
[
Obsolete
(
"GC tests are not guaranteed"
)
]
public
void
CollectBasicObject
(
)
{
Assert
.
IsTrue
(
Finalizer
.
Instance
.
Enable
)
;
Finalizer
.
Instance
.
Threshold
=
1
;
bool
called
=
false
;
var
objectCount
=
0
;
EventHandler
<
Finalizer
.
CollectArgs
>
handler
=
(
s
,
e
)
=>
{
objectCount
=
e
.
ObjectCount
;
called
=
true
;
}
;
Assert
.
IsFalse
(
called
,
"The event handler was called before it was installed"
)
;
Finalizer
.
Instance
.
CollectOnce
+=
handler
;
IntPtr
pyObj
=
MakeAGarbage
(
out
var
shortWeak
,
out
var
longWeak
)
;
FullGCCollect
(
)
;
// The object has been resurrected
Warn
.
If
(
shortWeak
.
IsAlive
,
"The referenced object is alive although it should have been collected"
,
shortWeak
)
;
Assert
.
IsTrue
(
longWeak
.
IsAlive
,
"The reference object is not alive although it should still be"
,
longWeak
)
;
{
var
garbage
=
Finalizer
.
Instance
.
GetCollectedObjects
(
)
;
Assert
.
NotZero
(
garbage
.
Count
,
"There should still be garbage around"
)
;
Warn
.
Unless
(
garbage
.
Contains
(
pyObj
)
,
$
"The
{
nameof
(
longWeak
)
}
reference doesn't show up in the garbage list"
,
garbage
)
;
}
try
{
Finalizer
.
Instance
.
Collect
(
)
;
}
finally
{
Finalizer
.
Instance
.
CollectOnce
-=
handler
;
}
Assert
.
IsTrue
(
called
,
"The event handler was not called during finalization"
)
;
Assert
.
GreaterOrEqual
(
objectCount
,
1
)
;
}
[
Test
]
[
Obsolete
(
"GC tests are not guaranteed"
)
]
public
void
CollectOnShutdown
(
)
{
IntPtr
op
=
MakeAGarbage
(
out
var
shortWeak
,
out
var
longWeak
)
;
FullGCCollect
(
)
;
Assert
.
IsFalse
(
shortWeak
.
IsAlive
)
;
List
<
IntPtr
>
garbage
=
Finalizer
.
Instance
.
GetCollectedObjects
(
)
;
Assert
.
IsNotEmpty
(
garbage
,
"The garbage object should be collected"
)
;
Assert
.
IsTrue
(
garbage
.
Contains
(
op
)
,
"Garbage should contains the collected object"
)
;
PythonEngine
.
Shutdown
(
)
;
garbage
=
Finalizer
.
Instance
.
GetCollectedObjects
(
)
;
Assert
.
IsEmpty
(
garbage
)
;
}
[
MethodImpl
(
MethodImplOptions
.
NoInlining
|
MethodImplOptions
.
NoOptimization
)
]
// ensure lack of references to obj
[
Obsolete
(
"GC tests are not guaranteed"
)
]
private
static
IntPtr
MakeAGarbage
(
out
WeakReference
shortWeak
,
out
WeakReference
longWeak
)
{
IntPtr
handle
=
IntPtr
.
Zero
;
WeakReference
@short
=
null
,
@long
=
null
;
// must create Python object in the thread where we have GIL
IntPtr
val
=
PyLong
.
FromLong
(
1024
)
;
// must create temp object in a different thread to ensure it is not present
// when conservatively scanning stack for GC roots.
// see https://xamarin.github.io/bugzilla-archives/17/17593/bug.html
var
garbageGen
=
new
Thread
(
(
)
=>
{
var
obj
=
new
PyObject
(
val
,
skipCollect
:
true
)
;
@short
=
new
WeakReference
(
obj
)
;
@long
=
new
WeakReference
(
obj
,
true
)
;
handle
=
obj
.
Handle
;
}
)
;
garbageGen
.
Start
(
)
;
Assert
.
IsTrue
(
garbageGen
.
Join
(
TimeSpan
.
FromSeconds
(
5
)
)
,
"Garbage creation timed out"
)
;
shortWeak
=
@short
;
longWeak
=
@long
;
return
handle
;
}
private
static
long
CompareWithFinalizerOn
(
PyObject
pyCollect
,
bool
enbale
)
{
// Must larger than 512 bytes make sure Python use
string
str
=
new
string
(
'1'
,
1024
)
;
Finalizer
.
Instance
.
Enable
=
true
;
FullGCCollect
(
)
;
FullGCCollect
(
)
;
pyCollect
.
Invoke
(
)
;
Finalizer
.
Instance
.
Collect
(
)
;
Finalizer
.
Instance
.
Enable
=
enbale
;
// Estimate unmanaged memory size
long
before
=
Environment
.
WorkingSet
-
GC
.
GetTotalMemory
(
true
)
;
for
(
int
i
=
0
;
i
<
10000
;
i
++
)
{
// Memory will leak when disable Finalizer
new
PyString
(
str
)
;
}
FullGCCollect
(
)
;
FullGCCollect
(
)
;
pyCollect
.
Invoke
(
)
;
if
(
enbale
)
{
Finalizer
.
Instance
.
Collect
(
)
;
}
FullGCCollect
(
)
;
FullGCCollect
(
)
;
long
after
=
Environment
.
WorkingSet
-
GC
.
GetTotalMemory
(
true
)
;
return
after
-
before
;
}
/// <summary>
/// Because of two vms both have their memory manager,
/// this test only prove the finalizer has take effect.
/// </summary>
[
Test
]
[
Ignore
(
"Too many uncertainties, only manual on when debugging"
)
]
public
void
SimpleTestMemory
(
)
{
bool
oldState
=
Finalizer
.
Instance
.
Enable
;
try
{
using
(
PyObject
gcModule
=
PythonEngine
.
ImportModule
(
"gc"
)
)
using
(
PyObject
pyCollect
=
gcModule
.
GetAttr
(
"collect"
)
)
{
long
span1
=
CompareWithFinalizerOn
(
pyCollect
,
false
)
;
long
span2
=
CompareWithFinalizerOn
(
pyCollect
,
true
)
;
Assert
.
Less
(
span2
,
span1
)
;
}
}
finally
{
Finalizer
.
Instance
.
Enable
=
oldState
;
}
}
[
Test
]
public
void
ValidateRefCount
(
)
{
if
(
!
Finalizer
.
Instance
.
RefCountValidationEnabled
)
{
Assert
.
Ignore
(
"Only run with FINALIZER_CHECK"
)
;
}
IntPtr
ptr
=
IntPtr
.
Zero
;
bool
called
=
false
;
Finalizer
.
IncorrectRefCntHandler
handler
=
(
s
,
e
)
=>
{
called
=
true
;
Assert
.
AreEqual
(
ptr
,
e
.
Handle
)
;
Assert
.
AreEqual
(
2
,
e
.
ImpactedObjects
.
Count
)
;
// Fix for this test, don't do this on general environment
Runtime
.
Runtime
.
XIncref
(
e
.
Handle
)
;
return
false
;
}
;
Finalizer
.
Instance
.
IncorrectRefCntResolver
+=
handler
;
try
{
ptr
=
CreateStringGarbage
(
)
;
FullGCCollect
(
)
;
Assert
.
Throws
<
Finalizer
.
IncorrectRefCountException
>
(
(
)
=>
Finalizer
.
Instance
.
Collect
(
)
)
;
Assert
.
IsTrue
(
called
)
;
}
finally
{
Finalizer
.
Instance
.
IncorrectRefCntResolver
-=
handler
;
}
}
[
MethodImpl
(
MethodImplOptions
.
NoInlining
|
MethodImplOptions
.
NoOptimization
)
]
// ensure lack of references to s1 and s2
private
static
IntPtr
CreateStringGarbage
(
)
{
PyString
s1
=
new
PyString
(
"test_string"
)
;
// s2 steal a reference from s1
PyString
s2
=
new
PyString
(
s1
.
Handle
)
;
return
s1
.
Handle
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL