perf(douyu): 限制任务并发并释放长事务连接
This commit is contained in:
@@ -196,6 +196,25 @@ class TestDouyuCollabOrders:
|
||||
assert result["goods"][0]["commodity_id"] == "gift-a"
|
||||
DouyuCollabAccountLookupOut.model_validate(result)
|
||||
|
||||
def test_create_order_rejects_missing_owner_before_insert(self):
|
||||
with pytest.raises(collab.CollabError) as exc_info:
|
||||
collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-MISSING-OWNER",
|
||||
owner_id=999999,
|
||||
client=FakeClient(),
|
||||
)
|
||||
|
||||
assert exc_info.value.code == "owner_invalid"
|
||||
assert exc_info.value.status_code == 400
|
||||
assert (
|
||||
self.session.query(collab.DouyuExternalOrder)
|
||||
.filter_by(order_no="DY-MISSING-OWNER")
|
||||
.count()
|
||||
== 0
|
||||
)
|
||||
|
||||
def test_unlink_account_clears_snapshots_and_keeps_platform_lease(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy import text
|
||||
|
||||
from tests.support.db import make_test_session
|
||||
@@ -9,7 +12,8 @@ from web.backend.database import engine as app_engine
|
||||
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.services import business_events, douyu_service
|
||||
from web.backend.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import business_events, douyu_concurrency, douyu_service
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
@@ -193,6 +197,44 @@ def test_active_batch_budget_returns_429(monkeypatch):
|
||||
assert exc_info.value.status_code == 429
|
||||
|
||||
|
||||
def test_douyu_batch_request_rejects_more_than_five_workers():
|
||||
with pytest.raises(ValidationError):
|
||||
DouyuTaskBatchRequest(
|
||||
account_ids=[1],
|
||||
task_type="query_points",
|
||||
handbook_scope="elite",
|
||||
concurrency=6,
|
||||
)
|
||||
|
||||
|
||||
def test_batch_task_concurrency_is_capped_at_five():
|
||||
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 5
|
||||
|
||||
|
||||
def test_overview_limiter_blocks_extra_workers(monkeypatch):
|
||||
slots = threading.BoundedSemaphore(1)
|
||||
monkeypatch.setattr(douyu_concurrency, "_overview_slots", slots)
|
||||
acquired: list[bool] = []
|
||||
started = threading.Event()
|
||||
|
||||
assert douyu_concurrency.acquire_overview_slot(lambda: False) is True
|
||||
|
||||
def worker():
|
||||
started.set()
|
||||
acquired.append(douyu_concurrency.acquire_overview_slot(lambda: False))
|
||||
|
||||
thread = threading.Thread(target=worker)
|
||||
thread.start()
|
||||
started.wait(timeout=1)
|
||||
time.sleep(0.05)
|
||||
assert acquired == []
|
||||
|
||||
douyu_concurrency.release_overview_slot()
|
||||
thread.join(timeout=1)
|
||||
assert acquired == [True]
|
||||
douyu_concurrency.release_overview_slot()
|
||||
|
||||
|
||||
def test_slim_task_result_removes_debug_payloads():
|
||||
result = slim_douyu_task_result_for_storage(
|
||||
{
|
||||
|
||||
@@ -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 = "20261119_0099"
|
||||
HEAD_REVISION = "20261120_0100"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from starlette.requests import ClientDisconnect
|
||||
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
@@ -14,7 +16,10 @@ from web.backend.models import (
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from web.backend.routers.platform_fulfillment_integration import require_integration_key
|
||||
from web.backend.routers.platform_fulfillment_integration import (
|
||||
require_integration_key,
|
||||
sync_order_event,
|
||||
)
|
||||
from web.backend.services.platform_fulfillment import service
|
||||
|
||||
|
||||
@@ -31,6 +36,32 @@ def test_integration_key_is_required(monkeypatch):
|
||||
assert invalid.value.status_code == 401
|
||||
|
||||
|
||||
def test_webhook_client_disconnect_is_not_an_application_error(monkeypatch):
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_INTEGRATION_KEY", "secret")
|
||||
|
||||
class DisconnectingRequest:
|
||||
async def body(self):
|
||||
raise ClientDisconnect()
|
||||
|
||||
result = asyncio.run(
|
||||
sync_order_event(
|
||||
request=DisconnectingRequest(),
|
||||
db=object(),
|
||||
x_integration_key="secret",
|
||||
x_integration_event_id="",
|
||||
x_integration_timestamp="",
|
||||
x_integration_signature="",
|
||||
)
|
||||
)
|
||||
|
||||
assert result == {
|
||||
"fetched": 0,
|
||||
"created": 0,
|
||||
"updated": 0,
|
||||
"disconnected": True,
|
||||
}
|
||||
|
||||
|
||||
def test_customer_binding_out_hides_internal_account_data(db):
|
||||
user = User(
|
||||
username="integration-actor",
|
||||
|
||||
Reference in New Issue
Block a user