2021-10-17 05:33:54 +00:00
|
|
|
import itertools
|
|
|
|
|
|
2022-12-12 00:46:33 +00:00
|
|
|
import pytest
|
2021-10-17 05:33:54 +00:00
|
|
|
import trio
|
|
|
|
|
import tractor
|
|
|
|
|
from tractor import open_actor_cluster
|
2021-10-24 17:48:36 +00:00
|
|
|
from tractor.trionics import gather_contexts
|
2024-03-12 19:48:20 +00:00
|
|
|
from tractor._testing import tractor_test
|
2021-10-17 05:33:54 +00:00
|
|
|
|
|
|
|
|
MESSAGE = 'tractoring at full speed'
|
|
|
|
|
|
|
|
|
|
|
2022-12-12 00:46:33 +00:00
|
|
|
def test_empty_mngrs_input_raises() -> None:
|
|
|
|
|
async def main():
|
2025-07-24 22:17:50 +00:00
|
|
|
with trio.fail_after(3):
|
2022-12-12 00:46:33 +00:00
|
|
|
async with (
|
|
|
|
|
open_actor_cluster(
|
|
|
|
|
modules=[__name__],
|
|
|
|
|
|
|
|
|
|
# NOTE: ensure we can passthrough runtime opts
|
2025-07-24 22:17:50 +00:00
|
|
|
loglevel='cancel',
|
|
|
|
|
debug_mode=False,
|
2022-12-12 00:46:33 +00:00
|
|
|
|
|
|
|
|
) as portals,
|
|
|
|
|
|
2025-07-24 22:17:50 +00:00
|
|
|
gather_contexts(mngrs=()),
|
2022-12-12 00:46:33 +00:00
|
|
|
):
|
2025-07-24 22:17:50 +00:00
|
|
|
# should fail before this?
|
|
|
|
|
assert portals
|
|
|
|
|
|
2025-07-08 16:51:08 +00:00
|
|
|
# test should fail if we mk it here!
|
|
|
|
|
assert 0, 'Should have raised val-err !?'
|
2022-12-12 00:46:33 +00:00
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
|
|
2021-10-17 05:33:54 +00:00
|
|
|
@tractor.context
|
2022-12-12 00:46:33 +00:00
|
|
|
async def worker(
|
|
|
|
|
ctx: tractor.Context,
|
|
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
2021-10-17 05:33:54 +00:00
|
|
|
await ctx.started()
|
2022-12-12 00:46:33 +00:00
|
|
|
|
|
|
|
|
async with ctx.open_stream(
|
2023-04-13 21:48:57 +00:00
|
|
|
allow_overruns=True,
|
2022-12-12 00:46:33 +00:00
|
|
|
) as stream:
|
|
|
|
|
|
|
|
|
|
# TODO: this with the below assert causes a hang bug?
|
|
|
|
|
# with trio.move_on_after(1):
|
|
|
|
|
|
2021-10-17 05:33:54 +00:00
|
|
|
async for msg in stream:
|
|
|
|
|
# do something with msg
|
|
|
|
|
print(msg)
|
|
|
|
|
assert msg == MESSAGE
|
|
|
|
|
|
2026-03-08 19:05:32 +00:00
|
|
|
# ?TODO, does this ever cause a hang?
|
2022-12-12 00:46:33 +00:00
|
|
|
# assert 0
|
|
|
|
|
|
2021-10-17 05:33:54 +00:00
|
|
|
|
2026-03-13 17:36:47 +00:00
|
|
|
# ?TODO, but needs a fn-scoped tpt_proto fixture..
|
|
|
|
|
# @pytest.mark.no_tpt('uds')
|
2021-10-17 05:33:54 +00:00
|
|
|
@tractor_test
|
2026-03-13 17:36:47 +00:00
|
|
|
async def test_streaming_to_actor_cluster(
|
|
|
|
|
tpt_proto: str,
|
|
|
|
|
):
|
|
|
|
|
'''
|
|
|
|
|
Open an actor "cluster" using the (experimental) `._clustering`
|
|
|
|
|
API and conduct standard inter-task-ctx streaming.
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
if tpt_proto == 'uds':
|
|
|
|
|
pytest.skip(
|
|
|
|
|
f'Test currently fails with tpt-proto={tpt_proto!r}\n'
|
|
|
|
|
)
|
2022-12-12 00:46:33 +00:00
|
|
|
|
2026-03-08 19:05:32 +00:00
|
|
|
with trio.fail_after(6):
|
|
|
|
|
async with (
|
|
|
|
|
open_actor_cluster(modules=[__name__]) as portals,
|
2022-12-12 00:46:33 +00:00
|
|
|
|
2026-03-08 19:05:32 +00:00
|
|
|
gather_contexts(
|
|
|
|
|
mngrs=[p.open_context(worker) for p in portals.values()],
|
|
|
|
|
) as contexts,
|
2022-12-12 00:46:33 +00:00
|
|
|
|
2026-03-08 19:05:32 +00:00
|
|
|
gather_contexts(
|
|
|
|
|
mngrs=[ctx[0].open_stream() for ctx in contexts],
|
|
|
|
|
) as streams,
|
2022-12-12 00:46:33 +00:00
|
|
|
|
2026-03-08 19:05:32 +00:00
|
|
|
):
|
|
|
|
|
with trio.move_on_after(1):
|
|
|
|
|
for stream in itertools.cycle(streams):
|
|
|
|
|
await stream.send(MESSAGE)
|