fix: 优化并发数批量设置接口,引入 c.close() 资源管理并增加对 list_accounts() 返回 None 的拦截保护

This commit is contained in:
liumangmang
2026-07-02 10:02:07 +08:00
parent 608ef00920
commit 274413bca7
2 changed files with 189 additions and 125 deletions
+59
View File
@@ -98,8 +98,17 @@ def test_set_website_accounts_concurrency_workflow(db_session, monkeypatch):
bulk_calls = []
update_calls = []
list_calls_count = 0
closed_count = 0
class MockClient:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
nonlocal closed_count
closed_count += 1
return False
def list_accounts(self):
nonlocal list_calls_count
list_calls_count += 1
@@ -146,6 +155,8 @@ def test_set_website_accounts_concurrency_workflow(db_session, monkeypatch):
assert len(bulk_calls) == 1
assert bulk_calls[0] == (["1001"], {"concurrency": 200})
assert len(update_calls) == 0
# 验证是否调用了 __exit__ 关闭连接
assert closed_count == 1
def test_set_website_accounts_concurrency_fallback(db_session, monkeypatch):
@@ -179,8 +190,17 @@ def test_set_website_accounts_concurrency_fallback(db_session, monkeypatch):
bulk_calls = []
update_calls = []
closed_count = 0
class MockClientError:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
nonlocal closed_count
closed_count += 1
return False
def list_accounts(self):
return [{"id": 1001, "name": "acc-1001", "concurrency": 10}]
@@ -210,3 +230,42 @@ def test_set_website_accounts_concurrency_fallback(db_session, monkeypatch):
assert len(items) == 1
assert items[0].status == "success"
assert "逐个更新" in items[0].message
assert closed_count == 1
def test_set_website_accounts_concurrency_list_accounts_none(db_session, monkeypatch):
w = Website(
id=1,
name="Sub2Api site",
site_type="sub2api",
base_url="http://sub2api",
api_prefix="api/v1",
auth_type="bearer",
auth_config_json='{"token": "tok1"}'
)
db_session.add(w)
db_session.commit()
closed_count = 0
class MockClientNone:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
nonlocal closed_count
closed_count += 1
return False
def list_accounts(self):
return None
monkeypatch.setattr("app.routers.websites._client", lambda row: MockClientNone())
req = SetConcurrencyRequest(concurrency=150)
res = set_website_accounts_concurrency(wid=w.id, body=req, db=db_session)
assert res.success is False
assert "拉取远端账号列表失败" in res.message
assert len(res.items) == 0
assert closed_count == 1