tractor/tests/conftest.py

75 lines
2.1 KiB
Python
Raw Normal View History

2018-07-11 23:25:30 +00:00
"""
``tractor`` testing!!
"""
import random
import platform
2018-07-11 23:25:30 +00:00
import pytest
import tractor
from tractor.testing import tractor_test
2018-07-11 23:25:30 +00:00
pytest_plugins = ['pytester']
_arb_addr = '127.0.0.1', random.randint(1000, 9999)
2018-07-11 23:25:30 +00:00
def pytest_addoption(parser):
parser.addoption(
"--ll", action="store", dest='loglevel',
2020-01-27 03:09:32 +00:00
default=None, help="logging level to set when testing"
)
parser.addoption(
"--spawn-backend", action="store", dest='spawn_backend',
default='trio_run_in_process',
help="Processing spawning backend to use for test run",
)
def pytest_configure(config):
backend = config.option.spawn_backend
2020-01-27 03:09:06 +00:00
2020-01-27 03:09:32 +00:00
if platform.system() == "Windows":
2020-01-27 03:09:06 +00:00
backend = 'mp'
if backend == 'mp':
tractor._spawn.try_set_start_method('spawn')
elif backend == 'trio_run_in_process':
tractor._spawn.try_set_start_method(backend)
2018-07-11 23:25:30 +00:00
@pytest.fixture(scope='session', autouse=True)
def loglevel(request):
2018-07-14 20:09:05 +00:00
orig = tractor.log._default_loglevel
level = tractor.log._default_loglevel = request.config.option.loglevel
2018-07-11 23:25:30 +00:00
yield level
2018-07-14 20:09:05 +00:00
tractor.log._default_loglevel = orig
@pytest.fixture(scope='session')
def arb_addr():
return _arb_addr
2019-03-06 05:36:37 +00:00
def pytest_generate_tests(metafunc):
spawn_backend = metafunc.config.getoption("spawn_backend")
assert spawn_backend in ('mp', 'trio_run_in_process')
if 'start_method' in metafunc.fixturenames:
if spawn_backend == 'mp':
from multiprocessing import get_all_start_methods
methods = get_all_start_methods()
2020-01-27 03:09:32 +00:00
if 'fork' in methods:
# fork not available on windows, so check before
# removing XXX: the fork method is in general
# incompatible with trio's global scheduler state
methods.remove('fork')
elif spawn_backend == 'trio_run_in_process':
if platform.system() == "Windows":
2020-01-27 03:09:32 +00:00
pytest.fail(
"Only `--spawn-backend=mp` is supported on Windows")
methods = ['trio_run_in_process']
2019-03-09 01:06:16 +00:00
metafunc.parametrize("start_method", methods, scope='module')