File: test_jarray_fixes.py

package info (click to toggle)
python-jpype 0.6.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,560 kB
  • sloc: cpp: 11,957; python: 3,844; java: 986; ansic: 875; makefile: 149; xml: 76; sh: 62
file content (65 lines) | stat: -rwxr-xr-x 1,875 bytes parent folder | download
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
#!/usr/bin/env python
"""
Checks for memory leak in the JVM when pushing array data from Python to Java.
"""

import pytest
import jpype

#!!! These settings are finely tuned !!!
#!!! DO NOT fiddle with them unless you know what you are doing !!!

#   Size of array to be copied.
ARRAY_SIZE = 4000000

#   Number of iterations to run.
#   - raise this value to increase efficacy of the memory leak test.
ITERATIONS = 10

#   Maximum size of JVM heap.
#   - sets a cap to allow memory leak detection.
MAX_JVM_HEAP_SIZE_BYTES = 128647168

def setup_module(module):
    #   Module-level setup.
    if not jpype.isJVMStarted():
        jvm_args = [
            '-Xmx%dM' % (MAX_JVM_HEAP_SIZE_BYTES // 1000 ** 2),
        ]
        jpype.startJVM(jpype.getDefaultJVMPath(), *jvm_args)
    module.JavaDoubleArray = jpype.JArray(jpype.JDouble, 1)

def test_memory_leak_fix():
    """
    This test raises java.lang.VirtualMachineErrorPyRaisable
    (java.lang.OutOfMemoryError: Java heap space) if the memory leak
    is present.

    """
    #   Check memory settings.
    rt = jpype.java.lang.Runtime.getRuntime()
    assert rt.maxMemory() == MAX_JVM_HEAP_SIZE_BYTES

    #   Perform leak test.
    for i in xrange(ITERATIONS):
        print 'iteration:', i
        py_list1 = [float(f) for f in xrange(ARRAY_SIZE)]
        j_array1 = JavaDoubleArray(py_list1)
        py_list2 = j_array1[:]
        assert py_list1 == py_list2

def test_jarray_basic_slicing_fix():
    jl1 = JavaDoubleArray([1., 2., 3.])
    assert list(jl1) == [1., 2., 3.]
    assert list(jl1[0:-1]) == [1., 2.]
    assert list(jl1[0:1]) == [1.]

def test_jarray_slice_copy_fix():
    jl1 = JavaDoubleArray([1., 2., 3.])
    pl1 = jl1[:]
    assert list(jl1) == pl1

def test_jarray_slice_assignment_fix():
    jl2 = JavaDoubleArray([1., 2., 3.])
    jl2[:] = [4., 5., 6.]
    assert list(jl2) == [4., 5., 6.]