-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e36c55b
commit d4d57ea
Showing
7 changed files
with
211 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/org/jpy/fixtures/MultiThreadedEvalTestFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jpy.fixtures; | ||
|
||
import org.jpy.PyInputMode; | ||
import org.jpy.PyLib; | ||
import org.jpy.PyObject; | ||
|
||
import java.util.List; | ||
|
||
public class MultiThreadedEvalTestFixture { | ||
|
||
public static void expression(String expression, int numThreads) { | ||
PyObject globals = PyLib.getCurrentGlobals(); | ||
PyObject locals = PyLib.getCurrentLocals(); | ||
|
||
List<Thread> threads = new java.util.ArrayList<>(); | ||
for (int i = 0; i < numThreads; i++) { | ||
threads.add(new Thread(() -> { | ||
PyObject.executeCode(expression, PyInputMode.EXPRESSION, globals, locals); | ||
})); | ||
} | ||
for (Thread thread : threads) { | ||
thread.start(); | ||
} | ||
for (Thread thread : threads) { | ||
try { | ||
thread.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
public static void script(String expression, int numThreads) { | ||
List<Thread> threads = new java.util.ArrayList<>(); | ||
PyObject globals = PyLib.getCurrentGlobals(); | ||
PyObject locals = PyLib.getCurrentLocals(); | ||
for (int i = 0; i < numThreads; i++) { | ||
threads.add(new Thread(() -> { | ||
PyObject.executeCode(expression, PyInputMode.SCRIPT, globals, locals); | ||
})); | ||
} | ||
for (Thread thread : threads) { | ||
thread.start(); | ||
} | ||
for (Thread thread : threads) { | ||
try { | ||
thread.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import unittest | ||
|
||
import jpyutil | ||
|
||
jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/classes', 'target/test-classes']) | ||
import jpy | ||
# jpy.diag.flags = jpy.diag.F_TYPE | ||
|
||
NUM_THREADS = 20 | ||
|
||
|
||
class MultiThreadedTestEvalExec(unittest.TestCase): | ||
def setUp(self): | ||
self.fixture = jpy.get_type("org.jpy.fixtures.MultiThreadedEvalTestFixture") | ||
self.assertIsNotNone(self.fixture) | ||
|
||
def test_inc_baz(self): | ||
baz = 15 | ||
self.fixture.script("baz = baz + 1; self.assertGreater(baz, 15)", NUM_THREADS) | ||
# note: this *is* correct wrt python semantics w/ exec(code, globals(), locals()) | ||
# https://bugs.python.org/issue4831 (Note: it's *not* a bug, is working as intended) | ||
self.assertEqual(baz, 15) | ||
|
||
def test_exec_import(self): | ||
import sys | ||
self.assertTrue("json" not in sys.modules) | ||
self.fixture.script("import json", NUM_THREADS) | ||
self.assertTrue("json" in sys.modules) | ||
|
||
def test_java_threading_jpy_get_type(self): | ||
self.fixture.script("j_child1_class = jpy.get_type(\"org.jpy.fixtures.CyclicReferenceChild1\");j_child2_class =" | ||
"jpy.get_type(\"org.jpy.fixtures.CyclicReferenceChild2\")", NUM_THREADS) | ||
|
||
def test_py_threading_jpy_get_type(self): | ||
import threading | ||
|
||
test_self = self | ||
|
||
class MyThread(threading.Thread): | ||
def __init__(self): | ||
threading.Thread.__init__(self) | ||
|
||
def run(self): | ||
barrier.wait() | ||
j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1") | ||
j_child2_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild2") | ||
j_child2 = j_child2_class() | ||
j_child1 = j_child1_class.of(8) | ||
test_self.assertEqual(88, j_child1.parentMethod()) | ||
test_self.assertEqual(888, j_child1.grandParentMethod()) | ||
test_self.assertIsNone(j_child1.refChild2(j_child2)) | ||
test_self.assertEqual(8, j_child1.get_x()) | ||
test_self.assertEqual(10, j_child1.y) | ||
test_self.assertEqual(100, j_child1.z) | ||
|
||
barrier = threading.Barrier(NUM_THREADS) | ||
threads = [] | ||
for i in range(NUM_THREADS): | ||
t = MyThread() | ||
t.start() | ||
threads.append(t) | ||
|
||
for t in threads: | ||
t.join() | ||
|
||
|
||
if __name__ == '__main__': | ||
print('\nRunning ' + __file__) | ||
unittest.main() |