Stabilize timeout tests with semantic assertions (#16537)

Replace fragile wall-clock timeout assertions with semantic checks for
deadline errors, retry suppression, and event ordering. Keep only
lower-bound timing checks where they prove backoff behavior. This
reduces CPU-load flakes without weakening regression coverage.
This commit is contained in:
Zhichang Yu
2026-07-02 10:56:38 +08:00
committed by GitHub
parent 3195d6fa89
commit ba552f64b9
15 changed files with 418 additions and 243 deletions

View File

@@ -1,10 +1,12 @@
#!/usr/bin/env python3
from __future__ import annotations
import ast
import json
import re
import subprocess
import sys
import tokenize
from pathlib import Path
import yaml
@@ -12,6 +14,10 @@ import yaml
MERGE_PATTERNS = ("<<<<<<< ", "=======\n", ">>>>>>> ")
# Printable ASCII (0x20-0x7E) plus newline — matches the regex used by the
# historical check_comment_ascii.py.
_PRINTABLE_ASCII = re.compile(r"^[\n -~]*\Z")
def _read_bytes(path: Path) -> bytes:
return path.read_bytes()
@@ -158,6 +164,52 @@ def check_case_conflicts(_: list[Path], fix: bool = False) -> int:
return _report(errors)
def check_comment_ascii(paths: list[Path], fix: bool = False) -> int:
"""Ensure Python comments and docstrings contain only ASCII characters.
Ported from the legacy check_comment_ascii.py. The fix flag is accepted
for signature consistency but no auto-fix exists — non-ASCII comments
must be rewritten by hand.
"""
errors: list[str] = []
for path in paths:
if path.suffix != ".py" or not path.is_file():
continue
# A common comment begins with `#`
try:
with tokenize.open(path) as fp:
for tk in tokenize.generate_tokens(fp.readline):
if tk.type == tokenize.COMMENT and not _PRINTABLE_ASCII.fullmatch(tk.string):
errors.append(f"non-ASCII comment: {path}:{tk.start[0]}: {tk.string}")
except (OSError, SyntaxError, UnicodeDecodeError, tokenize.TokenError):
# Skip files that can't be tokenised (binary, bad encoding decl,
# syntax errors). Other tools (e.g. ruff) handle those separately.
pass
# A docstring begins and ends with `'''` (or `"""`)
try:
source = path.read_text()
except (OSError, UnicodeDecodeError):
continue
try:
tree = ast.parse(source, filename=str(path))
except SyntaxError:
continue
for node in ast.walk(tree):
# AsyncFunctionDef is included alongside FunctionDef so that
# `async def` docstrings are also validated; without it, a
# non-ASCII docstring on an async function would slip past
# the scan silently.
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Module)):
continue
doc = ast.get_docstring(node)
if not doc or _PRINTABLE_ASCII.fullmatch(doc):
continue
first_line = doc.splitlines()[0] if doc.splitlines() else doc
errors.append(f"non-ASCII docstring: {path}:{node.lineno}: {first_line}")
return _report(errors)
CHECKS = {
"json": check_json,
"yaml": check_yaml,
@@ -167,6 +219,7 @@ CHECKS = {
"merge-conflict": check_merge_conflicts,
"symlinks": check_symlinks,
"case-conflict": check_case_conflicts,
"comment-ascii": check_comment_ascii,
}