def test_setting_message_sender_only_works_once():

    c = MessagePassingComputation("c")

    c.message_sender = MagicMock()
    with pytest.raises(AttributeError):
        c.message_sender = MagicMock()
def test_setting_message_sender_on_computation():

    c = MessagePassingComputation("c")

    c.message_sender = MagicMock()

    msg = Message("type")
    c.post_msg("target", msg)

    c.message_sender.assert_called_with("c", "target", msg, None, None)
Esempio n. 3
0
    def add_computation(self,
                        computation: MessagePassingComputation,
                        comp_name=None):
        """
        Add a computation to the agent.

        The computation will run on this agent thread and receives messages
        through his Messaging and CommunicationLayer.

        Parameters
        ----------
        computation: a MessagePassingComputation
            the computation to be added

        comp_name: str
            an optional name for the computation, if not given
            computation.name will be used.

        """
        comp_name = computation.name if comp_name is None else comp_name
        self.logger.debug('Add computation %s - %s ', comp_name,
                          self._messaging)
        computation.message_sender = self._messaging.post_msg
        self._computations[comp_name] = computation
        self.discovery.register_computation(comp_name, self.name, self.address)

        # start lookup for agent hosting a neighbor computation
        if hasattr(computation, 'computation_def') and \
                computation.computation_def is not None:
            for n in computation.computation_def.node.neighbors:
                self.discovery.subscribe_computation(n)

        if hasattr(computation, '_on_value_selection'):
            computation._on_value_selection = notify_wrap(
                computation._on_value_selection,
                partial(self._on_computation_value_changed, computation.name))
        if hasattr(computation, '_on_new_cycle'):
            computation._on_new_cycle = notify_wrap(
                computation._on_new_cycle,
                partial(self._on_computation_new_cycle, computation.name))

        computation.finished = notify_wrap(
            computation.finished,
            partial(self._on_computation_finished, computation.name))
Esempio n. 4
0
 def add_computation(self,
                     computation: MessagePassingComputation,
                     comp_name=None):
     super().add_computation(computation, comp_name)
     if self.replication_comp is not None \
             and not computation.name.startswith('_')\
             and not computation.name.startswith('B'):
         # FIXME : find a better way to filter out repair computation than
         # looking at the first character (B).
         self.replication_comp.add_computation(computation.computation_def,
                                               computation.footprint())