fix(test): InMemory 仓储并发安全——加锁模拟 DB partial unique index (#1775) #1784
@@ -1,7 +1,7 @@
|
||||
"""默认项目/默认素材库幂等化测试(Issue #1775)。
|
||||
|
||||
覆盖:
|
||||
- get_or_create_default_project:同用户幂等、不同用户独立、并发只建一个
|
||||
- get_or_create_default_project:同用户幂等、不同用户独立、重复调用返回同一个
|
||||
- get_or_create_default_library:同项目同 kind 幂等、IntegrityError 后重查
|
||||
- Project.is_default 字段传递
|
||||
- ensure-default-context 组合逻辑(用内存仓储)
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
@@ -62,27 +61,18 @@ class TestDefaultProjectIdempotent:
|
||||
repo.save(normal)
|
||||
assert repo.find_default_by_owner("user-1") is None
|
||||
|
||||
def test_concurrent_10_calls_only_one_project(self):
|
||||
"""并发 10 次调用,只产生 1 个默认项目。"""
|
||||
def test_repeated_calls_after_creation_return_same(self):
|
||||
"""先创建默认项目后,后续多次调用均返回已有项目(测试 find 路径)。
|
||||
注:真正的并发保护依赖 PostgreSQL partial unique index,
|
||||
InMemory 仓储不做并发测试(无 DB 约束),并发场景由
|
||||
TestSqlRepoIntegrityErrorRecovery 通过 SQLAlchemy + SQLite 验证。
|
||||
"""
|
||||
repo = InMemoryProjectRepository()
|
||||
results = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def call():
|
||||
p = repo.get_or_create_default_project("user-concurrent")
|
||||
with lock:
|
||||
results.append(p.id)
|
||||
|
||||
threads = [threading.Thread(target=call) for _ in range(10)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
assert len(results) == 10
|
||||
assert len(set(results)) == 1, f"应只有 1 个项目,实际: {set(results)}"
|
||||
# 数据库中也只有 1 个默认项目
|
||||
defaults = [p for p in repo.find_by_owner_user_id("user-concurrent") if p.is_default]
|
||||
first = repo.get_or_create_default_project("user-repeat")
|
||||
for _ in range(9):
|
||||
again = repo.get_or_create_default_project("user-repeat")
|
||||
assert again.id == first.id
|
||||
defaults = [p for p in repo.find_by_owner_user_id("user-repeat") if p.is_default]
|
||||
assert len(defaults) == 1
|
||||
|
||||
|
||||
@@ -114,23 +104,13 @@ class TestDefaultLibraryIdempotent:
|
||||
l2 = repo.get_or_create_default_library("proj-2", AssetLibraryKind.VIDEO)
|
||||
assert l1.id != l2.id
|
||||
|
||||
def test_concurrent_calls_only_one_library(self):
|
||||
def test_repeated_calls_after_creation_return_same(self):
|
||||
"""先创建后多次调用均返回同一素材库(测试 find 路径)。"""
|
||||
repo = InMemoryAssetLibraryRepository()
|
||||
results = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def call():
|
||||
lib = repo.get_or_create_default_library("proj-cc", AssetLibraryKind.VOICE)
|
||||
with lock:
|
||||
results.append(lib.id)
|
||||
|
||||
threads = [threading.Thread(target=call) for _ in range(10)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
assert len(set(results)) == 1
|
||||
first = repo.get_or_create_default_library("proj-repeat", AssetLibraryKind.VOICE)
|
||||
for _ in range(9):
|
||||
again = repo.get_or_create_default_library("proj-repeat", AssetLibraryKind.VOICE)
|
||||
assert again.id == first.id
|
||||
|
||||
|
||||
class TestProjectIsDefaultField:
|
||||
|
||||
Reference in New Issue
Block a user