def test_allforone_handle_failure_restart_directive_cant_restart(supervisor_data):
    pid1 = PID()
    pid1.address = 'address1'
    pid1.id = 'id1'
    pid2 = PID()
    pid2.address = 'address2'
    pid2.id = 'id2'
    children_pids = [pid1, pid2]

    supervisor_data['supervisor'].stop_children = Mock()
    supervisor_data['restart_statistic'].number_of_failures = Mock(return_value=15)
    supervisor_data['supervisor'].children = Mock(return_value=children_pids)
    exc = Exception()

    decider = lambda pid, cause: SupervisorDirective.Restart

    one_for_one = AllForOneStrategy(decider, 10, timedelta(seconds=20))
    one_for_one.request_restart_permission = Mock(return_value=False)

    one_for_one.handle_failure(supervisor_data['supervisor'],
                               supervisor_data['pid_child'],
                               supervisor_data['restart_statistic'],
                               exc)

    supervisor_data['supervisor'].stop_children\
        .assert_called_once_with(*children_pids)
Beispiel #2
0
async def test_allforone_handle_failure_restart_directive_can_restart(supervisor_data):
    pid1 = PID()
    pid1.address = 'address1'
    pid1.id = 'id1'
    pid2 = PID()
    pid2.address = 'address2'
    pid2.id = 'id2'
    children_pids = [pid1, pid2]

    supervisor_data['local_process'].send_system_message = AsyncMock()
    supervisor_data['supervisor'].restart_children = AsyncMock()
    supervisor_data['supervisor'].children = Mock(return_value=children_pids)
    exc = Exception()

    decider = lambda pid, cause: SupervisorDirective.Restart

    one_for_one = AllForOneStrategy(decider, 10, timedelta(seconds=20))
    await one_for_one.handle_failure(supervisor_data['supervisor'],
                                     supervisor_data['pid_child'],
                                     supervisor_data['restart_statistic'],
                                     exc,
                                     None)

    supervisor_data['supervisor'].restart_children \
        .assert_called_once_with(exc, children_pids)
Beispiel #3
0
    def try_add(self, id: str, ref: AbstractProcess) -> 'PID':
        pid = PID(address=self.address, id=id)
        if id not in self._local_actor_refs:
            self._local_actor_refs[id] = ref
            return pid, True

        return pid, False
Beispiel #4
0
    async def Receive(self, stream) -> None:
        targets = []
        async for batch in stream:
            if self.__suspended:
                await stream.send_message(Unit())

            for i in range(len(batch.target_names)):
                targets.append(PID(address=ProcessRegistry().address, id=batch.target_names[i]))

            type_names = list(batch.type_names)
            for envelope in batch.envelopes:
                target = targets[envelope.target]
                type_name = type_names[envelope.type_id]
                message = Serialization().deserialize(type_name, envelope.message_data, envelope.serializer_id)

                if isinstance(message, Terminated):
                    await EndpointManager().remote_terminate(RemoteTerminate(target, message.who))
                elif isinstance(message, Watch):
                    await target.send_system_message(message)
                elif isinstance(message, Unwatch):
                    await target.send_system_message(message)
                elif isinstance(message, Stop):
                    await target.send_system_message(message)
                else:
                    header = None
                    if envelope.message_header is not None:
                        header = proto.MessageHeader(envelope.message_header.header_data)
                    local_envelope = proto.MessageEnvelope(message, envelope.sender, header)
                    await GlobalRootContext().instance.send(target, local_envelope)

        await stream.send_message(Unit())
def supervisor_data():
    supervisor = MockSupervisor()
    mailbox = DefaultMailbox(None, None, None)
    local_process = ActorProcess(mailbox)
    pid_child = PID()
    pid_child.address = 'address'
    pid_child.id = 'id'
    restart_statistic = RestartStatistics(5, datetime(2017, 2, 15))

    return {
        'supervisor': supervisor,
        'mailbox': mailbox,
        'local_process': local_process,
        'pid_child': pid_child,
        'restart_statistic': restart_statistic
    }
Beispiel #6
0
    async def add_cache(self, name: str, pid: PID) -> bool:
        if name not in self._cache:
            key = pid.to_short_string()
            self._cache[name] = pid
            self._reverse_cache[key] = name

            await GlobalRootContext.send(self._watcher, WatchPidRequest(pid))
            return True
        return False
Beispiel #7
0
    async def __process_endpoint_terminated_event_message_in_connected_state(self, context, msg):
        # self.__logger.log_debug()
        for watched_id, pid_set in self._watched.items():
            watcher_pid = PID(address=ProcessRegistry().address, id=watched_id)
            watcher_ref = ProcessRegistry().get(watcher_pid)
            if watcher_ref != DeadLettersProcess():
                for pid in pid_set:
                    await watcher_pid.send_system_message(Terminated(who=pid, address_terminated=True))

        self._watched.clear()
        self._behavior.become(self.terminated)

        await context.my_self.stop()
Beispiel #8
0
 def activator_for_address(self, address):
     return PID(address=address, id='activator')
Beispiel #9
0
 def remove_cache_by_pid(self, pid: PID) -> None:
     key = pid.to_short_string()
     if key in self._reverse_cache:
         name = self._reverse_cache[key]
         del self._reverse_cache[key]
         del self._cache[name]