tractor/tests/test_local.py

73 lines
1.9 KiB
Python
Raw Normal View History

"""
Arbiter and "local" actor api
"""
2018-06-12 19:23:58 +00:00
import time
import pytest
2018-06-12 19:23:58 +00:00
import trio
import tractor
from conftest import tractor_test
2018-06-12 19:23:58 +00:00
@pytest.mark.trio
async def test_no_runtime():
2018-06-12 19:23:58 +00:00
"""An arbitter must be established before any nurseries
can be created.
2021-02-24 19:59:48 +00:00
(In other words ``tractor.open_root_actor()`` must be engaged at
some point?)
2018-06-12 19:23:58 +00:00
"""
with pytest.raises(RuntimeError) :
async with tractor.find_actor('doggy'):
2018-06-12 19:23:58 +00:00
pass
2018-09-21 04:32:23 +00:00
@tractor_test
async def test_self_is_registered(arb_addr):
2018-09-21 04:32:23 +00:00
"Verify waiting on the arbiter to register itself using the standard api."
actor = tractor.current_actor()
assert actor.is_arbiter
2021-01-09 01:40:49 +00:00
with trio.fail_after(0.2):
async with tractor.wait_for_actor('root') as portal:
assert portal.channel.uid[0] == 'root'
2018-09-21 04:32:23 +00:00
@tractor_test
async def test_self_is_registered_localportal(arb_addr):
"Verify waiting on the arbiter to register itself using a local portal."
actor = tractor.current_actor()
assert actor.is_arbiter
async with tractor.get_arbiter(*arb_addr) as portal:
assert isinstance(portal, tractor._portal.LocalPortal)
2021-01-09 01:40:49 +00:00
with trio.fail_after(0.2):
2021-02-24 19:59:48 +00:00
sockaddr = await portal.run_from_ns(
'self', 'wait_for_actor', name='root')
2021-01-09 01:40:49 +00:00
assert sockaddr[0] == arb_addr
2018-09-21 04:32:23 +00:00
def test_local_actor_async_func(arb_addr):
2018-06-12 19:23:58 +00:00
"""Verify a simple async function in-process.
"""
nums = []
async def print_loop():
2021-02-24 19:59:48 +00:00
async with tractor.open_root_actor(
arbiter_addr=arb_addr,
):
# arbiter is started in-proc if dne
assert tractor.current_actor().is_arbiter
for i in range(10):
nums.append(i)
await trio.sleep(0.1)
2018-06-12 19:23:58 +00:00
start = time.time()
2021-02-24 19:59:48 +00:00
trio.run(print_loop)
2018-06-12 19:23:58 +00:00
# ensure the sleeps were actually awaited
assert time.time() - start >= 1
assert nums == list(range(10))