perf(douyu): 拆分任务槽并改造绑定扫码调度
This commit is contained in:
@@ -114,7 +114,9 @@ def get_test_engine() -> Engine:
|
||||
pool_recycle=1800,
|
||||
)
|
||||
database_module.engine = _test_engine
|
||||
database_module.worker_engine = _test_engine
|
||||
database_module.SessionLocal.configure(bind=_test_engine)
|
||||
database_module.WorkerSessionLocal.configure(bind=_test_engine)
|
||||
return _test_engine
|
||||
|
||||
|
||||
@@ -163,6 +165,23 @@ def _add_missing_columns(engine: Engine) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _add_missing_indexes(engine: Engine) -> None:
|
||||
"""Create model indexes added after the reusable test schema."""
|
||||
inspector = inspect(engine)
|
||||
with engine.begin() as conn:
|
||||
for table in Base.metadata.sorted_tables:
|
||||
if not inspector.has_table(table.name):
|
||||
continue
|
||||
existing = {
|
||||
str(index["name"])
|
||||
for index in inspector.get_indexes(table.name)
|
||||
if index.get("name")
|
||||
}
|
||||
for index in table.indexes:
|
||||
if index.name and index.name not in existing:
|
||||
index.create(bind=conn)
|
||||
|
||||
|
||||
def init_test_schema(engine: Engine | None = None) -> None:
|
||||
"""确保测试库中所有表结构存在并清空所有数据(幂等高效)。"""
|
||||
eng = engine or get_test_engine()
|
||||
@@ -170,6 +189,7 @@ def init_test_schema(engine: Engine | None = None) -> None:
|
||||
_add_missing_columns(eng)
|
||||
Base.metadata.create_all(eng)
|
||||
clear_tables(eng)
|
||||
_add_missing_indexes(eng)
|
||||
except OperationalError as exc:
|
||||
# 测试不再使用 SQLite,连不上库时必须给出可操作的提示,
|
||||
# 而不是抛一段 SQLAlchemy 原始堆栈。
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
@@ -13,11 +14,18 @@ from web.backend.main import health
|
||||
from web.backend.models import Account, DouyuTask, User
|
||||
from web.backend.routers import douyu as douyu_router
|
||||
from web.backend.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import business_events, douyu_concurrency, douyu_service
|
||||
from web.backend.services import (
|
||||
binding_poll_scheduler,
|
||||
business_events,
|
||||
douyu_concurrency,
|
||||
douyu_service,
|
||||
)
|
||||
from web.backend.services.douyu_runner import DouyuBatchRunner
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
)
|
||||
from web.backend.services.task_runtime import TaskLane, douyu_lane_for_task
|
||||
|
||||
|
||||
def _fixtures(db, *, status: str = "planned") -> tuple[User, Account, DouyuTask]:
|
||||
@@ -156,6 +164,176 @@ def test_cleanup_commits_stopped_status_before_account_event(db, monkeypatch):
|
||||
assert observed == ["stopped"]
|
||||
|
||||
|
||||
def test_cleanup_preserves_recoverable_binding_poll(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC) + timedelta(seconds=5)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
db.commit()
|
||||
|
||||
cleaned = douyu_service.cleanup_orphan_douyu_tasks(
|
||||
db,
|
||||
active_batch_ids=set(),
|
||||
statuses=("running",),
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert cleaned == 0
|
||||
assert task.status == "running"
|
||||
assert task.queue_lane == "binding_poll"
|
||||
|
||||
|
||||
def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
due_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = due_at
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
claimed = binding_poll_scheduler._claim_due_binding_tasks(10)
|
||||
db.refresh(task)
|
||||
assert claimed == [task.id]
|
||||
assert task.next_run_at is not None
|
||||
assert task.next_run_at > due_at
|
||||
finally:
|
||||
with binding_poll_scheduler._in_flight_lock:
|
||||
binding_poll_scheduler._in_flight.discard(task.id)
|
||||
|
||||
|
||||
def test_bind_qr_schedules_poll_and_releases_worker(db, monkeypatch):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {
|
||||
"act_alias": "bind-act",
|
||||
"role_name": "old-role",
|
||||
"is_bound_act": True,
|
||||
}
|
||||
snapshot = {
|
||||
"role_name": "old-role",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": False,
|
||||
}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_baseline_bind_info", lambda *_args: info)
|
||||
monkeypatch.setattr(runner, "_pick_change_wait_bind_info", lambda *_args: None)
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_wait_bind_role_result",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("scan wait must not block the worker")
|
||||
),
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "bind-act",
|
||||
"legacy_act_alias": "legacy-act",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "running"
|
||||
assert task.queue_lane == "binding_poll"
|
||||
assert task.next_run_at is not None
|
||||
assert task.run_deadline_at is not None
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
|
||||
|
||||
def test_binding_poll_once_finishes_when_role_is_found(db, monkeypatch):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["bind-act"],
|
||||
"baseline_role_name": "old-role",
|
||||
"bind_polling": True,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {"act_alias": "bind-act", "role_name": "new-role"}
|
||||
snapshot = {
|
||||
"role_name": "new-role",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": False,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
client = type("Client", (), {})()
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
monkeypatch.setattr(runner, "_is_pending_role", lambda *_args, **_kwargs: True)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_format_bind_summary",
|
||||
lambda *_args, **_kwargs: "role=new-role",
|
||||
)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "bind-act",
|
||||
"legacy_act_alias": "legacy-act",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.queue_lane == ""
|
||||
assert task.next_run_at is None
|
||||
assert task.result["role_name"] == "new-role"
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
|
||||
|
||||
def test_active_task_budget_rejects_new_batch(db, monkeypatch):
|
||||
user = User(username="budget-user", password_hash="hash", role="operation")
|
||||
db.add(user)
|
||||
@@ -211,6 +389,13 @@ def test_batch_task_concurrency_is_capped_at_five():
|
||||
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 5
|
||||
|
||||
|
||||
def test_douyu_lanes_separate_interactive_query_payment_and_writes():
|
||||
assert douyu_lane_for_task("get_bind_qr") == TaskLane.INTERACTIVE_BINDING
|
||||
assert douyu_lane_for_task("query_account_overview") == TaskLane.QUERY_BATCH
|
||||
assert douyu_lane_for_task("create_gold_qr") == TaskLane.PAYMENT_WAIT
|
||||
assert douyu_lane_for_task("exchange_goods") == TaskLane.OPS_WRITE
|
||||
|
||||
|
||||
def test_overview_limiter_blocks_extra_workers(monkeypatch):
|
||||
slots = threading.BoundedSemaphore(1)
|
||||
monkeypatch.setattr(douyu_concurrency, "_overview_slots", slots)
|
||||
|
||||
@@ -40,7 +40,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261120_0100"
|
||||
HEAD_REVISION = "20261120_0101"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -1398,6 +1399,118 @@ class TestBindingAndCompletion(_Base):
|
||||
assert task.issue_code == ""
|
||||
assert task.last_error == ""
|
||||
|
||||
def test_customer_start_mapping_missing_is_idempotent(self, monkeypatch):
|
||||
task = self.task(mapped=False)
|
||||
task.issue_code = "mapping_missing"
|
||||
self.session.commit()
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"allocate_task",
|
||||
lambda **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("mapping_missing must not allocate synchronously")
|
||||
),
|
||||
)
|
||||
|
||||
for _ in range(5):
|
||||
payload = asyncio.run(
|
||||
service.customer_start_binding(self.session, task_id=task.id)
|
||||
)
|
||||
assert payload["accepted"] is False
|
||||
assert payload["queueState"] == "mapping_missing"
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.account_id is None
|
||||
assert task.bind_task_id is None
|
||||
|
||||
def test_customer_start_queues_allocation_without_sync_network(self, monkeypatch):
|
||||
task = self.task()
|
||||
calls: list[list[int]] = []
|
||||
monkeypatch.setattr(
|
||||
"web.backend.services.platform_fulfillment.auto_allocator.wake_auto_allocator",
|
||||
lambda task_ids=None: calls.append(list(task_ids or [])),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"allocate_task",
|
||||
lambda **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("binding start must not allocate in the request")
|
||||
),
|
||||
)
|
||||
|
||||
payload = asyncio.run(service.customer_start_binding(self.session, task_id=task.id))
|
||||
|
||||
assert payload["accepted"] is True
|
||||
assert payload["queueState"] == "allocation_queued"
|
||||
assert calls == [[task.id]]
|
||||
|
||||
def test_customer_start_creates_one_binding_task_and_links_platform(self, monkeypatch):
|
||||
task = self._allocated()
|
||||
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
|
||||
|
||||
first = asyncio.run(service.customer_start_binding(self.session, task_id=task.id))
|
||||
second = asyncio.run(service.customer_start_binding(self.session, task_id=task.id))
|
||||
|
||||
assert first["accepted"] is True
|
||||
assert second["accepted"] is False
|
||||
bind_tasks = (
|
||||
self.session.query(DouyuTask)
|
||||
.filter(
|
||||
DouyuTask.platform_task_id == task.id,
|
||||
DouyuTask.task_type == "get_bind_qr",
|
||||
)
|
||||
.all()
|
||||
)
|
||||
assert len(bind_tasks) == 1
|
||||
self.session.refresh(task)
|
||||
assert task.bind_task_id == bind_tasks[0].id
|
||||
|
||||
def test_concurrent_start_binding_creates_one_task(self, monkeypatch):
|
||||
task = self._allocated()
|
||||
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
|
||||
barrier = threading.Barrier(2)
|
||||
errors: list[BaseException] = []
|
||||
other_session = make_test_session(self.engine)
|
||||
other_admin = other_session.get(User, self.admin_id)
|
||||
assert other_admin is not None
|
||||
|
||||
def start(db, current):
|
||||
try:
|
||||
barrier.wait(timeout=3)
|
||||
asyncio.run(
|
||||
service.start_binding(
|
||||
db,
|
||||
task_id=task.id,
|
||||
current=current,
|
||||
)
|
||||
)
|
||||
except BaseException as exc: # noqa: BLE001 - surfaced below
|
||||
errors.append(exc)
|
||||
|
||||
first = threading.Thread(target=start, args=(self.session, self.admin))
|
||||
second = threading.Thread(target=start, args=(other_session, other_admin))
|
||||
first.start()
|
||||
second.start()
|
||||
first.join(timeout=10)
|
||||
second.join(timeout=10)
|
||||
other_session.close()
|
||||
|
||||
assert not first.is_alive()
|
||||
assert not second.is_alive()
|
||||
assert errors == []
|
||||
verify = make_test_session(self.engine)
|
||||
try:
|
||||
bind_tasks = (
|
||||
verify.query(DouyuTask)
|
||||
.filter(
|
||||
DouyuTask.platform_task_id == task.id,
|
||||
DouyuTask.task_type == "get_bind_qr",
|
||||
)
|
||||
.all()
|
||||
)
|
||||
assert len(bind_tasks) == 1
|
||||
finally:
|
||||
verify.close()
|
||||
|
||||
def test_confirm_binding_pins_expected_role_for_two_role_snapshot(
|
||||
self, monkeypatch
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user