Exemple #1
0
async def test_two_pools():
    start_method = os.environ.get('POOL_START_METHOD', 'forkserver') \
        if sys.platform != 'win32' else None

    ctx = get_context()

    pool1 = await create_actor_pool('127.0.0.1',
                                    pool_cls=MainActorPool,
                                    n_process=2,
                                    subprocess_start_method=start_method)
    pool2 = await create_actor_pool('127.0.0.1',
                                    pool_cls=MainActorPool,
                                    n_process=2,
                                    subprocess_start_method=start_method)

    def is_interprocess_address(addr):
        if sys.platform.startswith('win'):
            return re.match(r'127\.0\.0\.1:\d+', addr)
        else:
            return addr.startswith('unixsocket://')

    try:
        actor_ref1 = await ctx.create_actor(TestActor,
                                            address=pool1.external_address,
                                            allocate_strategy=MainPool())
        assert actor_ref1.address == pool1.external_address
        assert await actor_ref1.add(1) == 1
        assert Router.get_instance().get_internal_address(
            actor_ref1.address).startswith('dummy://')

        actor_ref2 = await ctx.create_actor(TestActor,
                                            address=pool1.external_address,
                                            allocate_strategy=RandomSubPool())
        assert actor_ref2.address in pool1._config.get_external_addresses()[1:]
        assert await actor_ref2.add(3) == 3
        assert is_interprocess_address(
            Router.get_instance().get_internal_address(actor_ref2.address))

        actor_ref3 = await ctx.create_actor(TestActor,
                                            address=pool2.external_address,
                                            allocate_strategy=MainPool())
        assert actor_ref3.address == pool2.external_address
        assert await actor_ref3.add(5) == 5
        assert Router.get_instance().get_internal_address(
            actor_ref3.address).startswith('dummy://')

        actor_ref4 = await ctx.create_actor(TestActor,
                                            address=pool2.external_address,
                                            allocate_strategy=RandomSubPool())
        assert actor_ref4.address in pool2._config.get_external_addresses()[1:]
        assert await actor_ref4.add(7) == 7
        assert is_interprocess_address(
            Router.get_instance().get_internal_address(actor_ref4.address))

        assert await actor_ref2.add_other(actor_ref4, 3) == 13
    finally:
        await pool1.stop()
        await pool2.stop()
async def test_mars_send(actor_pool_context):
    pool = actor_pool_context
    ref1 = await mo.create_actor(DummyActor, 1, address=pool.external_address)
    ref2 = await mo.actor_ref(await ref1.create(DummyActor, 2, address=pool.external_address))
    assert await ref1.send(ref2, 'add', 3) == 5

    ref3 = await mo.create_actor(DummyActor, 1, address=pool.external_address)
    ref4 = await mo.create_actor(DummyActor, 2, address=pool.external_address,
                                 allocate_strategy=RandomSubPool())
    assert await ref4.send(ref3, 'add', 3) == 4
Exemple #3
0
async def test_create_actor_pool():
    start_method = os.environ.get('POOL_START_METHOD', 'forkserver') \
        if sys.platform != 'win32' else None
    pool = await create_actor_pool('127.0.0.1',
                                   pool_cls=MainActorPool,
                                   n_process=2,
                                   subprocess_start_method=start_method)

    async with pool:
        # test global router
        global_router = Router.get_instance()
        # global router should not be the identical one with pool's router
        assert global_router is not pool.router
        assert pool.external_address in global_router._curr_external_addresses
        assert pool.external_address in global_router._mapping

        ctx = get_context()

        # actor on main pool
        actor_ref = await ctx.create_actor(TestActor,
                                           uid='test-1',
                                           address=pool.external_address)
        assert await actor_ref.add(3) == 3
        assert await actor_ref.add(1) == 4
        assert (await ctx.has_actor(actor_ref)) is True
        assert (await ctx.actor_ref(actor_ref)) == actor_ref
        # test cancel
        task = asyncio.create_task(actor_ref.sleep(20))
        await asyncio.sleep(0)
        task.cancel()
        assert await task == 5
        await ctx.destroy_actor(actor_ref)
        assert (await ctx.has_actor(actor_ref)) is False
        for f in actor_ref.add, ctx.actor_ref, ctx.destroy_actor:
            with pytest.raises(ActorNotExist):
                await f(actor_ref)

        # actor on sub pool
        actor_ref1 = await ctx.create_actor(TestActor,
                                            uid='test-main',
                                            address=pool.external_address)
        actor_ref2 = await ctx.create_actor(TestActor,
                                            uid='test-2',
                                            address=pool.external_address,
                                            allocate_strategy=RandomSubPool())
        assert (await ctx.actor_ref(uid='test-2',
                                    address=actor_ref2.address)) == actor_ref2
        main_ref = await ctx.actor_ref(uid='test-main',
                                       address=actor_ref2.address)
        assert main_ref.address == pool.external_address
        main_ref = await ctx.actor_ref(actor_ref1)
        assert main_ref.address == pool.external_address
        assert actor_ref2.address != actor_ref.address
        assert await actor_ref2.add(3) == 3
        assert await actor_ref2.add(1) == 4
        with pytest.raises(RuntimeError):
            await actor_ref2.return_cannot_unpickle()
        assert (await ctx.has_actor(actor_ref2)) is True
        assert (await ctx.actor_ref(actor_ref2)) == actor_ref2
        # test cancel
        task = asyncio.create_task(actor_ref2.sleep(20))
        start = time.time()
        await asyncio.sleep(0)
        task.cancel()
        assert await task == 5
        assert time.time() - start < 3
        await ctx.destroy_actor(actor_ref2)
        assert (await ctx.has_actor(actor_ref2)) is False

    # after pool shutdown, global router must has been cleaned
    global_router = Router.get_instance()
    assert len(global_router._curr_external_addresses) == 0
    assert len(global_router._mapping) == 0
Exemple #4
0
def test_random_sub_pool():
    strategy = RandomSubPool()
    addresses = config.get_external_addresses()[1:]
    assert strategy.get_allocated_address(config, dict()) in addresses