Debian Codegraph @

src/test/python/jpy_mt_eval_exec_test.py

raw symbols SCIP
1 import math
2 import unittest
3
4 import jpyutil
5
6 jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/classes', 'target/test-classes'])
7 import jpy
8
9 NUM_THREADS = 20
10
11
12 # A CPU-bound task: computing a large number of prime numbers
13 def is_prime(n: int) -> bool:
14 if n <= 1:
15 return False
16 for i in range(2, int(math.sqrt(n)) + 1):
17 if n % i == 0:
18 return False
19 return True
20
21
22 def count_primes(start: int, end: int) -> int:
23 count = 0
24 for i in range(start, end):
25 if is_prime(i):
26 count += 1
27 return count
28
29
30 def use_circular_java_classes():
31 j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1")
32 j_child2_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild2")
33 j_child2 = j_child2_class()
34 j_child1 = j_child1_class.of(8)
35 result = j_child1.parentMethod()
36 assert result == 88
37 assert 888 == j_child1.grandParentMethod()
38 j_child1.refChild2(j_child2)
39 assert 8 == j_child1.get_x()
40 assert 10 == j_child1.y
41 assert 100 == j_child1.z
42
43
44 class MultiThreadedTestEvalExec(unittest.TestCase):
45 def setUp(self):
46 self.fixture = jpy.get_type("org.jpy.fixtures.MultiThreadedEvalTestFixture")
47 self.assertIsNotNone(self.fixture)
48
49 def test_inc_baz(self):
50 baz = 15
51 self.fixture.script("baz = baz + 1; self.assertGreater(baz, 15)", NUM_THREADS)
52 # note: this *is* correct wrt python semantics w/ exec(code, globals(), locals())
53 # https://bugs.python.org/issue4831 (Note: it's *not* a bug, is working as intended)
54 self.assertEqual(baz, 15)
55
56 def test_exec_import(self):
57 import sys
58 self.assertTrue("json" not in sys.modules)
59 self.fixture.script("import json", NUM_THREADS)
60 self.assertTrue("json" in sys.modules)
61
62 def test_exec_function_call(self):
63 self.fixture.expression("use_circular_java_classes()", NUM_THREADS)
64
65 def test_count_primes(self):
66 self.fixture.expression("count_primes(1, 10000)", NUM_THREADS)
67
68 def test_java_threading_jpy_get_type(self):
69
70 py_script = """
71 j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1")
72 j_child2_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild2")
73 j_child2 = j_child2_class()
74 j_child1 = j_child1_class.of(8)
75 result = j_child1.parentMethod()
76 assert result == 88
77 assert 888 == j_child1.grandParentMethod()
78 j_child1.refChild2(j_child2)
79 assert 8 == j_child1.get_x()
80 assert 10 == j_child1.y
81 assert 100 == j_child1.z
82 """
83 self.fixture.script(py_script, NUM_THREADS)
84
85 def test_py_threading_jpy_get_type(self):
86 import threading
87
88 test_self = self
89
90 class MyThread(threading.Thread):
91 def __init__(self):
92 threading.Thread.__init__(self)
93
94 def run(self):
95 barrier.wait()
96 j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1")
97 j_child2_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild2")
98 j_child2 = j_child2_class()
99 j_child1 = j_child1_class.of(8)
100 test_self.assertEqual(88, j_child1.parentMethod())
101 test_self.assertEqual(888, j_child1.grandParentMethod())
102 test_self.assertIsNone(j_child1.refChild2(j_child2))
103 test_self.assertEqual(8, j_child1.get_x())
104 test_self.assertEqual(10, j_child1.y)
105 test_self.assertEqual(100, j_child1.z)
106
107 barrier = threading.Barrier(NUM_THREADS)
108 threads = []
109 for i in range(NUM_THREADS):
110 t = MyThread()
111 t.start()
112 threads.append(t)
113
114 for t in threads:
115 t.join()
116
117
118 if __name__ == '__main__':
119 print('\nRunning ' + __file__)
120 unittest.main()