2018-06-07 04:29:17 +00:00
|
|
|
"""
|
2018-09-05 02:17:02 +00:00
|
|
|
Arbiter and "local" actor api
|
2018-06-07 04:29:17 +00:00
|
|
|
"""
|
2018-06-12 19:23:58 +00:00
|
|
|
import time
|
|
|
|
|
2018-06-07 04:29:17 +00:00
|
|
|
import pytest
|
2018-06-12 19:23:58 +00:00
|
|
|
import trio
|
2018-07-05 23:49:21 +00:00
|
|
|
import tractor
|
2018-06-07 04:29:17 +00:00
|
|
|
|
2018-07-10 21:19:54 +00:00
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
@pytest.mark.trio
|
|
|
|
async def test_no_arbitter():
|
|
|
|
"""An arbitter must be established before any nurseries
|
|
|
|
can be created.
|
|
|
|
|
|
|
|
(In other words ``tractor.run`` must be used instead of ``trio.run`` as is
|
|
|
|
done by the ``pytest-trio`` plugin.)
|
|
|
|
"""
|
|
|
|
with pytest.raises(RuntimeError):
|
2018-06-19 15:49:25 +00:00
|
|
|
with tractor.open_nursery():
|
2018-06-12 19:23:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-08-07 18:30:25 +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.
|
|
|
|
"""
|
2018-06-19 15:49:25 +00:00
|
|
|
nums = []
|
|
|
|
|
|
|
|
async def print_loop():
|
|
|
|
# arbiter is started in-proc if dne
|
|
|
|
assert tractor.current_actor().is_arbiter
|
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
for i in range(10):
|
2018-06-19 15:49:25 +00:00
|
|
|
nums.append(i)
|
2018-06-12 19:23:58 +00:00
|
|
|
await trio.sleep(0.1)
|
|
|
|
|
|
|
|
start = time.time()
|
2018-08-07 18:30:25 +00:00
|
|
|
tractor.run(print_loop, arbiter_addr=arb_addr)
|
2018-06-19 15:49:25 +00:00
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
# ensure the sleeps were actually awaited
|
|
|
|
assert time.time() - start >= 1
|
2018-06-19 15:49:25 +00:00
|
|
|
assert nums == list(range(10))
|