| 1 | import pytest |
| 2 | import asyncio |
| 3 | |
| 4 | |
| 5 | def import_uvloop(): |
| 6 | try: |
| 7 | import uvloop |
| 8 | except ImportError: |
| 9 | return None |
| 10 | else: |
| 11 | return uvloop |
| 12 | |
| 13 | |
| 14 | @pytest.fixture( |
| 15 | params=( |
| 16 | "default", |
| 17 | "uvloop", |
| 18 | ), |
| 19 | ) |
| 20 | def event_loop_policy(request): |
| 21 | if request.param == "default": |
| 22 | return asyncio.DefaultEventLoopPolicy() |
| 23 | elif request.param == "uvloop": |
| 24 | uvloop = import_uvloop() |
| 25 | if uvloop is None: |
| 26 | pytest.skip("uvloop is not installed") |
| 27 | return uvloop.EventLoopPolicy() |
| 28 | return request.param |
| 29 | |
| 30 | |
| 31 | @pytest.fixture |
| 32 | def is_uvloop(event_loop_policy): |
| 33 | uvloop = import_uvloop() |
| 34 | if uvloop is None: |
| 35 | return False |
| 36 | return isinstance(event_loop_policy, uvloop.EventLoopPolicy) |