Skip to content
Myr

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 file

Take it

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 1–7why, one step back
1"""
2This module contains concurrency utilities that are only used in tests, thus not3required as part of the ConcurrencyBackend API.4"""56import asyncio7import functools

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

line 8why, one step back
8import typing

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 9why, one step back
9

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

lines 10–11why, one step back
10import trio
11

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 12why, one step back
12from httpx.concurrency.asyncio import AsyncioBackend

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 13why, one step back
13from httpx.concurrency.auto import AutoBackend

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

line 14why, one step back
14from httpx.concurrency.trio import TrioBackend

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 15–21why, one step back
15
1617@functools.singledispatch18async def sleep(backend, seconds: int):19 raise NotImplementedError # pragma: no cover2021

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 22–26why, one step back
22@sleep.register(AutoBackend)
23async def _sleep_auto(backend, seconds: int):24 return await sleep(backend.backend, seconds=seconds)2526

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 27–29why, one step back
27@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 back
30
31

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

lines 32–34why, one step back
32@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 back
35

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

lines 36–41why, one step back
36
37@functools.singledispatch38async def run_concurrently(backend, *coroutines: typing.Callable[[], typing.Awaitable]):39 raise NotImplementedError # pragma: no cover4041

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 42–46why, one step back
42@run_concurrently.register(AutoBackend)
43async def _run_concurrently_auto(backend, *coroutines):44 await run_concurrently(backend.backend, *coroutines)4546

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

lines 47–57why, one step back
47@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 back
58
5960@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