finalizer_test.py
4.33 KB
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
"""
Created on Mar 7, 2010
@author: barthelemy
"""
from __future__ import unicode_literals, absolute_import
import gc
import unittest
from weakref import ref
from py4j.finalizer import ThreadSafeFinalizer, Finalizer, clear_finalizers
def deleted(accumulator, id):
print(id)
accumulator.acc += 1
class Accumulator(object):
def __init__(self):
self.acc = 0
class AClass(object):
def __init__(self, id, acc):
self.id = id
self.acc = acc
ThreadSafeFinalizer.add_finalizer(
id,
ref(self, lambda wr, i=self.id, a=self.acc: deleted(a, i)))
class AClass2(object):
def __init__(self, id, acc):
self.id = id
self.acc = acc
Finalizer.add_finalizer(
id,
ref(self, lambda wr, i=self.id, a=self.acc: deleted(a, i)))
class JavaObjecTest(object):
def __init__(self, id, acc):
self.id = id
self.acc = acc
self.methods = []
ThreadSafeFinalizer.add_finalizer(
id,
ref(self, lambda wr, i=self.id, a=self.acc: deleted(a, i)))
class JavaMemberTest(object):
def __init__(self, name, container):
self.name = name
self.container = container
class TestThreadSafeFinalizer(unittest.TestCase):
def tearDown(self):
clear_finalizers(True)
def work1(self, acc):
a1 = AClass(1, acc) # noqa
a2 = AClass(2, acc) # noqa
def work1b(self, acc):
a1 = AClass(1, acc)
a2 = AClass(2, acc)
a1.foo = a2
a2.foo = a1
def work2(self, acc):
a1 = AClass(1, acc)
a2 = AClass(2, acc) # noqa
return a1
def testFinalizer(self):
acc = Accumulator()
self.work1(acc)
self.assertEqual(2, acc.acc)
self.work2(acc)
self.assertEqual(4, acc.acc)
def work_circ(self, acc):
jobj = JavaObjecTest(1, acc)
jmem1 = JavaMemberTest("append", jobj)
jobj.methods.append(jmem1)
def testCircularReference2(self):
acc = Accumulator()
self.work_circ(acc)
# Necessary because of circular references...
gc.collect()
self.assertEqual(1, acc.acc)
def testCleanUp(self):
acc = Accumulator()
a1 = self.work2(acc)
self.assertEqual(1, acc.acc)
self.assertEqual(2, len(ThreadSafeFinalizer.finalizers))
clear_finalizers(False)
self.assertEqual(1, acc.acc)
self.assertEqual(1, len(ThreadSafeFinalizer.finalizers))
a1.foo = "hello"
del(a1)
self.assertEqual(2, acc.acc)
clear_finalizers(False)
self.assertEqual(0, len(ThreadSafeFinalizer.finalizers))
def testCleanUpAll(self):
acc = Accumulator()
a1 = self.work2(acc)
self.assertEqual(1, acc.acc)
self.assertEqual(2, len(ThreadSafeFinalizer.finalizers))
clear_finalizers(True)
self.assertEqual(1, acc.acc)
self.assertEqual(0, len(ThreadSafeFinalizer.finalizers))
a1.foo = "hello"
del(a1)
self.assertEqual(1, acc.acc)
class TestFinalizer(unittest.TestCase):
def tearDown(self):
clear_finalizers(True)
def work1(self, acc):
a1 = AClass2(1, acc) # noqa
a2 = AClass2(2, acc) # noqa
def work2(self, acc):
a1 = AClass2(1, acc) # noqa
a2 = AClass2(2, acc) # noqa
return a1
def testFinalizer(self):
acc = Accumulator()
self.work1(acc)
self.assertEqual(2, acc.acc)
def testCleanUp(self):
acc = Accumulator()
a1 = self.work2(acc)
self.assertEqual(1, acc.acc)
self.assertEqual(2, len(Finalizer.finalizers))
clear_finalizers(False)
self.assertEqual(1, acc.acc)
self.assertEqual(1, len(Finalizer.finalizers))
a1.foo = "hello"
del(a1)
self.assertEqual(2, acc.acc)
clear_finalizers(False)
self.assertEqual(0, len(Finalizer.finalizers))
def testCleanUpAll(self):
acc = Accumulator()
a1 = self.work2(acc)
self.assertEqual(1, acc.acc)
self.assertEqual(2, len(Finalizer.finalizers))
clear_finalizers(True)
self.assertEqual(1, acc.acc)
self.assertEqual(0, len(Finalizer.finalizers))
a1.foo = "hello"
del(a1)
self.assertEqual(1, acc.acc)
if __name__ == "__main__":
unittest.main()