Merge pull request #39 from tgoodlet/self_register

Verify arbiter self registration works
loglevel_to_tractor_tests
goodboy 2018-09-21 10:18:17 -04:00 committed by GitHub
commit 8b7bf4fc07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View File

@ -31,6 +31,26 @@ def test_no_main():
tractor.run(None) tractor.run(None)
@tractor_test
async def test_self_is_registered():
"Verify waiting on the arbiter to register itself using the standard api."
actor = tractor.current_actor()
assert actor.is_arbiter
async with tractor.wait_for_actor('arbiter') as portal:
assert portal.channel.uid[0] == 'arbiter'
@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)
sockaddr = await portal.run('self', 'wait_for_actor', name='arbiter')
assert sockaddr[0] == arb_addr
def test_local_actor_async_func(arb_addr): def test_local_actor_async_func(arb_addr):
"""Verify a simple async function in-process. """Verify a simple async function in-process.
""" """

View File

@ -768,7 +768,7 @@ async def wait_for_actor(
) -> typing.AsyncGenerator[Portal, None]: ) -> typing.AsyncGenerator[Portal, None]:
"""Wait on an actor to register with the arbiter. """Wait on an actor to register with the arbiter.
A portal to the first actor which registered is be returned. A portal to the first registered actor is returned.
""" """
actor = current_actor() actor = current_actor()
async with get_arbiter(*arbiter_sockaddr or actor._arb_addr) as arb_portal: async with get_arbiter(*arbiter_sockaddr or actor._arb_addr) as arb_portal:

View File

@ -2,6 +2,7 @@
Portal api Portal api
""" """
import importlib import importlib
import inspect
import typing import typing
from typing import Tuple, Any, Dict, Optional from typing import Tuple, Any, Dict, Optional
@ -216,11 +217,15 @@ class LocalPortal:
) -> None: ) -> None:
self.actor = actor self.actor = actor
async def run(self, ns: str, func: str, **kwargs) -> Any: async def run(self, ns: str, func_name: str, **kwargs) -> Any:
"""Run a requested function locally and return it's result. """Run a requested function locally and return it's result.
""" """
obj = self.actor if ns == 'self' else importlib.import_module(ns) obj = self.actor if ns == 'self' else importlib.import_module(ns)
return getattr(obj, func)(**kwargs) func = getattr(obj, func_name)
if inspect.iscoroutinefunction(func):
return await func(**kwargs)
else:
return func(**kwargs)
@asynccontextmanager @asynccontextmanager