|
5 | 5 | The positive tree check deliberately requires the integrated producers, rather |
6 | 6 | than accepting a recorded count or treating existing integration findings as a |
7 | 7 | successful check. The supervisor runs this suite on the completed tree. |
| 8 | +
|
| 9 | +Use --shard I/N (1-based) to run sorted loader-discovered ManifestCheckerTests |
| 10 | +ids at positions [I-1::N], plus the cheap partition guard in every shard. |
| 11 | +Without --shard, the usual unittest discovery and CLI selectors apply. |
8 | 12 | """ |
9 | 13 | from __future__ import annotations |
10 | 14 |
|
| 15 | +import argparse |
11 | 16 | import ast |
12 | 17 | import copy |
13 | 18 | import ctypes |
@@ -466,5 +471,69 @@ def test_duplicate_json_key_is_rejected(self): |
466 | 471 | self.assertIn("ownership[input]: duplicate JSON key format", result.stdout.splitlines()) |
467 | 472 |
|
468 | 473 |
|
| 474 | +def shard_case_ids(index: int, count: int) -> list[str]: |
| 475 | + ids = sorted(test.id() for test in |
| 476 | + unittest.TestLoader().loadTestsFromTestCase(ManifestCheckerTests)) |
| 477 | + return ids[index - 1::count] |
| 478 | + |
| 479 | + |
| 480 | +class ManifestCheckerShardingTests(unittest.TestCase): |
| 481 | + def test_shards_partition_every_case_exactly_once(self): |
| 482 | + # Discover the expectation independently of shard_case_ids, and use |
| 483 | + # index residues rather than reproducing its slicing implementation. |
| 484 | + ids = sorted(test.id() for test in |
| 485 | + unittest.TestLoader().loadTestsFromTestCase(ManifestCheckerTests)) |
| 486 | + self.assertGreaterEqual(len(ids), 30) |
| 487 | + for count in range(1, 9): |
| 488 | + with self.subTest(shards=count): |
| 489 | + seen = set() |
| 490 | + total = 0 |
| 491 | + for index in range(1, count + 1): |
| 492 | + shard = shard_case_ids(index, count) |
| 493 | + expected = [test_id for position, test_id in enumerate(ids) |
| 494 | + if position % count == index - 1] |
| 495 | + self.assertEqual(shard, expected) |
| 496 | + self.assertEqual(len(shard), len(set(shard))) |
| 497 | + self.assertTrue(seen.isdisjoint(shard)) |
| 498 | + seen.update(shard) |
| 499 | + total += len(shard) |
| 500 | + self.assertEqual(seen, set(ids)) |
| 501 | + self.assertEqual(total, len(ids)) |
| 502 | + |
| 503 | + |
| 504 | +def parse_shard(value: str) -> tuple[int, int]: |
| 505 | + message = "--shard must be I/N with integers satisfying 1 <= I <= N" |
| 506 | + if re.fullmatch(r"[1-9][0-9]*/[1-9][0-9]*", value) is None: |
| 507 | + raise argparse.ArgumentTypeError(message) |
| 508 | + index, count = map(int, value.split("/")) |
| 509 | + if index > count: |
| 510 | + raise argparse.ArgumentTypeError(message) |
| 511 | + return index, count |
| 512 | + |
| 513 | + |
469 | 514 | if __name__ == "__main__": |
470 | | - unittest.main() |
| 515 | + if any(arg == "--shard" or arg.startswith("--shard=") for arg in sys.argv[1:]): |
| 516 | + parser = argparse.ArgumentParser(allow_abbrev=False) |
| 517 | + parser.add_argument("--shard", type=parse_shard, metavar="I/N", |
| 518 | + required=True, action="append") |
| 519 | + args = parser.parse_args() |
| 520 | + if len(args.shard) != 1: |
| 521 | + parser.error("specify --shard exactly once") |
| 522 | + index, count = args.shard[0] |
| 523 | + guards = [test.id() for test in unittest.TestLoader().loadTestsFromTestCase( |
| 524 | + ManifestCheckerShardingTests)] |
| 525 | + selected = shard_case_ids(index, count) + guards |
| 526 | + if len(selected) == len(guards): |
| 527 | + parser.error("shard %d/%d selects no case" % (index, count)) |
| 528 | + # The ids go in argv: with module=None unittest ignores defaultTest and |
| 529 | + # would fall back to discovery (zero tests, exit 0 before Python 3.12). |
| 530 | + program = unittest.main(module=None, argv=[sys.argv[0], *selected], exit=False) |
| 531 | + # A shard that ran a different number of tests than it selected fails, |
| 532 | + # whatever unittest's own exit status says. |
| 533 | + if program.result.testsRun != len(selected) or not program.result.wasSuccessful(): |
| 534 | + print("shard %d/%d: ran %d tests, selected %d" % ( |
| 535 | + index, count, program.result.testsRun, len(selected)), file=sys.stderr) |
| 536 | + sys.exit(1) |
| 537 | + print("shard %d/%d: ran %d tests as selected" % (index, count, len(selected))) |
| 538 | + else: |
| 539 | + unittest.main() |
0 commit comments