fix: 优化并发数批量设置接口,引入 c.close() 资源管理并增加对 list_accounts() 返回 None 的拦截保护
This commit is contained in:
@@ -1123,8 +1123,7 @@ def set_website_accounts_concurrency(
|
||||
if website.site_type != "sub2api":
|
||||
raise HTTPException(400, "only sub2api site supports account concurrency settings")
|
||||
|
||||
c = _client(website)
|
||||
|
||||
with _client(website) as c:
|
||||
try:
|
||||
remote_accounts = c.list_accounts()
|
||||
except Exception as e:
|
||||
@@ -1134,8 +1133,14 @@ def set_website_accounts_concurrency(
|
||||
items=[]
|
||||
)
|
||||
|
||||
if remote_accounts is None:
|
||||
return SetConcurrencyResponse(
|
||||
success=False,
|
||||
message="拉取远端账号列表失败,无法设置并发",
|
||||
items=[]
|
||||
)
|
||||
|
||||
remote_map = {}
|
||||
if remote_accounts:
|
||||
for acc in remote_accounts:
|
||||
aid = c.extract_id(acc)
|
||||
if aid:
|
||||
@@ -1205,7 +1210,7 @@ def set_website_accounts_concurrency(
|
||||
success = all(item.status == "skipped" for item in items)
|
||||
return SetConcurrencyResponse(
|
||||
success=success,
|
||||
message=f"无可执行批量更新的账号。已跳过 {len(skipped_items)} 个",
|
||||
message=f"无可执行批量更新 of 账号。已跳过 {len(skipped_items)} 个",
|
||||
items=items
|
||||
)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user