182 lines
6.3 KiB
Python
182 lines
6.3 KiB
Python
"""登录批次不能无限占着执行队列的槽位。
|
|
|
|
背景:登录批次在 execution_jobs 队列里占着一个槽。斗鱼邮箱验证码是攒批投递的,
|
|
等一封验证码最长 25 分钟,10 个账号的批次能占着槽位跑近一小时,把同队列的
|
|
斗鱼履约批次饿死(新建斗鱼任务直接报「斗鱼任务繁忙:当前有 N 个批次运行中」)。
|
|
所以批次要有时长上限:超时的账号本轮记「未执行」,留到下一轮。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
import web.backend.database as database_module
|
|
from tests.support.db import get_test_engine, make_test_session
|
|
from web.backend.models import Account, LoginTask, User
|
|
from web.backend.services import login_service
|
|
|
|
|
|
class _FakeLogin:
|
|
"""不会真的发请求的登录器;被调用就说明账号不该被跳过。"""
|
|
|
|
calls = 0
|
|
|
|
def __init__(self, *_args, **_kwargs):
|
|
pass
|
|
|
|
def login(self):
|
|
type(self).calls += 1
|
|
return SimpleNamespace(
|
|
success=True, cookie="new-cookie", message="登录成功", code=None
|
|
)
|
|
|
|
def check_account(self):
|
|
return self.login()
|
|
|
|
|
|
def _setup():
|
|
engine = get_test_engine()
|
|
database_module.SessionLocal.configure(bind=engine)
|
|
session = make_test_session(engine)
|
|
user = User(username="admin", password_hash="hash", role="super_admin")
|
|
session.add(user)
|
|
session.flush()
|
|
account = Account(
|
|
username="用户1160483772",
|
|
password="password",
|
|
email="user@example.com",
|
|
email_password="mail-password",
|
|
)
|
|
session.add(account)
|
|
session.commit()
|
|
return engine, session, user, account
|
|
|
|
|
|
def test_batch_budget_marks_late_account_as_skipped(monkeypatch):
|
|
"""批次已经超时:账号本轮不执行,记「未执行」而不是「失败」。"""
|
|
engine, session, user, account = _setup()
|
|
try:
|
|
task = LoginTask(
|
|
batch_id="batch-slot-budget",
|
|
account_id=account.id,
|
|
status="pending",
|
|
created_by=user.id,
|
|
)
|
|
session.add(task)
|
|
session.commit()
|
|
task_id = task.id
|
|
|
|
monkeypatch.setattr(login_service, "DouyuLogin", _FakeLogin)
|
|
monkeypatch.setattr(login_service, "_batch_max_seconds", lambda: 30.0)
|
|
_FakeLogin.calls = 0
|
|
|
|
runner_db = sessionmaker(bind=engine)()
|
|
runner = login_service.LoginBatchRunner(
|
|
db=runner_db,
|
|
account_ids=[account.id],
|
|
created_by=user.id,
|
|
creator_permissions=["login:view_all"],
|
|
proxy_config=None,
|
|
concurrency=1,
|
|
mode="login",
|
|
)
|
|
# 假装这个批次已经跑了 60 秒,超过 30 秒的批次预算
|
|
runner._batch_started_at = time.monotonic() - 60.0
|
|
runner._execute_one(
|
|
task_id,
|
|
{"username": account.username, "password": "password"},
|
|
1,
|
|
)
|
|
runner_db.close()
|
|
|
|
check = sessionmaker(bind=engine)()
|
|
try:
|
|
row = check.get(LoginTask, task_id)
|
|
assert row is not None
|
|
assert row.attempt_status == "skipped"
|
|
assert "批次已达" in (row.attempt_message or "")
|
|
assert "未执行" in (row.attempt_message or "")
|
|
logs = row.logs or []
|
|
assert logs, "被跳过的账号也要有日志"
|
|
assert "未执行" in logs[-1]["message"]
|
|
finally:
|
|
check.close()
|
|
assert _FakeLogin.calls == 0, "超时的账号不该再发起登录"
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def test_batch_max_seconds_default_and_env(monkeypatch):
|
|
monkeypatch.delenv("LOGIN_BATCH_MAX_SECONDS", raising=False)
|
|
assert login_service._batch_max_seconds() == 1800.0
|
|
monkeypatch.setenv("LOGIN_BATCH_MAX_SECONDS", "600")
|
|
assert login_service._batch_max_seconds() == 600.0
|
|
# 0 = 不限
|
|
monkeypatch.setenv("LOGIN_BATCH_MAX_SECONDS", "0")
|
|
assert login_service._batch_max_seconds() == 0.0
|
|
|
|
|
|
def test_batch_budget_skipped_account_releases_quota_and_avoids_email_quota(monkeypatch):
|
|
"""超时跳过走完整 run() 结算后,不消耗日配额、不记失败、也不触发 email_quota 误判。"""
|
|
from web.backend.models import LoginAttempt, LoginQuotaSetting, LoginQuotaUsage
|
|
from web.backend.services.login_failure import classify_failure
|
|
|
|
engine, session, user, account = _setup()
|
|
try:
|
|
session.add(
|
|
LoginQuotaSetting(
|
|
id=1,
|
|
daily_attempt_limit=10,
|
|
hourly_attempt_limit=10,
|
|
per_account_daily_limit=1,
|
|
onboarding_daily_limit=10,
|
|
enabled=True,
|
|
)
|
|
)
|
|
session.commit()
|
|
|
|
monkeypatch.setattr(login_service, "DouyuLogin", _FakeLogin)
|
|
monkeypatch.setattr(login_service, "_batch_max_seconds", lambda: 0.001)
|
|
_FakeLogin.calls = 0
|
|
|
|
original_submit = login_service.submit
|
|
|
|
def delayed_submit(lane, fn, /, *args, **kwargs):
|
|
time.sleep(0.02)
|
|
return original_submit(lane, fn, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(login_service, "submit", delayed_submit)
|
|
|
|
runner_db = sessionmaker(bind=engine)()
|
|
runner = login_service.LoginBatchRunner(
|
|
db=runner_db,
|
|
account_ids=[account.id],
|
|
created_by=user.id,
|
|
creator_permissions=["login:view_all"],
|
|
proxy_config=None,
|
|
concurrency=1,
|
|
mode="login",
|
|
)
|
|
runner.run()
|
|
|
|
check = sessionmaker(bind=engine)()
|
|
try:
|
|
task = check.query(LoginTask).filter_by(account_id=account.id).one()
|
|
attempt = check.query(LoginAttempt).filter_by(account_id=account.id).one()
|
|
usage_rows = check.query(LoginQuotaUsage).all()
|
|
|
|
assert task.attempt_status == "skipped"
|
|
assert classify_failure(task.attempt_message) != "email_quota"
|
|
assert attempt.status == "skipped"
|
|
assert attempt.failure_kind == ""
|
|
assert all(int(row.attempt_count or 0) == 0 for row in usage_rows)
|
|
assert all(int(row.failure_count or 0) == 0 for row in usage_rows)
|
|
assert _FakeLogin.calls == 0
|
|
finally:
|
|
check.close()
|
|
finally:
|
|
session.close()
|