Skip to content

Commit 0b217ac

Browse files
committed
[GR-29352] Split tagged unittests into multiple jobs
PullRequest: graalpython/1633
2 parents 5b73bef + 026d19e commit 0b217ac

File tree

2 files changed

+70
-45
lines changed

2 files changed

+70
-45
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "overlay": "cfa81790263e6faec8f9d4730631f1587b01947c" }
1+
{ "overlay": "6ddaf9b9d7fd029f2bd27f49e2dc63ee18ea3335" }

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

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
import glob
41-
import os
42-
import subprocess
4340
import sys
41+
42+
import glob
4443
import test
4544

45+
import os
46+
import subprocess
4647

4748
if os.environ.get("ENABLE_CPYTHON_TAGGED_UNITTESTS") == "true" or __name__ == "__main__":
4849
TAGS_DIR = os.path.join(os.path.dirname(__file__), "unittest_tags")
4950
else:
5051
TAGS_DIR = "null"
5152

52-
5353
RUNNER = os.path.join(os.path.dirname(__file__), "run_cpython_test.py")
5454

5555

@@ -61,7 +61,7 @@ def working_selectors(tagfile):
6161
return None
6262

6363

64-
def working_tests():
64+
def collect_working_tests():
6565
working_tests = []
6666
glob_pattern = os.path.join(TAGS_DIR, "*.txt")
6767
for arg in sys.argv:
@@ -72,39 +72,60 @@ def working_tests():
7272
for tagfile in glob.glob(glob_pattern):
7373
test = os.path.splitext(os.path.basename(tagfile))[0]
7474
working_tests.append((test, working_selectors(tagfile)))
75-
return working_tests
76-
77-
78-
class TestAllWorkingTests():
79-
pass
80-
81-
WORKING_TESTS = working_tests()
82-
for idx, working_test in enumerate(WORKING_TESTS):
83-
def make_test_func(working_test):
84-
def fun(self):
85-
cmd = [sys.executable]
86-
if "--inspect" in sys.argv:
87-
cmd.append("--inspect")
88-
if "-debug-java" in sys.argv:
89-
cmd.append("-debug-java")
90-
cmd += ["-S", RUNNER]
91-
for testpattern in working_test[1]:
92-
cmd.extend(["-k", testpattern])
93-
testmod = working_test[0].rpartition(".")[2]
94-
print("Running test:", working_test[0])
95-
testfile = os.path.join(os.path.dirname(test.__file__), "%s.py" % testmod)
96-
if not os.path.isfile(testfile):
97-
testfile = os.path.join(os.path.dirname(test.__file__), "%s/__init__.py" % testmod)
98-
cmd.append(testfile)
99-
subprocess.check_call(cmd)
100-
print(working_test[0], "was finished.")
75+
return sorted(working_tests)
76+
77+
78+
def make_test_function(working_test):
79+
testmod = working_test[0].rpartition(".")[2]
80+
81+
def test_tagged():
82+
cmd = [sys.executable]
83+
if "--inspect" in sys.argv:
84+
cmd.append("--inspect")
85+
if "-debug-java" in sys.argv:
86+
cmd.append("-debug-java")
87+
cmd += ["-S", RUNNER]
88+
for testpattern in working_test[1]:
89+
cmd.extend(["-k", testpattern])
90+
print("Running test:", working_test[0])
91+
testfile = os.path.join(os.path.dirname(test.__file__), "%s.py" % testmod)
92+
if not os.path.isfile(testfile):
93+
testfile = os.path.join(os.path.dirname(test.__file__), "%s/__init__.py" % testmod)
94+
cmd.append(testfile)
95+
subprocess.check_call(cmd)
96+
print(working_test[0], "was finished.")
10197

102-
fun.__name__ = "%s[%d/%d]" % (working_test[0], idx + 1, len(WORKING_TESTS))
103-
return fun
98+
if testmod.startswith('test_'):
99+
test_tagged.__name__ = testmod
100+
else:
101+
test_tagged.__name__ = 'test_' + testmod
102+
return test_tagged
103+
104+
105+
def make_tests_class():
106+
import unittest
107+
108+
global TestTaggedUnittests
109+
110+
class TestTaggedUnittests(unittest.TestCase):
111+
pass
112+
113+
partial = os.environ.get('TAGGED_UNITTEST_PARTIAL')
114+
if partial:
115+
selected_str, total_str = partial.split('/', 1)
116+
selected = int(selected_str) - 1
117+
total = int(total_str)
118+
else:
119+
selected = 0
120+
total = 1
121+
assert selected < total
104122

105-
test_f = make_test_func(working_test)
106-
setattr(TestAllWorkingTests, test_f.__name__, test_f)
107-
del test_f
123+
working_tests = collect_working_tests()[selected::total]
124+
for idx, working_test in enumerate(working_tests):
125+
fn = make_test_function(working_test)
126+
fn.__name__ = "%s[%d/%d]" % (fn.__name__, idx + 1, len(working_tests))
127+
fn.__qualname__ = "%s.%s" % (TestTaggedUnittests.__name__, fn.__name__)
128+
setattr(TestTaggedUnittests, fn.__name__, staticmethod(fn))
108129

109130

110131
# This function has a unittest in test_tagger
@@ -130,11 +151,8 @@ def parse_unittest_output(output):
130151
return
131152

132153

133-
if __name__ == "__main__":
134-
# find working tests
135-
import re
136-
137-
executable = sys.executable.split(" ") # HACK: our sys.executable on Java is a cmdline
154+
def main():
155+
executable = sys.executable.split(" ") # HACK: our sys.executable on Java is a cmdline
138156
kwargs = {"stdout": subprocess.PIPE, "stderr": subprocess.PIPE, "text": True, "check": False}
139157

140158
glob_pattern = os.path.join(os.path.dirname(test.__file__), "test_*.py")
@@ -195,7 +213,7 @@ def parse_unittest_output(output):
195213
# shouldn't be tried).
196214
continue
197215

198-
print("[%d/%d, Try %d] Testing %s" %(idx + 1, len(testfiles), repeat + 1, testmod))
216+
print("[%d/%d, Try %d] Testing %s" % (idx + 1, len(testfiles), repeat + 1, testmod))
199217
for selector in test_selectors:
200218
cmd += ["-k", selector]
201219
cmd.append(testfile)
@@ -232,8 +250,9 @@ def parse_unittest_output(output):
232250
continue
233251
print(f"Suite succeeded with {len(passing_tests)} tests")
234252
break
235-
elif p.returncode == -9:
236-
print(f"\nTimeout (return code -9)\nyou can try to increase the current timeout {tout}s by using --timeout=NNN")
253+
elif p.returncode == -9:
254+
print(
255+
f"\nTimeout (return code -9)\nyou can try to increase the current timeout {tout}s by using --timeout=NNN")
237256
break
238257
else:
239258
print(f"Suite failed, retrying with {len(passing_tests)} tests")
@@ -246,3 +265,9 @@ def parse_unittest_output(output):
246265
os.unlink(tagfile)
247266
except Exception:
248267
pass
268+
269+
270+
if __name__ == '__main__':
271+
main()
272+
else:
273+
make_tests_class()

0 commit comments

Comments
 (0)