Beispiel #1
0
def test_tell_sends_message_and_sender_to_cell():
    cell = Mock(spec_set=Cell)
    sender = Mock(spec_set=ActorRef)
    message = object()
    ref = ActorRef(cell)
    ref.tell(message, sender)

    cell.send_message.assert_called_once_with(message, sender)
Beispiel #2
0
def test_terminate_sends_unhandled_messages_to_dead_letters(
        cell, mailbox, system):
    message = Mock()
    sender = Mock()
    mailbox.flush_messages.return_value = [Envelope(message, sender)]
    cell.handle_system_message(Terminate)
    system.dead_letters.tell.assert_called_once_with(
        DeadLetter(message, sender, ActorRef(cell)))
Beispiel #3
0
def test_sending_message_to_terminated_actor_should_forward_to_dead_letters(
        cell, system):
    cell.handle_system_message(Terminate)
    message = Mock()
    sender = Mock()
    cell.send_message(message, sender)
    system.dead_letters.tell.assert_called_once_with(
        DeadLetter(message, sender, ActorRef(cell)))
Beispiel #4
0
def test_failure_in_start_hook_should_send_failure_to_supervisor(
        actor, supervisor):
    actor.pre_start.side_effect = AttributeError()
    cell = Cell(lambda: actor, Mock(), Mock(), supervisor)
    cell.handle_system_message(Start)
    actor.pre_start.assert_called_once_with()
    _check_sent_failure_message(ActorRef(cell), supervisor,
                                ActorInitializationError())
Beispiel #5
0
def test_equality():
    cell_1 = object()
    cell_2 = object()
    assert ActorRef(cell_1) != ActorRef(cell_2)
    assert ActorRef(cell_1) == ActorRef(cell_1)
    assert InternalRef(cell_1) != InternalRef(cell_2)
    assert InternalRef(cell_1) == InternalRef(cell_1)
    assert InternalRef(cell_1) == ActorRef(cell_1)
    assert ActorRef(cell_1) != object()
Beispiel #6
0
def test_context_properties(actor, supervisor, running_cell):
    sender = object()

    global actual_sender, actual_supervisor, actual_self_ref
    actual_sender = actual_supervisor = actual_self_ref = None

    def store_context(message):
        global actual_sender, actual_supervisor, actual_self_ref
        actual_sender = actor.context.sender
        actual_supervisor = actor.context.supervisor
        actual_self_ref = actor.context.self_ref

    actor.receive.side_effect = store_context

    running_cell.handle_message(Envelope(Mock(), sender))
    assert actor.receive.call_count == 1

    assert actual_sender is sender
    assert actual_supervisor is supervisor
    assert actual_self_ref == ActorRef(running_cell)
Beispiel #7
0
    def actor_of(self, cls=None, behaviour=None, dispatcher=None):
        if cls:
            factory = cls
        elif behaviour:
            factory = _actor_from_behaviour(behaviour)
        else:
            raise ValueError()

        if dispatcher is None:
            dispatcher = self._system._system_dispatcher

        from actors.internal.cell import Cell
        cell = Cell(factory,
                    dispatcher=dispatcher,
                    system=self._system,
                    parent=self._supervisor)

        internal_ref = InternalRef(cell)
        self._supervisor.send_system_message(Supervise(internal_ref))
        internal_ref.send_system_message(Start)

        return ActorRef(cell)
Beispiel #8
0
def test_tell_raises_if_sender_is_not_an_actor_ref():
    ref = ActorRef(Mock())
    with pytest.raises(ValueError):
        ref.tell(Mock(), object())
Beispiel #9
0
def test_failure_in_message_handler_sends_failure_message_to_supervisor(
        actor, supervisor, running_cell):
    actor.receive.side_effect = ArithmeticError()
    running_cell.handle_message(Mock())
    _check_sent_failure_message(ActorRef(running_cell), supervisor,
                                ArithmeticError())
Beispiel #10
0
def test_failure_in_restart_hook_should_send_failure_to_supervisor(
        actor, supervisor, running_cell):
    actor.pre_restart.side_effect = ArithmeticError()
    running_cell.handle_system_message(Restart)
    _check_sent_failure_message(ActorRef(running_cell), supervisor,
                                ActorInitializationError())
Beispiel #11
0
def test_sends_failure_if_factory_does_not_return_an_actor(supervisor):
    cell = Cell(lambda: object(), Mock(), Mock(), supervisor)
    cell.handle_system_message(Start)
    _check_sent_failure_message(ActorRef(cell), supervisor,
                                ActorInitializationError())