220 lines
5.7 KiB
Python
220 lines
5.7 KiB
Python
"""账号同步队列执行器测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from tests.support.db import make_test_session
|
|
from web.backend.models import (
|
|
Account,
|
|
AccountCommodityState,
|
|
AccountSyncState,
|
|
AccountWorkItem,
|
|
DouyuConfig,
|
|
)
|
|
from web.backend.services.account_sync_runner import (
|
|
WORK_STOREHOME_SYNC,
|
|
execute_account_work_item,
|
|
run_account_sync_queue,
|
|
)
|
|
from web.backend.services.account_work_queue import (
|
|
claim_account_work,
|
|
enqueue_account_work,
|
|
recover_running_account_work,
|
|
)
|
|
from web.backend.services.douyu_read_gateway import StorehomeSnapshot
|
|
|
|
|
|
def _config(db) -> None:
|
|
db.add(
|
|
DouyuConfig(
|
|
manual_id="manual",
|
|
rid="room",
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
|
|
def _account(name: str = "sync-account") -> Account:
|
|
return Account(
|
|
username=name,
|
|
password="p",
|
|
email=f"{name}@example.com",
|
|
email_password="ep",
|
|
cookie="acf_uid=100",
|
|
cookie_updated_at=datetime.now(UTC),
|
|
ck_check_status="valid",
|
|
status="active",
|
|
auto_managed=True,
|
|
)
|
|
|
|
|
|
def _snapshot() -> StorehomeSnapshot:
|
|
items = (
|
|
{
|
|
"commodityId": "limited",
|
|
"commodityName": "限兑商品",
|
|
"status": 1,
|
|
},
|
|
{
|
|
"commodityId": "available",
|
|
"commodityName": "可兑商品",
|
|
"status": 0,
|
|
"startTime": str(int((datetime.now(UTC) - timedelta(hours=1)).timestamp())),
|
|
"endTime": str(int((datetime.now(UTC) + timedelta(hours=1)).timestamp())),
|
|
"timeLimited": 1,
|
|
},
|
|
)
|
|
return StorehomeSnapshot(
|
|
manual_id="manual",
|
|
rid="room",
|
|
observed_at=datetime.now(UTC),
|
|
items=items,
|
|
limited_ids=frozenset({"limited"}),
|
|
raw={"data": {"list": list(items)}},
|
|
)
|
|
|
|
|
|
def test_execute_work_item_syncs_all_goods_and_completes(db):
|
|
_config(db)
|
|
account = _account()
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claimed = claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
assert [item.id for item in claimed] == [work.id]
|
|
assert claimed[0].attempt_count == 1
|
|
db.commit()
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work_row = verify.get(AccountWorkItem, work.id)
|
|
states = (
|
|
verify.query(AccountCommodityState)
|
|
.filter(AccountCommodityState.account_id == account.id)
|
|
.all()
|
|
)
|
|
sync_state = (
|
|
verify.query(AccountSyncState)
|
|
.filter_by(account_id=account.id, domain="storehome")
|
|
.one()
|
|
)
|
|
assert result["ok"] is True
|
|
assert result["item_count"] == 2
|
|
assert work_row.status == "success"
|
|
assert len(states) == 2
|
|
assert sync_state.status == "success"
|
|
|
|
|
|
def test_work_item_failure_returns_to_pending_for_retry(db):
|
|
_config(db)
|
|
account = _account("sync-retry")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claimed = claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
assert claimed[0].attempt_count == 1
|
|
db.commit()
|
|
|
|
def fail(*_args, **_kwargs):
|
|
raise RuntimeError("network unavailable")
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=fail,
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work_row = verify.get(AccountWorkItem, work.id)
|
|
assert result["ok"] is False
|
|
assert work_row.status == "pending"
|
|
assert work_row.attempt_count == 1
|
|
assert "network unavailable" in work_row.last_error
|
|
assert work_row.due_at > datetime.now(UTC)
|
|
|
|
|
|
def test_run_account_sync_queue_claims_and_executes(db):
|
|
_config(db)
|
|
account = _account("sync-runner")
|
|
db.add(account)
|
|
db.commit()
|
|
enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
|
|
result = run_account_sync_queue(
|
|
db,
|
|
batch_size=5,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work = (
|
|
verify.query(AccountWorkItem)
|
|
.filter_by(account_id=account.id, work_type=WORK_STOREHOME_SYNC)
|
|
.one()
|
|
)
|
|
assert result["scanned"] == 1
|
|
assert result["success"] == 1
|
|
assert work.status == "success"
|
|
|
|
|
|
def test_restart_recovers_running_work(db):
|
|
account = _account("sync-recovery")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
|
|
recovered = recover_running_account_work(db)
|
|
|
|
with make_test_session() as verify:
|
|
row = verify.get(AccountWorkItem, work.id)
|
|
assert recovered == 1
|
|
assert row.status == "pending"
|
|
assert row.due_at <= datetime.now(UTC)
|
|
assert "服务重启" in row.last_error
|