HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/zict/tests/conftest.py
from __future__ import annotations

import gc
import sys
import threading
from concurrent.futures import ThreadPoolExecutor

import pytest

try:
    import psutil
except ImportError:
    psutil = None  # type: ignore


@pytest.fixture
def check_fd_leaks():
    if sys.platform == "win32" or psutil is None:
        yield
    else:
        proc = psutil.Process()
        before = proc.num_fds()
        yield
        gc.collect()
        assert proc.num_fds() == before


@pytest.fixture
def is_locked():
    """Callable that returns True if the parameter zict mapping has its RLock engaged"""
    with ThreadPoolExecutor(1) as ex:

        def __is_locked(d):
            out = d.lock.acquire(blocking=False)
            if out:
                d.lock.release()
            return not out

        def _is_locked(d):
            return ex.submit(__is_locked, d).result()

        yield _is_locked


@pytest.fixture
def check_thread_leaks():
    active_threads_start = threading.enumerate()

    yield

    bad_threads = [
        thread for thread in threading.enumerate() if thread not in active_threads_start
    ]
    if bad_threads:
        raise RuntimeError(f"Leaked thread(s): {bad_threads}")