fix(platform): 防止绑定任务压垮连接池和事件循环
This commit is contained in:
@@ -15,6 +15,7 @@ from web.backend.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import (
|
||||
binding_poll_scheduler,
|
||||
business_events,
|
||||
douyu_batch_launcher,
|
||||
douyu_concurrency,
|
||||
douyu_service,
|
||||
payment_poll_scheduler,
|
||||
@@ -225,6 +226,61 @@ def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
binding_poll_scheduler._in_flight.discard(task.id)
|
||||
|
||||
|
||||
def test_binding_poll_dispatch_skips_claim_when_lane_is_saturated(monkeypatch):
|
||||
claimed_limits: list[int] = []
|
||||
monkeypatch.setattr(
|
||||
binding_poll_scheduler,
|
||||
"available_slots",
|
||||
lambda _lane: 0,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
binding_poll_scheduler,
|
||||
"_claim_due_binding_tasks",
|
||||
lambda limit: claimed_limits.append(limit) or [],
|
||||
)
|
||||
|
||||
submitted = binding_poll_scheduler.run_binding_poll_dispatch_once(20)
|
||||
|
||||
assert submitted == 0
|
||||
assert claimed_limits == []
|
||||
|
||||
|
||||
def test_batch_launcher_defers_database_session_to_worker(monkeypatch):
|
||||
submitted: dict[str, object] = {}
|
||||
|
||||
def fake_submit(lane, fn, /, *args, **kwargs):
|
||||
submitted.update(
|
||||
{
|
||||
"lane": lane,
|
||||
"fn": fn,
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
)
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(
|
||||
douyu_batch_launcher,
|
||||
"WorkerSessionLocal",
|
||||
lambda: (_ for _ in ()).throw(
|
||||
AssertionError("队列阶段不能创建数据库 Session")
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(douyu_batch_launcher, "submit", fake_submit)
|
||||
|
||||
queued = douyu_batch_launcher.start_douyu_batch(
|
||||
batch_id="deferred-session",
|
||||
task_type="get_bind_qr",
|
||||
payload={},
|
||||
concurrency=1,
|
||||
capture_event_loop=False,
|
||||
)
|
||||
|
||||
assert queued is True
|
||||
assert submitted["lane"] == TaskLane.BATCH_COORDINATOR
|
||||
assert submitted["fn"] is douyu_batch_launcher._run_batch
|
||||
|
||||
|
||||
def test_payment_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
due_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
|
||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0107"
|
||||
HEAD_REVISION = "20261121_0108"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
Reference in New Issue
Block a user