Add 6sec timeout around `test_simple_rpc` suite for macos
parent
c5af2fa778
commit
706a4b761b
|
|
@ -1,15 +1,25 @@
|
||||||
"""
|
'''
|
||||||
Bidirectional streaming.
|
Audit the simplest inter-actor bidirectional (streaming)
|
||||||
|
msg patterns.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import (
|
||||||
|
Callable,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
import pytest
|
import pytest
|
||||||
import trio
|
import trio
|
||||||
import tractor
|
import tractor
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from tractor import (
|
||||||
|
Portal,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def simple_rpc(
|
async def simple_rpc(
|
||||||
|
|
||||||
ctx: tractor.Context,
|
ctx: tractor.Context,
|
||||||
data: int,
|
data: int,
|
||||||
|
|
||||||
|
|
@ -39,15 +49,14 @@ async def simple_rpc(
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def simple_rpc_with_forloop(
|
async def simple_rpc_with_forloop(
|
||||||
|
|
||||||
ctx: tractor.Context,
|
ctx: tractor.Context,
|
||||||
data: int,
|
data: int,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Same as previous test but using ``async for`` syntax/api.
|
'''
|
||||||
|
Same as previous test but using `async for` syntax/api.
|
||||||
"""
|
|
||||||
|
|
||||||
|
'''
|
||||||
# signal to parent that we're up
|
# signal to parent that we're up
|
||||||
await ctx.started(data + 1)
|
await ctx.started(data + 1)
|
||||||
|
|
||||||
|
|
@ -74,56 +83,59 @@ async def simple_rpc_with_forloop(
|
||||||
'server_func',
|
'server_func',
|
||||||
[simple_rpc, simple_rpc_with_forloop],
|
[simple_rpc, simple_rpc_with_forloop],
|
||||||
)
|
)
|
||||||
def test_simple_rpc(server_func, use_async_for):
|
def test_simple_rpc(
|
||||||
|
server_func: Callabe,
|
||||||
|
use_async_for: bool,
|
||||||
|
):
|
||||||
'''
|
'''
|
||||||
The simplest request response pattern.
|
The simplest request response pattern.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
with trio.fail_after(6):
|
||||||
|
async with tractor.open_nursery() as an:
|
||||||
|
portal: Portal = await an.start_actor(
|
||||||
|
'rpc_server',
|
||||||
|
enable_modules=[__name__],
|
||||||
|
)
|
||||||
|
|
||||||
portal = await n.start_actor(
|
async with portal.open_context(
|
||||||
'rpc_server',
|
server_func, # taken from pytest parameterization
|
||||||
enable_modules=[__name__],
|
data=10,
|
||||||
)
|
) as (ctx, sent):
|
||||||
|
|
||||||
async with portal.open_context(
|
assert sent == 11
|
||||||
server_func, # taken from pytest parameterization
|
|
||||||
data=10,
|
|
||||||
) as (ctx, sent):
|
|
||||||
|
|
||||||
assert sent == 11
|
async with ctx.open_stream() as stream:
|
||||||
|
|
||||||
async with ctx.open_stream() as stream:
|
if use_async_for:
|
||||||
|
|
||||||
if use_async_for:
|
count = 0
|
||||||
|
# receive msgs using async for style
|
||||||
count = 0
|
|
||||||
# receive msgs using async for style
|
|
||||||
print('ping')
|
|
||||||
await stream.send('ping')
|
|
||||||
|
|
||||||
async for msg in stream:
|
|
||||||
assert msg == 'pong'
|
|
||||||
print('ping')
|
print('ping')
|
||||||
await stream.send('ping')
|
await stream.send('ping')
|
||||||
count += 1
|
|
||||||
|
|
||||||
if count >= 9:
|
async for msg in stream:
|
||||||
break
|
assert msg == 'pong'
|
||||||
|
print('ping')
|
||||||
|
await stream.send('ping')
|
||||||
|
count += 1
|
||||||
|
|
||||||
else:
|
if count >= 9:
|
||||||
# classic send/receive style
|
break
|
||||||
for _ in range(10):
|
|
||||||
|
|
||||||
print('ping')
|
else:
|
||||||
await stream.send('ping')
|
# classic send/receive style
|
||||||
assert await stream.receive() == 'pong'
|
for _ in range(10):
|
||||||
|
|
||||||
# stream should terminate here
|
print('ping')
|
||||||
|
await stream.send('ping')
|
||||||
|
assert await stream.receive() == 'pong'
|
||||||
|
|
||||||
# final context result(s) should be consumed here in __aexit__()
|
# stream should terminate here
|
||||||
|
|
||||||
await portal.cancel_actor()
|
# final context result(s) should be consumed here in __aexit__()
|
||||||
|
|
||||||
|
await portal.cancel_actor()
|
||||||
|
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue