verify_timeout.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Verify ResolveTimeout logic: ini waitTime(seconds) -> payTimeout(ms)."""
  2. import re
  3. # 抽取 HttpServices.cs 里的 ResolveTimeout 逻辑,用 Python 等价实现验证换算
  4. src = open(r"E:\普瑞工作文件夹\源码\Project\win-service-machine-dll\UppPay\HttpServices.cs",
  5. encoding="utf-8-sig").read()
  6. m = re.search(r"private int ResolveTimeout.*?\n \}", src, re.S)
  7. print("=== ResolveTimeout 源码 ===")
  8. print(m.group(0) if m else "NOT FOUND")
  9. print()
  10. def resolve_timeout(ini, timeout_type):
  11. """模拟 C# 实现"""
  12. try:
  13. if timeout_type == "PAY":
  14. s = ini.get("Config", {}).get("waitTime", "")
  15. if s.strip() and int(s.strip()) > 0:
  16. return int(s.strip()) * 1000
  17. return 90000
  18. else:
  19. s = ini.get("Config", {}).get("queryTimeout", "")
  20. if s.strip() and int(s.strip()) > 0:
  21. return int(s.strip())
  22. return 10000
  23. except Exception:
  24. return 90000 if timeout_type == "PAY" else 10000
  25. cases = [
  26. ("现场 ini 原样 waitTime=60", {"Config": {"waitTime": "60", "warningTime": "5"}}, 60000, 10000),
  27. ("改后 waitTime=90", {"Config": {"waitTime": "90"}}, 90000, 10000),
  28. ("ini 完全没有 Config 段", {}, 90000, 10000),
  29. ("waitTime 空值", {"Config": {"waitTime": ""}}, 90000, 10000),
  30. ("waitTime 非法值", {"Config": {"waitTime": "abc"}}, 90000, 10000),
  31. ("waitTime=0", {"Config": {"waitTime": "0"}}, 90000, 10000),
  32. ("配了 queryTimeout=15000", {"Config": {"waitTime": "90", "queryTimeout": "15000"}}, 90000, 15000),
  33. ]
  34. print("%-26s %-12s %-12s %s" % ("场景", "PAY(ms)", "QUERY(ms)", "结论"))
  35. print("-" * 70)
  36. ok = True
  37. for name, ini, exp_pay, exp_q in cases:
  38. got_pay = resolve_timeout(ini, "PAY")
  39. got_q = resolve_timeout(ini, "QUERY")
  40. good = (got_pay == exp_pay and got_q == exp_q)
  41. ok = ok and good
  42. print("%-26s %-12d %-12d %s" % (name, got_pay, got_q, "PASS" if good else "FAIL(exp %d/%d)" % (exp_pay, exp_q)))
  43. print("-" * 70)
  44. print("ALL PASS" if ok else "HAS FAILURE")