feat(automation): 按成功率灰度扩缩自动重登
This commit is contained in:
@@ -23,6 +23,7 @@ from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AutomationJob,
|
||||
LoginAttempt,
|
||||
LoginQuotaSetting,
|
||||
LoginTask,
|
||||
User,
|
||||
@@ -230,6 +231,100 @@ class TestFailureCircuit(_Base):
|
||||
assert paused is False
|
||||
|
||||
|
||||
class TestAdaptiveBatch(_Base):
|
||||
def _attempts(
|
||||
self,
|
||||
account: Account,
|
||||
*,
|
||||
success: int,
|
||||
failed: int,
|
||||
failure_kind: str = "network",
|
||||
):
|
||||
now = datetime.now(UTC)
|
||||
rows = [
|
||||
LoginAttempt(
|
||||
account_id=account.id,
|
||||
category="relogin_ordinary",
|
||||
trigger_source="automation",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="success",
|
||||
quota_day=now.date(),
|
||||
)
|
||||
for _ in range(success)
|
||||
]
|
||||
rows.extend(
|
||||
LoginAttempt(
|
||||
account_id=account.id,
|
||||
category="relogin_ordinary",
|
||||
trigger_source="automation",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="failed",
|
||||
failure_kind=failure_kind,
|
||||
quota_day=now.date(),
|
||||
)
|
||||
for _ in range(failed)
|
||||
)
|
||||
self.session.add_all(rows)
|
||||
self.session.commit()
|
||||
|
||||
def test_cold_start_uses_initial_batch_not_configured_ceiling(self):
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.batch_size == 10
|
||||
assert decision.action == "cold_start"
|
||||
|
||||
def test_high_recent_success_grows_by_at_most_twenty_five_percent(self):
|
||||
account = self.make_account("adaptive-success")
|
||||
self._attempts(account, success=9, failed=1)
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {"adaptive_batch_size": 10}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.success_rate == 90.0
|
||||
assert decision.batch_size == 13
|
||||
assert decision.action == "grow"
|
||||
assert job.config["adaptive_batch_size"] == 13
|
||||
|
||||
def test_low_recent_success_halves_batch(self):
|
||||
account = self.make_account("adaptive-shrink")
|
||||
self._attempts(account, success=2, failed=8)
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {"adaptive_batch_size": 20}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.success_rate == 20.0
|
||||
assert decision.batch_size == 10
|
||||
assert decision.action == "shrink"
|
||||
|
||||
|
||||
class TestBackoff(_Base):
|
||||
def test_ladder_is_escalating(self):
|
||||
now = datetime(2026, 10, 2, 12, 0, tzinfo=UTC)
|
||||
|
||||
Reference in New Issue
Block a user