2018-07-11 23:25:30 +00:00
|
|
|
"""
|
|
|
|
``tractor`` testing!!
|
|
|
|
"""
|
2018-08-07 18:30:25 +00:00
|
|
|
import random
|
2020-01-23 06:16:10 +00:00
|
|
|
import platform
|
2018-08-07 18:30:25 +00:00
|
|
|
|
2018-07-11 23:25:30 +00:00
|
|
|
import pytest
|
|
|
|
import tractor
|
2020-07-25 16:00:04 +00:00
|
|
|
|
|
|
|
# export for tests
|
|
|
|
from tractor.testing import tractor_test # noqa
|
2018-07-11 23:25:30 +00:00
|
|
|
|
|
|
|
|
2018-09-08 13:44:29 +00:00
|
|
|
pytest_plugins = ['pytester']
|
2018-08-07 18:30:25 +00:00
|
|
|
_arb_addr = '127.0.0.1', random.randint(1000, 9999)
|
|
|
|
|
|
|
|
|
2020-07-25 16:00:04 +00:00
|
|
|
no_windows = pytest.mark.skipif(
|
|
|
|
platform.system() == "Windows",
|
|
|
|
reason="Test is unsupported on windows",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-07-11 23:25:30 +00:00
|
|
|
def pytest_addoption(parser):
|
2020-01-27 02:36:08 +00:00
|
|
|
parser.addoption(
|
|
|
|
"--ll", action="store", dest='loglevel',
|
2020-01-27 03:09:32 +00:00
|
|
|
default=None, help="logging level to set when testing"
|
2020-01-27 02:36:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.addoption(
|
|
|
|
"--spawn-backend", action="store", dest='spawn_backend',
|
2020-07-20 19:18:38 +00:00
|
|
|
default='trio',
|
2020-01-27 02:36:08 +00:00
|
|
|
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 02:36:08 +00:00
|
|
|
if backend == 'mp':
|
|
|
|
tractor._spawn.try_set_start_method('spawn')
|
2020-07-20 19:18:38 +00:00
|
|
|
elif backend == 'trio':
|
2020-01-27 02:36:08 +00:00
|
|
|
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
|
2018-08-07 18:30:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def arb_addr():
|
|
|
|
return _arb_addr
|
2019-03-06 05:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
2020-01-27 03:46:48 +00:00
|
|
|
spawn_backend = metafunc.config.option.spawn_backend
|
2020-01-27 04:16:43 +00:00
|
|
|
if not spawn_backend:
|
|
|
|
# XXX some weird windows bug with `pytest`?
|
|
|
|
spawn_backend = 'mp'
|
2020-07-20 19:18:38 +00:00
|
|
|
assert spawn_backend in ('mp', 'trio')
|
2020-01-23 06:16:10 +00:00
|
|
|
|
2020-01-27 02:36:08 +00:00
|
|
|
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
|
2020-01-27 02:36:08 +00:00
|
|
|
methods.remove('fork')
|
2020-07-20 19:18:38 +00:00
|
|
|
elif spawn_backend == 'trio':
|
|
|
|
methods = ['trio']
|
2020-01-23 06:16:10 +00:00
|
|
|
|
2019-03-09 01:06:16 +00:00
|
|
|
metafunc.parametrize("start_method", methods, scope='module')
|