Skip to content

Commit 05250ff

Browse files
committed
py gateway setup
1 parent bc99d93 commit 05250ff

File tree

4 files changed

+37
-92
lines changed

4 files changed

+37
-92
lines changed

test/ExampleApplication.java

-81
This file was deleted.

test/TestPy4JGateway.class

36 Bytes
Binary file not shown.

test/TestPy4JGateway.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
public class TestPy4JGateway {
33

44
public static void main(String[] args) {
5-
GatewayServer gatewayServer = new GatewayServer();
5+
GatewayServer gatewayServer = new GatewayServer(new Object());
66
gatewayServer.start();
77
System.out.println("Gateway Server Started");
88
}

test/testInfra.py

+36-10
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,60 @@
99
from py4j.java_gateway import JavaGateway
1010
from py4j.protocol import Py4JNetworkError
1111
from time import sleep
12+
from multiprocessing import Process
13+
from py4j.java_gateway import JavaGateway, GatewayParameters
1214

1315
PY4J_JAVA_PATH='.:../thirdParty/lib//py4j0.10.9.jar:$ZINGG_HOME/common/client/target/zingg-common-client-0.4.0-SNAPSHOT.jar'
14-
def start_example_server():
16+
def compileGatewayEntry():
1517
subprocess.call([
1618
"javac", "-cp", PY4J_JAVA_PATH,
1719
"TestPy4JGateway.java"])
20+
21+
def startGatewayEntry():
1822
subprocess.call([
1923
"java", "-Xmx512m", "-cp", PY4J_JAVA_PATH,
2024
"TestPy4JGateway"])
25+
26+
def start_example_app_process():
27+
# XXX DO NOT FORGET TO KILL THE PROCESS IF THE TEST DOES NOT SUCCEED
28+
p = Process(target=startGatewayEntry)
29+
p.start()
30+
sleep(2)
31+
return p
2132

22-
def check_connection(gateway):
33+
def check_connection(gateway_parameters=None):
34+
test_gateway = JavaGateway(gateway_parameters=gateway_parameters)
2335
try:
24-
gateway.jvm.System.currentTimeMillis()
36+
# Call a dummy method just to make sure we can connect to the JVM
37+
test_gateway.jvm.System.currentTimeMillis()
2538
except Py4JNetworkError:
39+
# We could not connect. Let"s wait a long time.
40+
# If it fails after that, there is a bug with our code!
2641
sleep(2)
42+
finally:
43+
test_gateway.close()
44+
45+
def safe_shutdown(instance):
46+
if hasattr(instance, 'gateway'):
47+
try:
48+
instance.gateway.shutdown()
49+
except Exception:
50+
print("exception")
2751

28-
class MyJavaClass:
29-
def addition(self, a, b):
30-
return a + b
52+
3153

3254
class MyJavaIntegrationTest(unittest.TestCase):
3355
def setUp(self):
34-
start_example_server()
35-
self.gateway = JavaGateway()
36-
check_connection(self.gateway)
56+
compileGatewayEntry()
57+
self.p = start_example_app_process()
58+
self.gateway = JavaGateway(
59+
gateway_parameters=GatewayParameters(auto_convert=True))
3760

3861
def tearDown(self):
39-
self.gateway.close()
62+
safe_shutdown(self)
63+
self.p.join()
64+
sleep(2)
65+
4066

4167
def test_jvm_access(self):
4268
print("Accessing the JVM...")

0 commit comments

Comments
 (0)