| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 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) |