async def test_sub_actor_pool(notify_main_pool): notify_main_pool.return_value = None config = ActorPoolConfig() ext_address0 = f'127.0.0.1:{get_next_port()}' ext_address1 = f'127.0.0.1:{get_next_port()}' _add_pool_conf(config, 0, 'main', 'unixsocket:///0', ext_address0) _add_pool_conf(config, 1, 'sub', 'unixsocket:///1', ext_address1) pool = await SubActorPool.create({ 'actor_pool_config': config, 'process_index': 1 }) await pool.start() create_actor_message = CreateActorMessage( new_message_id(), TestActor, b'test', tuple(), dict(), AddressSpecified(pool.external_address)) message = await pool.create_actor(create_actor_message) assert message.message_type == MessageType.result actor_ref = message.result assert actor_ref.address == pool.external_address assert actor_ref.uid == b'test' has_actor_message = HasActorMessage(new_message_id(), actor_ref) assert (await pool.has_actor(has_actor_message)).result is True actor_ref_message = ActorRefMessage(new_message_id(), actor_ref) assert (await pool.actor_ref(actor_ref_message)).result == actor_ref tell_message = TellMessage(new_message_id(), actor_ref, ('add', 0, (1, ), dict())) message = await pool.tell(tell_message) assert message.result is None send_message = SendMessage(new_message_id(), actor_ref, ('add', 0, (3, ), dict())) message = await pool.send(send_message) assert message.result == 4 # test error message # type mismatch send_message = SendMessage(new_message_id(), actor_ref, ('add', 0, ('3', ), dict())) result = await pool.send(send_message) assert result.message_type == MessageType.error assert isinstance(result.error, TypeError) send_message = SendMessage( new_message_id(), create_actor_ref(actor_ref.address, 'non_exist'), ('add', 0, (3, ), dict())) result = await pool.send(send_message) assert isinstance(result.error, ActorNotExist) # test send message and cancel it send_message = SendMessage(new_message_id(), actor_ref, ('sleep', 0, (20, ), dict())) result_task = asyncio.create_task(pool.send(send_message)) await asyncio.sleep(0) start = time.time() cancel_message = CancelMessage(new_message_id(), actor_ref.address, send_message.message_id) cancel_task = asyncio.create_task(pool.cancel(cancel_message)) result = await asyncio.wait_for(cancel_task, 3) assert result.message_type == MessageType.result assert result.result is True result = await result_task # test time assert time.time() - start < 3 assert result.message_type == MessageType.result assert result.result == 5 # test processing message on background async with await pool.router.get_client(pool.external_address) as client: send_message = SendMessage(new_message_id(), actor_ref, ('add', 0, (5, ), dict())) await client.send(send_message) result = await client.recv() assert result.result == 9 send_message = SendMessage(new_message_id(), actor_ref, ('add', 0, ('5', ), dict())) await client.send(send_message) result = await client.recv() assert isinstance(result.error, TypeError) destroy_actor_message = DestroyActorMessage(new_message_id(), actor_ref) message = await pool.destroy_actor(destroy_actor_message) assert message.result == actor_ref.uid # send destroy failed message = await pool.destroy_actor(destroy_actor_message) assert isinstance(message.error, ActorNotExist) message = await pool.has_actor(has_actor_message) assert not message.result # test sync config _add_pool_conf(config, 1, 'sub', 'unixsocket:///1', f'127.0.0.1:{get_next_port()}') sync_config_message = ControlMessage(new_message_id(), '', ControlMessageType.sync_config, config) message = await pool.handle_control_command(sync_config_message) assert message.result is True # test get config get_config_message = ControlMessage(new_message_id(), '', ControlMessageType.get_config, None) message = await pool.handle_control_command(get_config_message) config2 = message.result assert config.as_dict() == config2.as_dict() assert pool.router._mapping == Router.get_instance()._mapping assert pool.router._curr_external_addresses == \ Router.get_instance()._curr_external_addresses stop_message = ControlMessage(new_message_id(), '', ControlMessageType.stop, None) message = await pool.handle_control_command(stop_message) assert message.result is True await pool.join(.05) assert pool.stopped
def test_address_specified(): addr = '127.0.0.1:1112' strategy = AddressSpecified(addr) assert strategy.get_allocated_address(config, dict()) == addr