A response body that counts how often it is closed. It holds more than a test reads.
| 186 | |
| 187 | |
| 188 | class Body(httpx2.SyncByteStream, httpx2.AsyncByteStream): |
| 189 | """A response body that counts how often it is closed. It holds more than a test reads.""" |
| 190 | |
| 191 | def __init__(self) -> None: |
| 192 | self.chunks = [EVENT] * 100 |
| 193 | self.closes = 0 |
| 194 | |
| 195 | @override |
| 196 | def __iter__(self) -> Iterator[bytes]: |
| 197 | yield from self.chunks |
| 198 | |
| 199 | @override |
| 200 | async def __aiter__(self) -> AsyncIterator[bytes]: |
| 201 | for chunk in self.chunks: |
| 202 | yield chunk |
| 203 | |
| 204 | @override |
| 205 | def close(self) -> None: |
| 206 | self.closes += 1 |
| 207 | |
| 208 | @override |
| 209 | async def aclose(self) -> None: |
| 210 | self.close() |
| 211 | |
| 212 | def transport(self) -> httpx2.MockTransport: |
| 213 | return httpx2.MockTransport(lambda _: httpx2.Response(200, stream=self)) |
| 214 | |
| 215 | async def wait_closed(self) -> None: |
| 216 | """An event loop closes what was dropped once it gets to run, and one generator after the other.""" |
| 217 | with anyio.fail_after(10): |
| 218 | while not self.closes: |
| 219 | await anyio.sleep(0) |
| 220 | |
| 221 | |
| 222 | Request = Callable[..., Stream[object]] |
no outgoing calls