forked from goodboy/tractor
1
0
Fork 0
tractor/tractor/testing/_tractor_test.py

90 lines
2.3 KiB
Python
Raw Normal View History

2018-11-26 16:20:53 +00:00
import inspect
import platform
2018-11-26 16:20:53 +00:00
from functools import partial, wraps
2021-01-09 01:49:38 +00:00
import trio
import tractor
# from tractor import run
__all__ = ['tractor_test']
2018-11-26 16:20:53 +00:00
def tractor_test(fn):
"""
Use:
@tractor_test
async def test_whatever():
await ...
If fixtures:
- ``arb_addr`` (a socket addr tuple where arbiter is listening)
- ``loglevel`` (logging level passed to tractor internals)
- ``start_method`` (subprocess spawning backend)
are defined in the `pytest` fixture space they will be automatically
injected to tests declaring these funcargs.
2018-11-26 16:20:53 +00:00
"""
@wraps(fn)
2019-03-06 05:36:37 +00:00
def wrapper(
*args,
loglevel=None,
arb_addr=None,
start_method=None,
2019-03-06 05:36:37 +00:00
**kwargs
):
2018-11-26 16:20:53 +00:00
# __tracebackhide__ = True
2021-01-09 01:49:38 +00:00
2018-11-26 16:20:53 +00:00
if 'arb_addr' in inspect.signature(fn).parameters:
# injects test suite fixture value to test as well
# as `run()`
2018-11-26 16:20:53 +00:00
kwargs['arb_addr'] = arb_addr
2020-08-13 15:53:45 +00:00
if 'loglevel' in inspect.signature(fn).parameters:
# allows test suites to define a 'loglevel' fixture
# that activates the internal logging
kwargs['loglevel'] = loglevel
if start_method is None:
if platform.system() == "Windows":
start_method = 'spawn'
else:
start_method = 'trio'
2019-03-09 01:06:16 +00:00
if 'start_method' in inspect.signature(fn).parameters:
# set of subprocess spawning backends
2019-03-09 01:06:16 +00:00
kwargs['start_method'] = start_method
2020-08-13 15:53:45 +00:00
2021-01-09 01:49:38 +00:00
if kwargs:
# use explicit root actor start
async def _main():
async with tractor.open_root_actor(
# **kwargs,
arbiter_addr=arb_addr,
loglevel=loglevel,
start_method=start_method,
# TODO: only enable when pytest is passed --pdb
# debug_mode=True,
) as actor:
await fn(*args, **kwargs)
main = _main
else:
# use implicit root actor start
2021-06-14 00:22:26 +00:00
main = partial(fn, *args, **kwargs)
2021-01-09 01:49:38 +00:00
return trio.run(main)
# arbiter_addr=arb_addr,
# loglevel=loglevel,
# start_method=start_method,
# )
2018-11-26 16:20:53 +00:00
return wrapper