Skip to content

Commit

Permalink
Run under anyio
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jul 26, 2021
1 parent 3ef6d76 commit 9f59fba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pytest = "~6.2.4"
pre-commit = "~2.13.0"
pytest-cov = { version = "~2.12.1", extras = ["toml"] }
pytest-sugar = "~0.9.4"
trio = "^0.19.0"

This comment has been minimized.

Copy link
@graingert

graingert Jul 26, 2021

Contributor

Btw you can install anyio[trio] to get both

This comment has been minimized.

Copy link
@adriangb

adriangb Jul 26, 2021

Author Owner

I only want trio as a dev dependency (that's the section it's under, diff makes it hard to tell). I guess I could use anyio[trio] as a dev dependency, but I'm not sure how poetry handles that


[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
30 changes: 14 additions & 16 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import asyncio
from typing import AsyncGenerator, Generator

import pytest

from anydep.container import Container
from anydep.lifespan import AsyncExitStackDependencyLifespan
from anydep.models import Dependant
from anydep.params import Depends


Expand All @@ -25,12 +25,13 @@ def sync_gen() -> Generator[int, None, None]:
yield 1


class Class:
calls: int = 0
counter_holder = {"counter": 0}


class Class:
def __init__(self) -> None:
self.value = 1
Class.calls += 1
counter_holder["counter"] += 1


def sub_dep(
Expand Down Expand Up @@ -58,16 +59,13 @@ def requestor(
return v0.value + v1 + v2 + v4 + v5 + v7.value + v8 + v9.value + v10


async def amain(dep: Dependant, container: Container):
async with AsyncExitStackDependencyLifespan() as lifespan:
return await container.solve(dep, lifespan=lifespan, solved={Depends(Class): Class()})


def test_all():
@pytest.mark.anyio
async def test_all():
counter_holder["counter"] = 0
dep = Depends(call=requestor)
c = Container()
c.wire_dependant(dep, cache={})
r = asyncio.run(amain(dep, c))
assert Class.calls == 1 # basic check for caching
container = Container()
container.wire_dependant(dep, cache={})
async with AsyncExitStackDependencyLifespan() as lifespan:
r = await container.solve(dep, lifespan=lifespan, solved={Depends(Class): Class()})
assert counter_holder["counter"] == 1 # basic check for caching
assert r == 14 # empirical for now
print(r)

0 comments on commit 9f59fba

Please sign in to comment.