From f8db1240b1338df13297d92ad3806c8d76765fda Mon Sep 17 00:00:00 2001 From: NekoYume517 <3146527494@qq.com> Date: Mon, 16 Nov 2026 10:15:45 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E7=A7=9F=E7=BA=A6=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E9=9A=94=E7=A6=BB=EF=BC=88XBCR=5FDISABLE=5FLEASES=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E8=B7=A8=E8=BF=9B=E7=A8=8B=E7=A7=9F?= =?UTF-8?q?=E7=BA=A6=E6=8A=8A=E5=8D=95=E6=B5=8B=E6=8B=A6=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - db.acquire_lease 支持 XBCR_DISABLE_LEASES=1 直接放行(仅测试用) - test_order_sync 设置该开关;test_leases 在自己的 setUp 里临时关闭并恢复 - 顺手把 map.js 的自动拆分实验回滚(脚本按缩进切函数不安全, 函数在文件里的括号深度不一致,容易切坏),保留已验证的 src/core/geo.js --- db.py | 5 +++++ tests/test_leases.py | 5 +++++ tests/test_order_sync.py | 3 +++ 3 files changed, 13 insertions(+) diff --git a/db.py b/db.py index 6f4b785..70e399b 100644 --- a/db.py +++ b/db.py @@ -37,6 +37,11 @@ def acquire_lease(name: str, ttl_seconds: float = 120): 调用方要把 None 当「按单进程处理」,否则老库上所有后台任务会被误拦。 BEGIN IMMEDIATE 保证「读-判断-写」原子:多进程同时抢只有一个成功。 """ + # 测试里用临时库、但租约走的是真实库:给一个显式开关,避免上一轮进程 + # 留下的租约(300 秒)把这一轮的单测全拦掉。 + if _os.environ.get("XBCR_DISABLE_LEASES") == "1": + return True + now = _time.time() conn = get_db() try: diff --git a/tests/test_leases.py b/tests/test_leases.py index d41cdb3..3c1a2f2 100644 --- a/tests/test_leases.py +++ b/tests/test_leases.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """跨进程租约(多 worker 选主 / 单任务抢占)。""" +import os import sqlite3 import tempfile import unittest @@ -10,6 +11,8 @@ import db class LeaseTests(unittest.TestCase): def setUp(self): + # 其它用例会设 XBCR_DISABLE_LEASES 让租约直接放行;这里要真测租约 + self._old_flag = os.environ.pop("XBCR_DISABLE_LEASES", None) self.tmp = tempfile.TemporaryDirectory() self.path = Path(self.tmp.name) / "lease.db" self._old = db.DB_PATH @@ -18,6 +21,8 @@ class LeaseTests(unittest.TestCase): def tearDown(self): db.DB_PATH = self._old + if self._old_flag is not None: + os.environ["XBCR_DISABLE_LEASES"] = self._old_flag self.tmp.cleanup() def test_first_acquire_wins_and_second_is_blocked_for_other_owner(self): diff --git a/tests/test_order_sync.py b/tests/test_order_sync.py index 121f99f..d07e392 100644 --- a/tests/test_order_sync.py +++ b/tests/test_order_sync.py @@ -1,6 +1,9 @@ import sqlite3 import tempfile import unittest +import os + +os.environ["XBCR_DISABLE_LEASES"] = "1" # 单测不参与跨进程租约 from pathlib import Path from unittest.mock import patch