Debian Codegraph @

test/jpypetest/test_proxy_multithreaded.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 from jpype import *
19 import common
20 import time
21
22
23 class C:
24 def testMethodInt(self):
25 return 5
26
27 def testMethodObject(self):
28 return JPackage("jpype.proxy").ReturnObject(3)
29
30 def testMethodString(self):
31 return "Hallo"
32
33 def testMethodList(self, noOfValues):
34 responses = list()
35 for i in range(0, noOfValues):
36 responses.append(JPackage("jpype.proxy").ReturnObject(i))
37 return java.util.Arrays.asList(responses)
38
39
40 class ThreadCallbackImpl:
41 def __init__(self):
42 self.values = []
43
44 def notifyValue(self, val):
45 self.values.append(val)
46
47
48 class ProxyMultiThreadedTestCase(common.JPypeTestCase):
49
50 def setUp(self):
51 super(ProxyMultiThreadedTestCase, self).setUp()
52 self.package = JPackage("jpype.proxy")
53 self.executor = None
54
55 def tearDown(self):
56
57 if self.executor is not None:
58 self.executor.shutdown()
59
60 def testProxyWithSingleThreadExecution(self):
61 """Test multiple proxy calls with a single threaded executor.
62
63 The Java part will only call one proxy method (longer running methods)
64 at a time.
65
66 The methods should run correctly.
67 """
68
69 proxy = JProxy(self.package.TestInterface4, inst=C())
70
71 self.executor = self.package.ProxyExecutor(1)
72 self.executor.registerProxy(proxy, 10)
73
74 # register proxy executions
75 # threads
76
77 self.executor.runExecutor()
78 result = self.executor.waitForExecutedTasks()
79 expected = self.executor.getExpectedTasks()
80 self.assertEqual(result, expected,
81 "Executed Tasks should be the same.")
82
83 def testProxyWithMultipleThreadExecutionSingleCallbackInstance(self):
84 """Test multiple proxy calls with different threads at the same time.
85
86 The Java part will call the same callback instance from different
87 threads at a time.
88
89 Jpype library will throw an exception and the proxy method won't be
90 called:
91 java.lang.RuntimeException: Python exception thrown:
92 AttributeError: 'NoneType' object has no attribute 'getName'
93 at jpype.JPypeInvocationHandler.hostInvoke(Native Method)
94 at jpype.JPypeInvocationHandler.invoke(
95 JPypeInvocationHandler.java:10)
96 ...
97 """
98 proxy = JProxy(self.package.TestInterface4, inst=C())
99
100 self.executor = self.package.ProxyExecutor(10)
101 self.executor.registerProxy(proxy, 100)
102
103 # register proxy executions
104 # threads
105 self.executor.runExecutor()
106 result = self.executor.waitForExecutedTasks()
107
108 expected = self.executor.getExpectedTasks()
109 self.assertEqual(result, expected,
110 "Executed Tasks should be the same.")
111
112 def testProxyWithMultipleThreadExecutionMultiCallbackInstances(self):
113 """Test multiple proxy calls with different threads at the same time.
114
115 The Java part will call the proxy method from different callback
116 instances and from different threads at a time.
117
118 Jpype library will throw an exception and the proxy method won't be
119 called:
120 java.lang.RuntimeException: Python exception thrown:
121 AttributeError: 'NoneType' object has no attribute 'getName'
122 at jpype.JPypeInvocationHandler.hostInvoke(Native Method)
123 at jpype.JPypeInvocationHandler.invoke(
124 JPypeInvocationHandler.java:10)
125 ...
126 """
127
128 self.executor = self.package.ProxyExecutor(10)
129
130 for i in range(0, 5):
131 proxy = JProxy(self.package.TestInterface4, inst=C())
132 self.executor.registerProxy(proxy, 15)
133
134 # register proxy executions
135 # threads
136
137 self.executor.runExecutor()
138 result = self.executor.waitForExecutedTasks()
139 expected = self.executor.getExpectedTasks()
140 self.assertEqual(result, expected,
141 "Executed Tasks should be the same.")