perf(douyu): 拆分任务槽并改造绑定扫码调度
This commit is contained in:
@@ -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