From 087915039964ca0b627f69dab07789392cff5531 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 26 Nov 2018 11:20:53 -0500 Subject: [PATCH 1/3] Move `tractor_test` to new module --- tests/conftest.py | 19 +------------------ tests/test_multi_program.py | 25 ++++++++++++++----------- tractor/_tractor_test.py | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 tractor/_tractor_test.py diff --git a/tests/conftest.py b/tests/conftest.py index 5226305..a84205f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,10 +2,10 @@ ``tractor`` testing!! """ import random -from functools import partial, wraps import pytest import tractor +from tractor._tractor_test import tractor_test pytest_plugins = ['pytester'] @@ -28,20 +28,3 @@ def loglevel(request): @pytest.fixture(scope='session') def arb_addr(): return _arb_addr - - -def tractor_test(fn): - """ - Use: - - @tractor_test - async def test_whatever(): - await ... - """ - @wraps(fn) - def wrapper(*args, **kwargs): - # __tracebackhide__ = True - return tractor.run( - partial(fn, *args, **kwargs), arbiter_addr=_arb_addr) - - return wrapper diff --git a/tests/test_multi_program.py b/tests/test_multi_program.py index 1a71d6b..53413b7 100644 --- a/tests/test_multi_program.py +++ b/tests/test_multi_program.py @@ -8,8 +8,6 @@ import subprocess import pytest import tractor - - from conftest import tractor_test @@ -72,14 +70,19 @@ async def test_cancel_remote_arbiter(daemon, arb_addr): pass -@tractor_test -async def test_register_duplicate_name(daemon): - assert not tractor.current_actor().is_arbiter - async with tractor.open_nursery() as n: - p1 = await n.start_actor('doggy') - p2 = await n.start_actor('doggy') +async def test_register_duplicate_name(daemon, arb_addr): - async with tractor.wait_for_actor('doggy') as portal: - assert portal.channel.uid in (p2.channel.uid, p1.channel.uid) + async def main(): + assert not tractor.current_actor().is_arbiter + async with tractor.open_nursery() as n: + p1 = await n.start_actor('doggy') + p2 = await n.start_actor('doggy') - await n.cancel() + async with tractor.wait_for_actor('doggy') as portal: + assert portal.channel.uid in (p2.channel.uid, p1.channel.uid) + + await n.cancel() + + # run it manually since we want to start **after** + # the other "daemon" program + tractor.run(main, arb_addr=arbiter_addr) diff --git a/tractor/_tractor_test.py b/tractor/_tractor_test.py new file mode 100644 index 0000000..077570a --- /dev/null +++ b/tractor/_tractor_test.py @@ -0,0 +1,26 @@ +import inspect +from functools import partial, wraps + +from . import run + + +def tractor_test(fn): + """ + Use: + + @tractor_test + async def test_whatever(): + await ... + + If an ``arb_addr`` (a socket addr tuple) is defined in the + `pytest` fixture space it will be automatically injected. + """ + @wraps(fn) + def wrapper(*args, arb_addr=None, **kwargs): + # __tracebackhide__ = True + if 'arb_addr' in inspect.signature(fn).parameters: + kwargs['arb_addr'] = arb_addr + return run( + partial(fn, *args, **kwargs), arbiter_addr=arb_addr) + + return wrapper From 512a2f25a241a901217ff3307e8228354b41be55 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 26 Nov 2018 11:26:04 -0500 Subject: [PATCH 2/3] Expose `tractor_test` in the same way as `trio` --- tests/conftest.py | 2 +- tractor/testing/__init__.py | 1 + tractor/{ => testing}/_tractor_test.py | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tractor/testing/__init__.py rename tractor/{ => testing}/_tractor_test.py (92%) diff --git a/tests/conftest.py b/tests/conftest.py index a84205f..97dccd7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ import random import pytest import tractor -from tractor._tractor_test import tractor_test +from tractor.testing import tractor_test pytest_plugins = ['pytester'] diff --git a/tractor/testing/__init__.py b/tractor/testing/__init__.py new file mode 100644 index 0000000..8bcfa90 --- /dev/null +++ b/tractor/testing/__init__.py @@ -0,0 +1 @@ +from ._tractor_test import tractor_test diff --git a/tractor/_tractor_test.py b/tractor/testing/_tractor_test.py similarity index 92% rename from tractor/_tractor_test.py rename to tractor/testing/_tractor_test.py index 077570a..05e0b24 100644 --- a/tractor/_tractor_test.py +++ b/tractor/testing/_tractor_test.py @@ -1,7 +1,10 @@ import inspect from functools import partial, wraps -from . import run +from .. import run + + +__all__ = ['tractor_test'] def tractor_test(fn): From 321c096496f35977e34b5e6eeb01c56b87b1152b Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 26 Nov 2018 14:16:29 -0500 Subject: [PATCH 3/3] Add testing sub-pkg --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 3f464b0..9103ab7 100755 --- a/setup.py +++ b/setup.py @@ -35,6 +35,7 @@ setup( platforms=['linux'], packages=[ 'tractor', + 'tractor.testing', ], install_requires=['msgpack', 'trio>0.8', 'async_generator', 'colorlog'], tests_require=['pytest'],