Skip to content

Commit

Permalink
add concurrency test for singleton only
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 committed Nov 17, 2024
1 parent 9a3748b commit 95eb838
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions packages/modern-di/tests_core/providers/test_singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def some_factory(_: SimpleCreator) -> None: ...


@pytest.mark.repeat(10)
async def test_singleton_async_resolve_race_condition() -> None:
async def test_singleton_async_resolve_concurrency() -> None:
calls: int = 0

async def create_resource() -> typing.AsyncIterator[str]:
Expand All @@ -119,7 +119,37 @@ async def resolve_factory(container: Container) -> SimpleCreator:


@pytest.mark.repeat(10)
def test_resource_sync_resolve_race_condition() -> None:
def test_singleton_sync_resolve_concurrency() -> None:
calls: int = 0
lock = threading.Lock()

def create_singleton() -> str:
nonlocal calls
with lock:
calls += 1
time.sleep(0.01)
return ""

singleton = providers.Singleton(Scope.APP, create_singleton)

def resolve_singleton(container: Container) -> str:
return singleton.sync_resolve(container)

with Container(scope=Scope.APP) as app_container, ThreadPoolExecutor(max_workers=4) as pool:
tasks = [
pool.submit(resolve_singleton, app_container),
pool.submit(resolve_singleton, app_container),
pool.submit(resolve_singleton, app_container),
pool.submit(resolve_singleton, app_container),
]
results = [x.result() for x in as_completed(tasks)]

assert all(x == "" for x in results)
assert calls == 1


@pytest.mark.repeat(10)
def test_singleton_wth_resource_sync_resolve_concurrency() -> None:
calls: int = 0
lock = threading.Lock()

Expand Down

0 comments on commit 95eb838

Please sign in to comment.