Skip to content

Commit 49f1118

Browse files
committed
allow parallel execution of test methods in a test class
1 parent 68ec965 commit 49f1118

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@
5252
verbose = False
5353

5454

55+
import threading
56+
import _thread
57+
result_lock = threading.RLock()
58+
if os.environ.get(b"ENABLE_THREADED_GRAALPYTEST") == b"true":
59+
thread_count = os.cpu_count()
60+
thread_token = threading.Semaphore(thread_count)
61+
print("Running with %d threads" % thread_count)
62+
else:
63+
thread_count = 0
64+
thread_token = None
65+
66+
5567
def dump_truffle_ast(func):
5668
try:
5769
print(__dump_truffle_ast__(func))
@@ -72,7 +84,8 @@ def __init__(self):
7284

7385
def run_safely(self, func, print_immediately=False):
7486
if verbose:
75-
print(u"\n\t\u21B3 ", func.__name__, " ", end="")
87+
with result_lock:
88+
print(u"\n\t\u21B3 ", func.__name__, " ", end="")
7689
try:
7790
func()
7891
except BaseException as e:
@@ -102,10 +115,19 @@ def run_test(self, func):
102115
pass
103116
elif not hasattr(func, "__call__"):
104117
pass
105-
elif self.run_safely(func):
106-
self.success()
107118
else:
108-
self.failure()
119+
def do_run():
120+
r = self.run_safely(func)
121+
with result_lock:
122+
self.success() if r else self.failure()
123+
if thread_token:
124+
thread_token.release()
125+
126+
if thread_token:
127+
thread_token.acquire()
128+
threading.Thread(target=do_run).start()
129+
else:
130+
do_run()
109131

110132
def success(self):
111133
self.passed += 1
@@ -318,6 +340,11 @@ def run(self):
318340
self.failed += testcase.failed
319341
if verbose:
320342
print()
343+
for i in range(thread_count):
344+
print("waiting for %d tests to finish" % (thread_count - i))
345+
thread_token.acquire() # waits until all threads are exited
346+
for i in range(thread_count):
347+
thread_token.release()
321348
print("\n\nRan %d tests (%d passes, %d failures)" % (self.passed + self.failed, self.passed, self.failed))
322349
for e in self.exceptions:
323350
print(e)

graalpython/com.oracle.graal.python.test/src/tests/test_tagged_unittests.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ def working_tests():
7272
return working_tests
7373

7474

75+
class TestAllWorkingTests():
76+
pass
77+
78+
7579
WORKING_TESTS = working_tests()
7680
for idx, working_test in enumerate(WORKING_TESTS):
7781
def make_test_func(working_test):
78-
def fun():
82+
def fun(self):
7983
cmd = [sys.executable, "-S", "-m", "unittest"]
8084
for testpattern in working_test[1]:
8185
cmd.extend(["-k", testpattern])
@@ -88,7 +92,7 @@ def fun():
8892
return fun
8993

9094
test_f = make_test_func(working_test)
91-
globals()[test_f.__name__] = test_f
95+
setattr(TestAllWorkingTests, test_f.__name__, test_f)
9296
del test_f
9397

9498

0 commit comments

Comments
 (0)