Ejemplo n.º 1
0
    def test_hello(self):
        """Test hello."""
        for i in range(4):
            self.actors.append(Hello())

        dispatcher = Dispatcher()
        msg = dispatcher.send('hello', 'word')
        assert 'Hello word' == dispatcher.result(msg, timeout=2)
Ejemplo n.º 2
0
    def test_hello(self):
        """Test hello."""
        for i in range(2):
            self.actors.append(Counter())

        dispatcher = Dispatcher()
        msg = dispatcher.send('counter', None)
        assert dispatcher.result(msg, timeout=2) == 1
        msg = dispatcher.send('counter', None)
        assert dispatcher.result(msg, timeout=2) == 1
        msg = dispatcher.send('counter', None)
        assert dispatcher.result(msg, timeout=2) == 2
        msg = dispatcher.send('counter', None)
        assert dispatcher.result(msg, timeout=2) == 2
Ejemplo n.º 3
0
def node(port, conn, barrier):
    with Dispatcher() as dispatcher:  # noqa
        dispatcher.setup(True, True)
        with Hello() as _actor:  # noqa
            barrier.wait()
            dispatcher.connect_to_server('0.0.0.0', port)
            conn.recv()  # wait to exit
Ejemplo n.º 4
0
 def transact(self):
     self.actors = []
     with Dispatcher() as dispatcher:
         dispatcher.setup(True, True)
         yield
         for actor in self.actors:
             actor.cleanup()
def master(port, barrier, conn):
    with Dispatcher() as dispatcher:
        dispatcher.setup(True, True)
        dispatcher.setup_server('0.0.0.0', port)
        with Hello() as _actor:  # noqa
            barrier.wait()
            conn.send(True)
            conn.recv()
            dispatcher.server.close()
Ejemplo n.º 6
0
def master(port, conn, result_conn, barrier):
    with Dispatcher() as dispatcher:  # noqa
        dispatcher.setup(True, True)
        dispatcher.setup_server('0.0.0.0', port)
        msg = dispatcher.send('hello', 'world')
        barrier.wait()
        result_conn.send(dispatcher.result(msg))

        dispatcher.logger.debug('exit_text')
        dispatcher.server.close()
Ejemplo n.º 7
0
 def transact(self):
     self.actors = []
     with Dispatcher() as dispatcher:
         config = ConfigParser()
         config.add_section('dispatcher')
         config.set('dispatcher', 'selection', 'round_robin')
         dispatcher.setup(True, True, config)
         yield
         for actor in self.actors:
             actor.cleanup()
def node(port, barrier, conn, result_conn):
    with Dispatcher() as dispatcher:
        dispatcher.setup(True, True)
        barrier.wait()
        dispatcher.connect_to_server('0.0.0.0', port)
        conn.recv()
        sleep(0.3)  # nodes synchronization
        msg = dispatcher.send('hello', 'world')
        result_conn.send(dispatcher.result(msg))
        conn.send(True)
Ejemplo n.º 9
0
 def transact(self):
     with Dispatcher() as dispatcher:
         dispatcher.setup(True, True)
         yield
Ejemplo n.º 10
0
class Actor():
    """Actor base class.

    New implementation of actors should be child of this class.
    """
    @property
    def name(self):
        """Get actor name."""
        return re.sub(RGX, '\\1_\\2', self.__class__.__name__).lower()

    def __init__(self, *_args, **_kwargs):
        """Initialize actor."""
        self._dispatcher = Dispatcher()
        self.id = self._dispatcher.uuid()
        self._dispatcher.add_actor(self)

    def __del__(self):
        """Remove from dispatcher."""
        self._dispatcher.remove_actor(self)
        self.cleanup()

    def __enter__(self):
        return self

    def __str__(self):
        return self.name

    def __exit__(self, *args, **kwargs):
        self.cleanup()

    def cleanup(self, *args, **kwargs):
        """Cleanup method.

        Util for cleanup information when actor exits.

        By default does nothing, this could be implemented on child
        actors.
        """

    def setup(self):
        """Setup method.

        Util for define configurations and associate objects to the actor at
        startup.

        By default does nothing, this could be implemented on child
        actors.
        """

    def send(self, msg: object, target_id: str = None, sender_id: str = None):
        """Send msg to actor.

        Params:
          * msg (object): Message data to be sent to an actor. It should
            be serializable
          * target_id(str): if provided send message to specified actor
            instance
          * sender_id(str): if provided specify message sender to target agent
        """
        return self._dispatcher.dispatch(msg, self.name, target_id or self.id,
                                         sender_id)

    def result(self, *args, **kwargs):
        """Result method.

        Method for waiting result about executed task. This should
        be implemented in child actors.
        """
        raise NotImplementedError()

    def loop(self):
        """Loop method

        For reading incoming messages. This should be implemented in child
        actors.
        """
        raise NotImplementedError()
Ejemplo n.º 11
0
 def __init__(self, *_args, **_kwargs):
     """Initialize actor."""
     self._dispatcher = Dispatcher()
     self.id = self._dispatcher.uuid()
     self._dispatcher.add_actor(self)
Ejemplo n.º 12
0
 def setup(self):
     """Setup test."""
     with Dispatcher() as dispatcher:
         self.dispatcher = dispatcher
         dispatcher.setup(True, True)
         yield