| 1 |
# Utilities for testing |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import asyncio |
| 5 |
import gc |
| 6 |
import os |
| 7 |
import socket as stdlib_socket |
| 8 |
import sys |
| 9 |
import warnings |
| 10 |
from contextlib import closing, contextmanager |
| 11 |
from typing import TYPE_CHECKING, TypeVar |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
# See trio/_tests/pytest_plugin.py for the other half of this |
| 16 |
from trio._tests.pytest_plugin import RUN_SLOW |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from collections.abc import Generator, Iterable, Sequence |
| 20 |
|
| 21 |
slow = pytest.mark.skipif(not RUN_SLOW, reason="use --run-slow to run slow tests") |
| 22 |
|
| 23 |
T = TypeVar("T") |
| 24 |
|
| 25 |
try: |
| 26 |
s = stdlib_socket.socket(stdlib_socket.AF_INET6, stdlib_socket.SOCK_STREAM, 0) |
| 27 |
except OSError: # pragma: no cover |
| 28 |
# Some systems don't even support creating an IPv6 socket, let alone |
| 29 |
# binding it. (ex: Linux with 'ipv6.disable=1' in the kernel command line) |
| 30 |
# We don't have any of those in our CI, and there's nothing that gets |
| 31 |
# tested _only_ if can_create_ipv6 = False, so we'll just no-cover this. |
| 32 |
can_create_ipv6 = False |
| 33 |
can_bind_ipv6 = False |
| 34 |
else: |
| 35 |
can_create_ipv6 = True |
| 36 |
with s: |
| 37 |
try: |
| 38 |
s.bind(("::1", 0)) |
| 39 |
except OSError: # pragma: no cover # since support for 3.7 was removed |
| 40 |
can_bind_ipv6 = False |
| 41 |
else: |
| 42 |
can_bind_ipv6 = True |
| 43 |
|
| 44 |
creates_ipv6 = pytest.mark.skipif(not can_create_ipv6, reason="need IPv6") |
| 45 |
binds_ipv6 = pytest.mark.skipif(not can_bind_ipv6, reason="need IPv6") |
| 46 |
|
| 47 |
|
| 48 |
def gc_collect_harder() -> None: |
| 49 |
# In the test suite we sometimes want to call gc.collect() to make sure |
| 50 |
# that any objects with noisy __del__ methods (e.g. unawaited coroutines) |
| 51 |
# get collected before we continue, so their noise doesn't leak into |
| 52 |
# unrelated tests. |
| 53 |
# |
| 54 |
# On PyPy, coroutine objects (for example) can survive at least 1 round of |
| 55 |
# garbage collection, because executing their __del__ method to print the |
| 56 |
# warning can cause them to be resurrected. So we call collect a few times |
| 57 |
# to make sure. |
| 58 |
for _ in range(5): |
| 59 |
gc.collect() |
| 60 |
|
| 61 |
|
| 62 |
# Some of our tests need to leak coroutines, and thus trigger the |
| 63 |
# "RuntimeWarning: coroutine '...' was never awaited" message. This context |
| 64 |
# manager should be used anywhere this happens to hide those messages, because |
| 65 |
# when expected they're clutter. |
| 66 |
@contextmanager |
| 67 |
def ignore_coroutine_never_awaited_warnings() -> Generator[None, None, None]: |
| 68 |
with warnings.catch_warnings(): |
| 69 |
warnings.filterwarnings("ignore", message="coroutine '.*' was never awaited") |
| 70 |
try: |
| 71 |
yield |
| 72 |
finally: |
| 73 |
# Make sure to trigger any coroutine __del__ methods now, before |
| 74 |
# we leave the context manager. |
| 75 |
gc_collect_harder() |
| 76 |
|
| 77 |
|
| 78 |
def _noop(*args: object, **kwargs: object) -> None: |
| 79 |
pass # pragma: no cover |
| 80 |
|
| 81 |
|
| 82 |
@contextmanager |
| 83 |
def restore_unraisablehook() -> Generator[None, None, None]: |
| 84 |
sys.unraisablehook, prev = sys.__unraisablehook__, sys.unraisablehook |
| 85 |
try: |
| 86 |
yield |
| 87 |
finally: |
| 88 |
sys.unraisablehook = prev |
| 89 |
|
| 90 |
|
| 91 |
# Used to check sequences that might have some elements out of order. |
| 92 |
# Example usage: |
| 93 |
# The sequences [1, 2.1, 2.2, 3] and [1, 2.2, 2.1, 3] are both |
| 94 |
# matched by the template [1, {2.1, 2.2}, 3] |
| 95 |
def check_sequence_matches(seq: Sequence[T], template: Iterable[T | set[T]]) -> None: |
| 96 |
i = 0 |
| 97 |
for pattern in template: |
| 98 |
if not isinstance(pattern, set): |
| 99 |
pattern = {pattern} |
| 100 |
got = set(seq[i : i + len(pattern)]) |
| 101 |
assert got == pattern |
| 102 |
i += len(got) |
| 103 |
|
| 104 |
|
| 105 |
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=246350 |
| 106 |
skip_if_fbsd_pipes_broken = pytest.mark.skipif( |
| 107 |
sys.platform != "win32" # prevent mypy from complaining about missing uname |
| 108 |
and hasattr(os, "uname") |
| 109 |
and os.uname().sysname == "FreeBSD" |
| 110 |
and os.uname().release[:4] < "12.2", |
| 111 |
reason="hangs on FreeBSD 12.1 and earlier, due to FreeBSD bug #246350", |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
def create_asyncio_future_in_new_loop() -> asyncio.Future[object]: |
| 116 |
with closing(asyncio.new_event_loop()) as loop: |
| 117 |
return loop.create_future() |