52
52
verbose = False
53
53
54
54
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
+
55
67
def dump_truffle_ast (func ):
56
68
try :
57
69
print (__dump_truffle_ast__ (func ))
@@ -72,7 +84,8 @@ def __init__(self):
72
84
73
85
def run_safely (self , func , print_immediately = False ):
74
86
if verbose :
75
- print (u"\n \t \u21B3 " , func .__name__ , " " , end = "" )
87
+ with result_lock :
88
+ print (u"\n \t \u21B3 " , func .__name__ , " " , end = "" )
76
89
try :
77
90
func ()
78
91
except BaseException as e :
@@ -102,10 +115,19 @@ def run_test(self, func):
102
115
pass
103
116
elif not hasattr (func , "__call__" ):
104
117
pass
105
- elif self .run_safely (func ):
106
- self .success ()
107
118
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 ()
109
131
110
132
def success (self ):
111
133
self .passed += 1
@@ -318,6 +340,11 @@ def run(self):
318
340
self .failed += testcase .failed
319
341
if verbose :
320
342
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 ()
321
348
print ("\n \n Ran %d tests (%d passes, %d failures)" % (self .passed + self .failed , self .passed , self .failed ))
322
349
for e in self .exceptions :
323
350
print (e )
0 commit comments