# -*- coding: utf-8 -*- """让"经中转"的 E2E 测试对网络抖动有容忍度。 问题: 这条链路实测可用率 ~75%(丢包 40%,到 Cloudflare 边缘还是美西)。 而这个测试串行发好多个请求,任何一个被丢就整体失败 —— 于是大概 1/4 的概率假失败,看着像产品有问题,其实是链路抖。 它要验证的是"**产品逻辑**跑得通",不是"链路百分百可靠" (后者由专门的可用率测量来盯)。所以重试 3 次,任一次全过就算过。 """ from __future__ import annotations import sys from pathlib import Path TEST = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_tests" r"\test_package_remote.py") OLD = ''' result = subprocess.run( [str(JAVA), "-Dfile.encoding=UTF-8", "-cp", str(classes), "JavaRelayTest", base, PAIR_CODE], capture_output=True, text=True, encoding="utf-8", errors="replace") tail = result.stdout.strip().splitlines()[-1] if result.stdout.strip() else "" check("Java 手表端经中转全部通过", result.returncode == 0 and "失败 0 项" in tail, tail)''' NEW = ''' # 这条链路实测只有 ~75% 可用(丢包 40%,边缘在美西), # 而这里串行发多个请求,任何一个被丢就整体失败 —— 会假失败。 # 测试要验证的是**产品逻辑**,不是链路可靠性(那有专门的可用率测量)。 # 所以重试 3 次,任一次全过就算过。 result = None tail = "" for attempt in range(3): result = subprocess.run( [str(JAVA), "-Dfile.encoding=UTF-8", "-cp", str(classes), "JavaRelayTest", base, PAIR_CODE], capture_output=True, text=True, encoding="utf-8", errors="replace") tail = (result.stdout.strip().splitlines()[-1] if result.stdout.strip() else "") if result.returncode == 0 and "失败 0 项" in tail: if attempt: print(f" (第 {attempt + 1} 次才通过 —— 链路抖动,不是产品问题)") break if attempt < 2: time.sleep(2.0) check("Java 手表端经中转全部通过", result is not None and result.returncode == 0 and "失败 0 项" in tail, tail)''' def main() -> int: text = TEST.read_text(encoding="utf-8") if "for attempt in range(3)" in text: print(" 已经改过") return 0 if OLD not in text: print(" [失败] 锚点没找到") return 1 text = text.replace(OLD, NEW, 1) # 确保有 time 导入和 subprocess 已导入 if "\nimport time" not in text: text = text.replace("\nimport tempfile", "\nimport tempfile\nimport time", 1) print(" 补了 import time") TEST.write_text(text, encoding="utf-8", newline="\n") import ast try: ast.parse(text) print(" 已改:E2E 测试重试 3 次") except SyntaxError as exc: print(f" 语法错误: 行 {exc.lineno}: {exc.msg}") return 1 return 0 if __name__ == "__main__": sys.exit(main())