Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Use anyio, so that ASGI support works for both asyncio and Trio #1872

Closed
Zac-HD opened this issue Nov 2, 2023 · 3 comments 路 Fixed by #2052
Closed

[FEATURE] Use anyio, so that ASGI support works for both asyncio and Trio #1872

Zac-HD opened this issue Nov 2, 2023 · 3 comments 路 Fixed by #2052
Assignees
Labels
Core: Transport Sending data to the tested app Type: Compatibility Compatibility with other tools Type: Feature New functionalities or enhancements

Comments

@Zac-HD
Copy link

Zac-HD commented Nov 2, 2023

The support for directly testing ASGI apps is really nice, but at the moment it doesn't support Trio - so I can't use it 馃槩

Fortunately this is pretty easy to fix by using anyio, a compatibility layer which can be run against either asyncio or Trio backends; it's already widely used by e.g. httpx and starlette/fastapi. What that would look like:

def make_async_test(test: Callable) -> Callable:
def async_run(*args: Any, **kwargs: Any) -> None:
loop = asyncio.get_event_loop()
coro = test(*args, **kwargs)
future = asyncio.ensure_future(coro, loop=loop)
loop.run_until_complete(future)
return async_run

 def make_async_test(test: Callable) -> Callable: 
     def async_run(*args: Any, **kwargs: Any) -> None:
        return anyio.run(partial(test, *args, **kwargs))
  
     return async_run 

I think the main effort here would actually be to test something on trio as well as asyncio, which shouldn't be too hard to parametrize.

@Zac-HD Zac-HD added Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Feature New functionalities or enhancements labels Nov 2, 2023
@Stranger6667 Stranger6667 added Type: Compatibility Compatibility with other tools Core: Transport Sending data to the tested app and removed Status: Needs Triage Requires initial assessment to categorize and prioritize labels Nov 6, 2023
@Stranger6667
Copy link
Member

The current blocker is that aiohttp test client depends on its own loop (provided via a fixture) and using anyio.run(partial(test, *args, **kwargs)) breaks it :(

@Zac-HD
Copy link
Author

Zac-HD commented Dec 2, 2023

IIRC aiohttp is asyncio-specific, so you might need to switch that out for httpx. pytest-anyio may also be helpful?

Stranger6667 added a commit that referenced this issue Feb 11, 2024
Ref: #1872

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
Stranger6667 added a commit that referenced this issue Feb 11, 2024
Ref: #1872

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
Stranger6667 added a commit that referenced this issue Feb 11, 2024
Ref: #1872

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
Stranger6667 added a commit that referenced this issue Feb 11, 2024
Ref: #1872

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
Stranger6667 added a commit that referenced this issue Feb 12, 2024
Ref: #1872

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
@Stranger6667
Copy link
Member

In the next release, @pytest.mark.trio and other ways of enabling trio support from pytest-trio will work 馃帀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core: Transport Sending data to the tested app Type: Compatibility Compatibility with other tools Type: Feature New functionalities or enhancements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants