2019-01-24 03:35:04 +00:00
|
|
|
import time
|
2019-01-21 17:31:03 +00:00
|
|
|
from itertools import cycle
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
from tractor.testing import tractor_test
|
|
|
|
|
|
|
|
|
2019-01-25 05:13:13 +00:00
|
|
|
def test_type_checks():
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as err:
|
|
|
|
@tractor.msg.pub
|
|
|
|
async def no_get_topics(yo):
|
|
|
|
yield
|
|
|
|
|
|
|
|
assert "must define a `get_topics`" in str(err.value)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as err:
|
|
|
|
@tractor.msg.pub
|
|
|
|
def not_async_gen(yo):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert "must be an async generator function" in str(err.value)
|
|
|
|
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
def is_even(i):
|
|
|
|
return i % 2 == 0
|
|
|
|
|
|
|
|
|
2020-12-23 00:30:51 +00:00
|
|
|
# placeholder for topics getter
|
|
|
|
_get_topics = None
|
|
|
|
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
@tractor.msg.pub
|
2019-02-15 22:04:41 +00:00
|
|
|
async def pubber(get_topics, seed=10):
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2020-12-23 00:30:51 +00:00
|
|
|
# ensure topic subscriptions are as expected
|
|
|
|
global _get_topics
|
|
|
|
_get_topics = get_topics
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2020-12-23 00:30:51 +00:00
|
|
|
for i in cycle(range(seed)):
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
yield {'even' if is_even(i) else 'odd': i}
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
|
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
async def subs(
|
2021-02-24 23:06:59 +00:00
|
|
|
which,
|
|
|
|
pub_actor_name,
|
|
|
|
seed=10,
|
2019-02-15 22:04:41 +00:00
|
|
|
task_status=trio.TASK_STATUS_IGNORED,
|
|
|
|
):
|
2019-01-21 17:31:03 +00:00
|
|
|
if len(which) == 1:
|
|
|
|
if which[0] == 'even':
|
|
|
|
pred = is_even
|
|
|
|
else:
|
2019-01-25 05:13:13 +00:00
|
|
|
def pred(i):
|
|
|
|
return not is_even(i)
|
2019-01-21 17:31:03 +00:00
|
|
|
else:
|
2019-01-25 05:13:13 +00:00
|
|
|
def pred(i):
|
|
|
|
return isinstance(i, int)
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2021-04-28 18:35:25 +00:00
|
|
|
# TODO: https://github.com/goodboy/tractor/issues/207
|
2021-02-24 23:06:59 +00:00
|
|
|
async with tractor.wait_for_actor(pub_actor_name) as portal:
|
|
|
|
assert portal
|
|
|
|
|
2021-04-28 18:35:25 +00:00
|
|
|
async with portal.open_stream_from(
|
|
|
|
pubber,
|
|
|
|
topics=which,
|
|
|
|
seed=seed,
|
|
|
|
) as stream:
|
2021-04-28 15:55:37 +00:00
|
|
|
task_status.started(stream)
|
|
|
|
times = 10
|
|
|
|
count = 0
|
|
|
|
await stream.__anext__()
|
2019-02-16 03:10:55 +00:00
|
|
|
async for pkt in stream:
|
2019-01-21 17:31:03 +00:00
|
|
|
for topic, value in pkt.items():
|
2021-04-28 15:55:37 +00:00
|
|
|
assert pred(value)
|
2019-02-15 22:04:41 +00:00
|
|
|
count += 1
|
|
|
|
if count >= times:
|
|
|
|
break
|
2021-04-28 15:55:37 +00:00
|
|
|
|
2019-02-16 03:10:55 +00:00
|
|
|
await stream.aclose()
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2021-04-28 18:35:25 +00:00
|
|
|
async with portal.open_stream_from(
|
|
|
|
pubber,
|
|
|
|
topics=['odd'],
|
|
|
|
seed=seed,
|
|
|
|
) as stream:
|
2021-04-28 15:55:37 +00:00
|
|
|
await stream.__anext__()
|
|
|
|
count = 0
|
|
|
|
# async with aclosing(stream) as stream:
|
|
|
|
try:
|
|
|
|
async for pkt in stream:
|
|
|
|
for topic, value in pkt.items():
|
|
|
|
pass
|
|
|
|
# assert pred(value)
|
|
|
|
count += 1
|
|
|
|
if count >= times:
|
|
|
|
break
|
|
|
|
finally:
|
|
|
|
await stream.aclose()
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2019-01-25 05:13:13 +00:00
|
|
|
@tractor.msg.pub(tasks=['one', 'two'])
|
|
|
|
async def multilock_pubber(get_topics):
|
|
|
|
yield {'doggy': 10}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'callwith_expecterror',
|
|
|
|
[
|
|
|
|
(pubber, {}, TypeError),
|
|
|
|
# missing a `topics`
|
|
|
|
(multilock_pubber, {'ctx': None}, TypeError),
|
|
|
|
# missing a `task_name`
|
|
|
|
(multilock_pubber, {'ctx': None, 'topics': ['topic1']}, TypeError),
|
|
|
|
# should work
|
|
|
|
(multilock_pubber,
|
2019-02-16 03:10:55 +00:00
|
|
|
{'ctx': None, 'topics': ['doggy'], 'task_name': 'one'},
|
2019-01-25 05:13:13 +00:00
|
|
|
None),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@tractor_test
|
|
|
|
async def test_required_args(callwith_expecterror):
|
|
|
|
func, kwargs, err = callwith_expecterror
|
|
|
|
|
|
|
|
if err is not None:
|
|
|
|
with pytest.raises(err):
|
|
|
|
await func(**kwargs)
|
|
|
|
else:
|
|
|
|
async with tractor.open_nursery() as n:
|
2021-04-28 15:55:37 +00:00
|
|
|
|
|
|
|
portal = await n.start_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
name='pubber',
|
2021-04-28 15:55:37 +00:00
|
|
|
enable_modules=[__name__],
|
2020-12-21 14:09:55 +00:00
|
|
|
)
|
2019-02-16 03:10:55 +00:00
|
|
|
|
|
|
|
async with tractor.wait_for_actor('pubber'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
await trio.sleep(0.5)
|
2019-01-25 05:13:13 +00:00
|
|
|
|
2021-04-28 15:55:37 +00:00
|
|
|
async with portal.open_stream_from(
|
|
|
|
multilock_pubber,
|
|
|
|
**kwargs
|
|
|
|
) as stream:
|
|
|
|
async for val in stream:
|
|
|
|
assert val == {'doggy': 10}
|
|
|
|
|
|
|
|
await portal.cancel_actor()
|
2019-01-25 05:13:13 +00:00
|
|
|
|
|
|
|
|
2019-01-24 03:35:04 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'pub_actor',
|
|
|
|
['streamer', 'arbiter']
|
|
|
|
)
|
2019-02-15 22:04:41 +00:00
|
|
|
def test_multi_actor_subs_arbiter_pub(
|
2019-01-21 17:31:03 +00:00
|
|
|
loglevel,
|
|
|
|
arb_addr,
|
2019-01-24 03:35:04 +00:00
|
|
|
pub_actor,
|
2019-01-21 17:31:03 +00:00
|
|
|
):
|
2019-01-24 03:35:04 +00:00
|
|
|
"""Try out the neato @pub decorator system.
|
|
|
|
"""
|
2020-12-23 00:30:51 +00:00
|
|
|
global _get_topics
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
async def main():
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2021-02-24 23:06:59 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
enable_modules=[__name__],
|
|
|
|
) as n:
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2021-02-25 13:05:35 +00:00
|
|
|
name = 'root'
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
if pub_actor == 'streamer':
|
2019-01-24 03:35:04 +00:00
|
|
|
# start the publisher as a daemon
|
|
|
|
master_portal = await n.start_actor(
|
|
|
|
'streamer',
|
2021-02-24 23:06:59 +00:00
|
|
|
enable_modules=[__name__],
|
2019-01-24 03:35:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
even_portal = await n.run_in_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
subs,
|
|
|
|
which=['even'],
|
|
|
|
name='evens',
|
|
|
|
pub_actor_name=name
|
|
|
|
)
|
2019-01-24 03:35:04 +00:00
|
|
|
odd_portal = await n.run_in_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
subs,
|
|
|
|
which=['odd'],
|
|
|
|
name='odds',
|
|
|
|
pub_actor_name=name
|
|
|
|
)
|
2019-01-24 03:35:04 +00:00
|
|
|
|
|
|
|
async with tractor.wait_for_actor('evens'):
|
|
|
|
# block until 2nd actor is initialized
|
|
|
|
pass
|
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
if pub_actor == 'arbiter':
|
2020-12-23 00:30:51 +00:00
|
|
|
|
2019-01-24 03:35:04 +00:00
|
|
|
# wait for publisher task to be spawned in a local RPC task
|
2020-12-23 00:30:51 +00:00
|
|
|
while _get_topics is None:
|
2019-01-24 03:35:04 +00:00
|
|
|
await trio.sleep(0.1)
|
|
|
|
|
2020-12-23 00:30:51 +00:00
|
|
|
get_topics = _get_topics
|
2019-01-24 03:35:04 +00:00
|
|
|
|
|
|
|
assert 'even' in get_topics()
|
2019-01-21 17:31:03 +00:00
|
|
|
|
|
|
|
async with tractor.wait_for_actor('odds'):
|
|
|
|
# block until 2nd actor is initialized
|
|
|
|
pass
|
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
if pub_actor == 'arbiter':
|
2019-01-24 03:35:04 +00:00
|
|
|
start = time.time()
|
|
|
|
while 'odd' not in get_topics():
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
if time.time() - start > 1:
|
|
|
|
pytest.fail("odds subscription never arrived?")
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
# TODO: how to make this work when the arbiter gets
|
|
|
|
# a portal to itself? Currently this causes a hang
|
|
|
|
# when the channel server is torn down due to a lingering
|
|
|
|
# loopback channel
|
|
|
|
# with trio.move_on_after(1):
|
|
|
|
# await subs(['even', 'odd'])
|
|
|
|
|
|
|
|
# XXX: this would cause infinite
|
|
|
|
# blocking due to actor never terminating loop
|
|
|
|
# await even_portal.result()
|
|
|
|
|
2019-01-24 03:35:04 +00:00
|
|
|
await trio.sleep(0.5)
|
2019-01-21 17:31:03 +00:00
|
|
|
await even_portal.cancel_actor()
|
2020-08-08 19:16:10 +00:00
|
|
|
await trio.sleep(1)
|
2019-01-24 03:35:04 +00:00
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
if pub_actor == 'arbiter':
|
2019-01-24 03:35:04 +00:00
|
|
|
assert 'even' not in get_topics()
|
|
|
|
|
2019-01-21 17:31:03 +00:00
|
|
|
await odd_portal.cancel_actor()
|
2020-08-08 19:16:10 +00:00
|
|
|
await trio.sleep(2)
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2019-02-15 22:04:41 +00:00
|
|
|
if pub_actor == 'arbiter':
|
2019-01-24 03:35:04 +00:00
|
|
|
while get_topics():
|
|
|
|
await trio.sleep(0.1)
|
2020-08-08 19:16:10 +00:00
|
|
|
if time.time() - start > 2:
|
2019-01-24 03:35:04 +00:00
|
|
|
pytest.fail("odds subscription never dropped?")
|
|
|
|
else:
|
|
|
|
await master_portal.cancel_actor()
|
2019-01-21 17:31:03 +00:00
|
|
|
|
2021-02-24 23:06:59 +00:00
|
|
|
trio.run(main)
|
2019-02-15 22:04:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_single_subactor_pub_multitask_subs(
|
|
|
|
loglevel,
|
|
|
|
arb_addr,
|
|
|
|
):
|
|
|
|
async def main():
|
|
|
|
|
2021-02-24 23:06:59 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
enable_modules=[__name__],
|
|
|
|
) as n:
|
2019-02-15 22:04:41 +00:00
|
|
|
|
|
|
|
portal = await n.start_actor(
|
|
|
|
'streamer',
|
2021-02-24 23:06:59 +00:00
|
|
|
enable_modules=[__name__],
|
2019-02-15 22:04:41 +00:00
|
|
|
)
|
|
|
|
async with tractor.wait_for_actor('streamer'):
|
|
|
|
# block until 2nd actor is initialized
|
|
|
|
pass
|
|
|
|
|
|
|
|
async with trio.open_nursery() as tn:
|
|
|
|
agen = await tn.start(subs, ['even'], 'streamer')
|
|
|
|
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
tn.start_soon(subs, ['even'], 'streamer')
|
|
|
|
|
|
|
|
# XXX this will trigger the python bug:
|
|
|
|
# https://bugs.python.org/issue32526
|
2019-02-16 03:10:55 +00:00
|
|
|
# if using async generators to wrap tractor channels
|
|
|
|
await agen.aclose()
|
2019-02-15 22:04:41 +00:00
|
|
|
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
tn.start_soon(subs, ['even'], 'streamer')
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
tn.start_soon(subs, ['even'], 'streamer')
|
|
|
|
|
|
|
|
await portal.cancel_actor()
|
|
|
|
|
2021-02-24 23:06:59 +00:00
|
|
|
trio.run(main)
|