Beispiel #1
0
    def test_message_processing_warning(self, conn, warn):

        ag, al = dAgent(conn), A(conn)
        ag.pool = Mock()
        al._on_message = Mock()
        body, message = Mock(), Mock()

        # warning is not triggered when greenlets are disabled
        ag.pool.is_green = True
        ag.process_message(al, body, message)
        self.assertEquals(warn.call_count, 0)
        warn.reset_mock()

        # warning is not triggered when greenlets are enabled
        # but the call is not blocking
        ag.pool.is_green = True
        ag.process_message(al, body, message)
        self.assertEquals(warn.call_count, 0)
        warn.reset_mock()

        # warning is not triggered when greenlets are disables
        # and teh call is blocking
        ag.pool.is_green = False

        import cell
        cell.agents.itemgetter = Mock()
        message.properties = {'reply_to': '1234'}
        ag.process_message(al, body, message)
        warn.assert_called_once_with(
            'Starting a blocking call (%s) on actor (%s) '
            'when greenlets are disabled.', ANY, al.__class__)

        cell.agents.itemgetter.called_once_with('method')
Beispiel #2
0
    def test_message_processing_warning(self, conn, warn):

        ag, al = dAgent(conn), A(conn)
        ag.pool = Mock()
        al._on_message = Mock()
        body, message = Mock(), Mock()

        # warning is not triggered when greenlets are disabled
        ag.pool.is_green = True
        ag.process_message(al, body, message)
        self.assertEquals(warn.call_count, 0)
        warn.reset_mock()

        # warning is not triggered when greenlets are enabled
        # but the call is not blocking
        ag.pool.is_green = True
        ag.process_message(al, body, message)
        self.assertEquals(warn.call_count, 0)
        warn.reset_mock()

        # warning is not triggered when greenlets are disables
        # and teh call is blocking
        ag.pool.is_green = False

        import cell
        cell.agents.itemgetter = Mock()
        message.properties = {'reply_to': '1234'}
        ag.process_message(al, body, message)
        warn.assert_called_once_with(
            'Starting a blocking call (%s) on actor (%s) '
            'when greenlets are disabled.',
            ANY, al.__class__)

        cell.agents.itemgetter.called_once_with('method')
Beispiel #3
0
        def config(self, act_type, number):
            agent = dAgent(self.actor.connection)

            for _ in range(0, number):
                agent.spawn(
                    act_type,
                    {'group_exchange': self.actor.inbox_scatter.name})
Beispiel #4
0
    def test_select_returns_error_when_no_result_found(self, conn):
        def scatter_result():
            yield None

        gen = scatter_result()
        next(gen)
        ag = dAgent(conn)
        ag.scatter = Mock(return_value=gen)

        with self.assertRaises(KeyError):
            ag.select(A)
Beispiel #5
0
    def test_select_returns_error_when_no_result_found(self, conn):

        def scatter_result():
            yield None

        gen = scatter_result()
        next(gen)
        ag = dAgent(conn)
        ag.scatter = Mock(return_value=gen)

        with self.assertRaises(KeyError):
            ag.select(A)
Beispiel #6
0
    def test_gather_kwargs(self, conn, collect):
        actor = Actor(conn)
        ares = AsyncResult(uuid(), actor)
        prev_to_python = ares.to_python
        new_to_python = lambda x, propagate = True: x
        ares.to_python = new_to_python

        # Test default kwargs,
        # nothing is passed, the actor does not have agent assigned
        self.assert_gather_kwargs(
            ares, collect, {},
            timeout=actor.default_timeout, ignore_timeout=False)

        # limit - set the default agent limit if NONE is set
        # Test default kwargs, nothing is passed,
        # the actor does have default agent assigned
        actor.agent = dAgent(conn)
        self.assert_gather_kwargs(
            ares, collect, {},
            timeout=actor.default_timeout, limit=None, ignore_timeout=False)

        # limit - set the default agent limit if NONE is set
        # Test default kwargs, nothing is passed,
        # the actor does have agent with custom scatter limit assigned
        ag = Ag(conn)
        actor.agent = ag
        self.assert_gather_kwargs(
            ares, collect, {}, timeout=actor.default_timeout,
            limit=ag.get_default_scatter_limit())

        # pass all args
        actor.agent = Ag(conn)
        timeout, ignore_timeout, limit = 200.0, False, uuid()

        self.assert_gather_kwargs(
            ares, collect,
            {'timeout': timeout, 'ignore_timeout': ignore_timeout,
             'limit': limit},
            timeout=timeout, limit=limit, ignore_timeout=ignore_timeout)

        # ig ignore_tiemout is passed,
        # the custom logic for limit is not applies
        actor.agent = None
        timeout, ignore_timeout = 200.0, True
        self.assert_gather_kwargs(
            ares, collect,
            {'timeout': timeout, 'ignore_timeout': ignore_timeout},
            timeout=timeout, ignore_timeout=ignore_timeout)

        ares.to_python = prev_to_python
Beispiel #7
0
    def test_message_processing_when_greenlets_are_disabled(self, conn):
        ag = dAgent(conn)
        ag.pool = Mock()
        al = Mock()
        ag.pool.is_green = False
        body, message = Mock(), Mock()

        ag.process_message(al, body, message)

        al._on_message.assert_called_once_with(body, message)

        ag._on_message = Mock()
        ag.process_message(ag, body, message)
        self.assertEqual(ag.pool.spawn_n.call_count, 0)
        ag._on_message.assert_called_once_with(body, message)
Beispiel #8
0
    def test_message_processing_when_greenlets_are_disabled(self, conn):
        ag = dAgent(conn)
        ag.pool = Mock()
        al = Mock()
        ag.pool.is_green = False
        body, message = Mock(), Mock()

        ag.process_message(al, body, message)

        al._on_message.assert_called_once_with(body, message)

        ag._on_message = Mock()
        ag.process_message(ag, body, message)
        self.assertEqual(ag.pool.spawn_n.call_count, 0)
        ag._on_message.assert_called_once_with(body, message)
Beispiel #9
0
    def test_messages_processing_when_greenlets_are_enabled(self, conn):

        ag = dAgent(conn)
        ag.pool = Mock()
        ag.pool.is_green = True
        al, body, message = Mock(), Mock(), Mock()
        self.assertEqual(ag.is_green(), True)

        # message is processed in a separate pool if
        # greenlets are enabled and the sending actor is not an agent
        ag.process_message(al, body, message)
        ag.pool.spawn_n.assert_called_once_with(al._on_message, body, message)
        ag.pool.reset_mock()

        # message is always processed in a the same thread
        # if the sending actor is an agent
        ag._on_message = Mock()
        ag.process_message(ag, body, message)
        self.assertEqual(ag.pool.spawn_n.call_count, 0)
        ag._on_message.assert_called_once_with(body, message)
Beispiel #10
0
    def test_state_select_returns_from_registry(self, conn):
        class B(Actor):
            pass

        ag = dAgent(conn)
        id1, id2 = uuid(), uuid()

        with self.assertRaises(Actor.Next):
            ag.state.select(qualname(A))

        ag.state.registry[id1] = A()
        key = ag.state.select(qualname(A))

        self.assertEqual(key, id1)

        ag.state.registry[id2] = B(conn)
        keyA = ag.state.select(qualname(A))
        keyB = ag.state.select(qualname(B))
        self.assertEqual(keyA, id1)
        self.assertEqual(keyB, id2)
Beispiel #11
0
    def test_messages_processing_when_greenlets_are_enabled(self, conn):

        ag = dAgent(conn)
        ag.pool = Mock()
        ag.pool.is_green = True
        al, body, message = Mock(), Mock(), Mock()
        self.assertEqual(ag.is_green(), True)

        # message is processed in a separate pool if
        # greenlets are enabled and the sending actor is not an agent
        ag.process_message(al, body, message)
        ag.pool.spawn_n.assert_called_once_with(al._on_message, body, message)
        ag.pool.reset_mock()

        # message is always processed in a the same thread
        # if the sending actor is an agent
        ag._on_message = Mock()
        ag.process_message(ag, body, message)
        self.assertEqual(ag.pool.spawn_n.call_count, 0)
        ag._on_message.assert_called_once_with(body, message)
Beispiel #12
0
    def test_state_select_returns_from_registry(self, conn):
        class B(Actor):
            pass

        ag = dAgent(conn)
        id1, id2 = uuid(), uuid()

        with self.assertRaises(Actor.Next):
            ag.state.select(qualname(A))

        ag.state.registry[id1] = A()
        key = ag.state.select(qualname(A))

        self.assertEqual(key, id1)

        ag.state.registry[id2] = B(conn)
        keyA = ag.state.select(qualname(A))
        keyB = ag.state.select(qualname(B))
        self.assertEqual(keyA, id1)
        self.assertEqual(keyB, id2)
Beispiel #13
0
    def test_select_returns_scatter_results(self, conn):
        id1, id2 = uuid(), uuid()

        def scatter_result():
            yield id1
            yield id2

        ag = dAgent(conn)
        ag.scatter = Mock(return_value=scatter_result())

        proxy = ag.select(A)

        ag.scatter.assert_called_once_with(
            'select', {'cls': qualname(A)}, limit=1)

        # Check ActorProxy initialisation
        self.assertIsInstance(proxy, ActorProxy)
        self.assertEqual(proxy.id, id1)
        self.assertIsInstance(proxy._actor, A)
        self.assertEqual(proxy._actor.name, A().__class__.__name__)
        self.assertEqual(proxy._actor.connection, conn)
        self.assertEqual(proxy._actor.agent, ag)
        self.assertIsNone(proxy.async_start_result)
Beispiel #14
0
    def test_select_returns_scatter_results(self, conn):
        id1, id2 = uuid(), uuid()

        def scatter_result():
            yield id1
            yield id2

        ag = dAgent(conn)
        ag.scatter = Mock(return_value=scatter_result())

        proxy = ag.select(A)

        ag.scatter.assert_called_once_with('select', {'cls': qualname(A)},
                                           limit=1)

        # Check ActorProxy initialisation
        self.assertIsInstance(proxy, ActorProxy)
        self.assertEqual(proxy.id, id1)
        self.assertIsInstance(proxy._actor, A)
        self.assertEqual(proxy._actor.name, A().__class__.__name__)
        self.assertEqual(proxy._actor.connection, conn)
        self.assertEqual(proxy._actor.agent, ag)
        self.assertIsNone(proxy.async_start_result)
Beispiel #15
0
 def __init__(self, actors):
     self.actors_mng = dAgent(connection=my_app.broker_connection(),
                              app=my_app)
     self.actors = actors
Beispiel #16
0
        def config(self, act_type, number):
            agent = dAgent(self.actor.connection)

            for _ in range(0, number):
                agent.spawn(act_type,
                            {'group_exchange': self.actor.inbox_scatter.name})
Beispiel #17
0
 def __init__(self, actors):
     self.actors_mng = dAgent(
         connection=my_app.broker_connection(),
         app=my_app)
     self.actors = actors
Beispiel #18
0
import celery
from cell.actors import Actor
from cell.agents import dAgent
from kombu.utils import uuid
from examples.workflow import forward

my_app = celery.Celery(broker='pyamqp://guest@localhost//')
agent = dAgent(connection=my_app.broker_connection())


class Adder(Actor):
    def __init__(self, connection=None, *args, **kwargs):
        super(Adder, self).__init__(connection or my_app.broker_connection(),
                                    *args, **kwargs)

    class state():
        def add_one(self, i, token=None):
            print 'Increasing %s with one' % i
            res = i + 1
            self.actor.emit('count', {'res': res, 'token': token})
            return res


class Counter(Actor):
    def __init__(self, connection=None, *args, **kwargs):
        super(Counter, self).__init__(connection or my_app.broker_connection(),
                                      *args, **kwargs)

    class state():
        def __init__(self):
            self.targets = {}
Beispiel #19
0
 def __init__(self):
     self.agent = agent = dAgent(Connection())
     self.abs = agent.spawn(Abs)
     self.sqrt = agent.spawn(SquareRoot)
     self.square = agent.spawn(Square)
     self.printer = agent.spawn(Printer)
Beispiel #20
0
import celery
from cell.actors import Actor
from cell.agents import dAgent

my_app = celery.Celery(broker='pyamqp://guest@localhost//')
agent = dAgent(connection=my_app.broker_connection())


class User(Actor):
    def __init__(self, connection=None, *args, **kwargs):
            super(User, self).__init__(
                connection or my_app.broker_connection(), *args, **kwargs)

    class state():

        def post(self, msg):
            print msg

        def connect(self):
            return agent.spawn(self.__class__)

    def connect(self, nickname):
        self.call('connect', {'name': nickname})

    def post(self, msg):
        msg = 'Posting on the wall: %s' % msg
        self.scatter('post', {'msg': msg})

    def message_to(self, actor, msg):
        a = User(id=actor, connection=self.connection)
        msg = 'Actor %s is sending you a message: %s' % (self.id, msg)
Beispiel #21
0
 def __init__(self):
     self.agent = agent = dAgent(Connection())
     self.abs = agent.spawn(Abs)
     self.sqrt = agent.spawn(SquareRoot)
     self.square = agent.spawn(Square)
     self.printer = agent.spawn(Printer)