You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running uvicorn test_endless_sse:app --reload then python test_endless_sse.py works.
Running pytest test_endless_sse.py hangs forever.
Expected behaviour:
pytest test_endless_sse.py should pass
# test_endless_sse.py
import pytest
import httpx
import asyncio
from fastapi import FastAPI
from sse_starlette.sse import EventSourceResponse
app = FastAPI()
@app.get("/endless_sse")
async def endless_sse():
async def event_publisher():
i = 0
try:
while True:
i += 1
yield dict(data=i)
await asyncio.sleep(0.2)
except asyncio.CancelledError as e:
raise e
return EventSourceResponse(event_publisher())
# This test should pass but it hangs forever and prints no output
@pytest.mark.asyncio
async def test_endless_sse():
async with httpx.AsyncClient(app=app, base_url="http://test") as aclient:
async with aclient.stream("GET", "/endless_sse") as response:
async for line in response.aiter_lines():
line_stripped = line.strip()
print("line_stripped: ", line_stripped)
if line_stripped == "data: 10":
break
if __name__ == "__main__":
# Run: uvicorn test_endless_sse:app --reload
# Check that we get output from the app running in uvicorn
async def fetch_endless_sse():
async with httpx.AsyncClient(base_url="http://127.0.0.1:8000") as aclient:
async with aclient.stream("GET", "/endless_sse") as response:
async for line in response.aiter_lines():
line_stripped = line.strip()
print("line_stripped: ", line_stripped)
if line_stripped == "data: 10":
break
asyncio.run(fetch_endless_sse())
If no one takes this until the end of the year, I will.
EDIT on 28 July 2023: I lied.
Reacted by Mostafa Yassin, Patrick Arminio, JeHa Kim, nchernukhina, Jimmy Yeh, Ray Tseng, Kyle Putnam, Yuriy, Yuval Goldberg, Christian Warmuth and 6 more
Reacted by Håvard Gjøby Thom, Jeremiah Lowin, synodriver, T-256 and edpyt
Activity
[-]Testing endless SSE endpoint with AsyncClient hangs forever[/-][+]ASGITransport does not stream responses.[/+]lovelydinosaur commentedon May 6, 2022
You've identified this correctly in your comment here... #1787 (comment)
The issue is that the ASGITransport doesn't stream responses.
I've retitled this issue to be more specific regarding what's needed to resolve this.
aditya-n-invcr commentedon Nov 1, 2022
@tomchristie Is there any update on this one ? Or has it already been fixed ?
florimondmanca commentedon Nov 6, 2022
As mentioned in havardthom's comment, I had a previous PR from a long time ago here which attempted to solve something that would resolve this: #998
It's okay to start off that PR, adjusting anything that needs to be due to changes in HTTPX since then.
Kludex commentedon Nov 6, 2022
If no one takes this until the end of the year, I will.
EDIT on 28 July 2023: I lied.
richardhundt commentedon Jul 28, 2023
I've run into the same issue, and unfortunately @florimondmanca's branch didn't work for me (all requests hang, not just SSE streams). I used that as inspiration though, and am monkey patching the transport in my tests. Here's a gist: https://gist.github.com/richardhundt/17dfccb5c1e253f798999fc2b2417d7e
lovelydinosaur commentedon Oct 11, 2023
My suggestion would be that we document this as a known constraint.
If someone is invested in resolving it, then implementing it as a third party transport would be a good first step.
Stream response body in ASGITransport
Stream response body in ASGITransport
Stream response body in ASGITransport
Stream response body in ASGITransport
galah92 commentedon Aug 28, 2024
I've got the same problem - FastAPI SSE endpoint I'd like to test.
Is there a workaround for this?
Stream response body in ASGITransport
Stream response body in ASGITransport
Stream response body in ASGITransport
Stream response body in ASGITransport