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