concurrency.py
This file
Path
tests/concurrency.py
77 lines · 2.1 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 1–7why, one step back1"""2This module contains concurrency utilities that are only used in tests, thus not3required as part of the ConcurrencyBackend API.4"""56import asyncio7import functoolsFix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 8why, one step back8import typingParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 9why, one step back9Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 10–11why, one step back10import trio11Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 12why, one step back12from httpx.concurrency.asyncio import AsyncioBackendRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 13why, one step back13from httpx.concurrency.auto import AutoBackendFix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 14why, one step back14from httpx.concurrency.trio import TrioBackendParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 15–21why, one step back151617@functools.singledispatch18async def sleep(backend, seconds: int):19 raise NotImplementedError # pragma: no cover2021Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 22–26why, one step back22@sleep.register(AutoBackend)23async def _sleep_auto(backend, seconds: int):24 return await sleep(backend.backend, seconds=seconds)2526Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 27–29why, one step back27@sleep.register(AsyncioBackend)28async def _sleep_asyncio(backend, seconds: int):29 await asyncio.sleep(seconds)Add trio concurrency backend (#276)Florimond Mancabefore adoption08355c6
lines 30–31why, one step back3031Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 32–34why, one step back32@sleep.register(TrioBackend)33async def _sleep_trio(backend, seconds: int):34 await trio.sleep(seconds)Add trio concurrency backend (#276)Florimond Mancabefore adoption08355c6
line 35why, one step back35Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 36–41why, one step back3637@functools.singledispatch38async def run_concurrently(backend, *coroutines: typing.Callable[[], typing.Awaitable]):39 raise NotImplementedError # pragma: no cover4041Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 42–46why, one step back42@run_concurrently.register(AutoBackend)43async def _run_concurrently_auto(backend, *coroutines):44 await run_concurrently(backend.backend, *coroutines)4546Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 47–57why, one step back47@run_concurrently.register(AsyncioBackend)48async def _run_concurrently_asyncio(backend, *coroutines):49 coros = (coroutine() for coroutine in coroutines)50 await asyncio.gather(*coros)515253@run_concurrently.register(TrioBackend)54async def _run_concurrently_trio(backend, *coroutines):55 async with trio.open_nursery() as nursery:56 for coroutine in coroutines:57 nursery.start_soon(coroutine)Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 58–77why, one step back585960@functools.singledispatch61def get_cipher(backend, stream):62 raise NotImplementedError # pragma: no cover636465@get_cipher.register(AutoBackend)66def _get_cipher_auto(backend, stream):67 return get_cipher(backend.backend, stream)686970@get_cipher.register(AsyncioBackend)71def _get_cipher_asyncio(backend, stream):72 return stream.stream_writer.get_extra_info("cipher", default=None)737475@get_cipher.register(TrioBackend)76def get_trio_cipher(backend, stream):77 return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None