Skip to content

Commit 71c240a

Browse files
committed
Add test for ThreadPool.
1 parent 728636c commit 71c240a

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.nio.file.Files;
4646
import java.nio.file.Path;
4747
import java.nio.file.Paths;
48+
import java.util.Collections;
49+
import java.util.Map;
4850

4951
import org.graalvm.polyglot.Context;
5052
import org.graalvm.polyglot.Engine;
@@ -97,10 +99,14 @@ public class PythonTests {
9799
}
98100

99101
public static void enterContext(String... newArgs) {
102+
enterContext(Collections.emptyMap(), newArgs);
103+
}
104+
105+
public static void enterContext(Map<String, String> options, String[] args) {
100106
PythonTests.outArray.reset();
101107
PythonTests.errArray.reset();
102108
Context prevContext = context;
103-
context = Context.newBuilder().engine(engine).allowExperimentalOptions(true).allowAllAccess(true).arguments("python", newArgs).option("python.Executable", executable).build();
109+
context = Context.newBuilder().engine(engine).allowExperimentalOptions(true).allowAllAccess(true).options(options).arguments("python", args).option("python.Executable", executable).build();
104110
context.initialize("python");
105111
if (prevContext != null) {
106112
closeContext(prevContext);
@@ -333,6 +339,15 @@ public static Value runScript(String[] args, File path, OutputStream out, Output
333339
}
334340
}
335341

342+
public static Value runScript(Map<String, String> options, String[] args, String source, OutputStream out, OutputStream err) {
343+
try {
344+
enterContext(options, args);
345+
return context.eval(org.graalvm.polyglot.Source.create("python", source));
346+
} finally {
347+
flush(out, err);
348+
}
349+
}
350+
336351
public static Value runScript(String[] args, String source, OutputStream out, OutputStream err) {
337352
try {
338353
enterContext(args);
@@ -352,8 +367,12 @@ public static Value runScript(String[] args, org.graalvm.polyglot.Source source,
352367
}
353368

354369
public static Value runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
370+
return runScript(Collections.emptyMap(), args, source, out, err, cb);
371+
}
372+
373+
public static Value runScript(Map<String, String> options, String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
355374
try {
356-
enterContext(args);
375+
enterContext(options, args);
357376
return context.eval(org.graalvm.polyglot.Source.create("python", source));
358377
} finally {
359378
cb.run();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.test.module;
42+
43+
import static org.junit.Assert.assertEquals;
44+
45+
import java.io.ByteArrayOutputStream;
46+
import java.io.PrintStream;
47+
import java.util.Collections;
48+
49+
import org.junit.Test;
50+
51+
import com.oracle.graal.python.test.PythonTests;
52+
53+
public class ThreadPoolTests {
54+
55+
@Test
56+
public void threadPool() {
57+
String source = "import _sysconfig\n" +
58+
"assert _sysconfig.get_config_vars().get('WITH_THREAD'), 'context was not started for threading'\n" +
59+
"from multiprocessing.pool import ThreadPool\n" +
60+
"\n" +
61+
"def fun(item):\n" +
62+
" return item != None\n" +
63+
"\n" +
64+
"items = list(range(0, 10))\n" +
65+
"\n" +
66+
"pool = ThreadPool(2)\n" +
67+
"res = list(pool.imap(fun, items))\n" +
68+
"pool.close()\n" +
69+
"\n" +
70+
"print(res)\n";
71+
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
72+
final PrintStream printStream = new PrintStream(byteArray);
73+
PythonTests.runScript(Collections.singletonMap("python.WithThread", "true"), new String[0], source, printStream, System.err, () -> PythonTests.closeContext());
74+
String result = byteArray.toString().replaceAll("\r\n", "\n");
75+
assertEquals("[True, True, True, True, True, True, True, True, True, True]\n", result);
76+
}
77+
}

0 commit comments

Comments
 (0)