Debian Codegraph @

test/jpypetest/test_leak2.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************import _jpype
18 import jpype
19 from jpype.types import *
20 import sys
21 import logging
22 import time
23 import common
24
25
26 class Tracer(object):
27 ctor = 0
28 dtor = 0
29
30 def __init__(self):
31 Tracer.ctor += 1
32
33 def __del__(self):
34 Tracer.dtor += 1
35
36 @staticmethod
37 def reset():
38 Tracer.ctor = 0
39 Tracer.dtor = 0
40
41 @staticmethod
42 def leaked():
43 return Tracer.ctor - Tracer.dtor
44
45 @staticmethod
46 def attach(obj):
47 setattr(obj, "_trace", Tracer())
48
49
50 # This test finds reference counting leak by attaching an
51 # object that lives as long as the wrapper is alive.
52 # It can't detect Java reference counter leaks.
53 class Leak2TestCase(common.JPypeTestCase):
54
55 def setUp(self):
56 common.JPypeTestCase.setUp(self)
57 self.fixture = JClass('jpype.common.Fixture')()
58 Tracer.reset()
59
60 def testArrayCall(self):
61 JA = JArray(JInt)
62
63 def call():
64 inst = JA(100)
65 Tracer.attach(inst)
66 self.fixture.callObject(inst)
67 for i in range(100):
68 call()
69 self.assertEqual(Tracer.leaked(), 0)
70
71 def testArrayMemoryView(self):
72 JA = JArray(JInt)
73
74 def call():
75 inst = JA(100)
76 Tracer.attach(inst)
77 memoryview(inst)
78 for i in range(100):
79 call()
80 self.assertEqual(Tracer.leaked(), 0)
81
82 def testBufferCall(self):
83 byte_buffer = jpype.JClass('java.nio.ByteBuffer')
84
85 def call():
86 inst = byte_buffer.allocateDirect(10)
87 Tracer.attach(inst)
88 self.fixture.callObject(inst)
89 for i in range(100):
90 call()
91 self.assertEqual(Tracer.leaked(), 0)
92
93 def testBufferMemoryView(self):
94 byte_buffer = jpype.JClass('java.nio.ByteBuffer')
95
96 def call():
97 inst = byte_buffer.allocateDirect(10)
98 Tracer.attach(inst)
99 memoryview(inst)
100 for i in range(100):
101 call()
102 self.assertEqual(Tracer.leaked(), 0)