Skip to content

Commit ceb4c9b

Browse files
authored
Add asyncsafe and threadsafe injection tests
1 parent 63f1064 commit ceb4c9b

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
ci:
1212
strategy:
1313
matrix:
14-
python-version: ["3.12", "3.13", "3.14"]
14+
python-version: ["3.12", "3.13", "3.14", "3.14t"]
1515

1616
name: Continuous Integration ・ Python ${{ matrix.python-version }}
1717
runs-on: ubuntu-latest

tests/test_inject.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from abc import ABC, abstractmethod
2+
from concurrent.futures import Future, ThreadPoolExecutor
23
from dataclasses import dataclass
34
from typing import Annotated, Any, Optional, TypeVar, Union
45

6+
import anyio
57
import pytest
68

79
from injection import inject, injectable
@@ -294,3 +296,43 @@ def function(a: A): ...
294296

295297
function()
296298
assert module.is_locked
299+
300+
async def test_inject_with_async_singleton(self, module):
301+
class Dependency: ...
302+
303+
@module.singleton
304+
async def dependency_factory() -> Dependency:
305+
await anyio.sleep(0)
306+
return Dependency()
307+
308+
instances = []
309+
310+
@module.inject
311+
async def append_dependency(dependency: Dependency):
312+
instances.append(dependency)
313+
314+
async with anyio.create_task_group() as task_group:
315+
for _ in range(100):
316+
task_group.start_soon(append_dependency)
317+
318+
reference = instances[0]
319+
for instance in instances:
320+
assert instance is reference
321+
322+
def test_inject_with_threadsafe(self, module):
323+
@module.singleton
324+
class Dependency: ...
325+
326+
@module.inject(threadsafe=True)
327+
def get_dependency(dependency: Dependency) -> Dependency:
328+
return dependency
329+
330+
futures: list[Future[Dependency]] = []
331+
332+
with ThreadPoolExecutor() as executor:
333+
for _ in range(100):
334+
futures.append(executor.submit(get_dependency))
335+
336+
reference = futures[0].result()
337+
for future in futures:
338+
assert future.result() is reference

uv.lock

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)