Пример #1
0
 def __init__(self, config):
     Actor.__init__(self, config)
     self.withresponse_actor = None
     self.withresponse_msg = None
Пример #2
0
    def test_actor_async(self):
        import gevent
        from synapse.message import Message
        from synapse.node import AnnounceServer, Actor, async

        class DummyMessage(Message):
            type = 'dummy'

            def __init__(self, msg, id=None):
                Message.__init__(self, id)
                self.msg = msg

            @property
            def attrs(self):
                return {'msg': self.msg}

        srv_config = {'type': 'zmq',
                      'codec': 'jsonrpc',
                      'name': 'test_actor_announcer',
                      'announce': {
                          'server_uri': 'ipc://./test_asyncactor_srv.unix',
                          'pubsub_uri': 'ipc://./test_asyncactor_pub.unix'}
                      }
        srv = AnnounceServer(srv_config)
        srv.start()

        actor_config1 = srv_config.copy()
        actor_config1.update({'name': 'actor1',
                              'uri': 'ipc://./test_asyncactor_1.unix'
                              })
        actor_config2 = srv_config.copy()
        actor_config2.update({'name': 'actor2',
                              'uri': 'ipc://./test_asyncactor_2.unix'
                              })

        class Handler(object):

            def __init__(self):
                self.withresponse_actor = None
                self.withresponse_msg = None
                self.async_response = None

        hdl = Handler()

        @async
        def handler_withresponse(actor, msg):
            hdl.withresponse_actor = actor
            hdl.withresponse_msg = msg
            return DummyMessage("actor1 to actor2 response")

        def on_recv(msg):
            hdl.async_response = msg.msg

        actor1 = Actor(actor_config1, handler_withresponse)
        actor1.connect()

        actor2 = Actor(actor_config2, handler_withresponse)
        actor2.connect()

        # asyn method always return an ack
        dummy = DummyMessage('actor1 to actor2 request')
        response = actor1.sendrecv('actor2', dummy, on_recv)
        self.assertEquals(response.type, 'ack')
        self.assertEquals(hdl.withresponse_actor.name, 'actor2')

        actor2.sendrecv('actor1',
                        DummyMessage('actor2 async response', dummy.id))
        #gevent.sleep(0.) # force run in the next eventloop cycle
        gevent.sleep(1)

        self.assertEquals(hdl.async_response, 'actor2 async response')
        self.assertEquals(len(actor1._pendings), 0)

        self.assertEquals(len(srv._nodes), 2)
        actor1.close()
        actor2.close()
        self.assertEquals(len(srv._nodes), 0)
        srv.stop()
Пример #3
0
    def test_actor(self):
        import gevent
        from synapse.message import Message
        from synapse.node import AnnounceServer, Actor

        class DummyMessage(Message):
            type = 'dummy'

            def __init__(self, msg, id=None):
                Message.__init__(self, id)
                self.msg = msg

            @property
            def attrs(self):
                return {'msg': self.msg}

        srv_config = {'type': 'zmq',
                      'codec': 'jsonrpc',
                      'name': 'test_actor_announcer',
                      'announce': {'server_uri': 'ipc://./test_actor_srv.unix',
                                   'pubsub_uri': 'ipc://./test_actor_pub.unix'}
                      }
        srv = AnnounceServer(srv_config)
        srv.start()

        actor_config1 = srv_config.copy()
        actor_config1.update({'name': 'actor1',
                              'uri': 'ipc://./test_actor_1.unix'})
        actor_config2 = srv_config.copy()
        actor_config2.update({'name': 'actor2',
                              'uri': 'ipc://./test_actor_2.unix'})

        class Handler(object):

            def __init__(self):
                self.withresponse_actor = None
                self.withresponse_msg = None
                self.noresponse_actor = None
                self.noresponse_msg = None

            def handler_withresponse(self, actor, msg):
                self.withresponse_actor = actor
                self.withresponse_msg = msg
                return DummyMessage("actor1 to actor2 response")

            def handler_noresponse(self, actor, msg):
                self.noresponse_actor = actor
                self.noresponse_msg = msg
                #will return an ack if None return here

        hdl = Handler()

        actor1 = Actor(actor_config1, hdl.handler_withresponse)
        self.assertEquals(actor1.name, 'actor1')
        actor1.connect()

        actor2 = Actor(actor_config2, hdl.handler_noresponse)
        actor2.connect()

        dummy = DummyMessage('actor1 to actor2 request')
        response = actor1.sendrecv('actor2', dummy)
        self.assertEquals(response.type, 'ack')
        self.assertEquals(hdl.noresponse_msg.msg, 'actor1 to actor2 request')
        self.assertEquals(hdl.noresponse_actor.name, 'actor2')

        dummy = DummyMessage('actor2 to actor1 request')
        response = actor2.sendrecv('actor1', dummy)
        self.assertEquals(response.msg, "actor1 to actor2 response")
        self.assertEquals(response.type, 'dummy')

        self.assertEquals(hdl.withresponse_actor.name, 'actor1')
        self.assertEquals(hdl.withresponse_msg.msg, 'actor2 to actor1 request')

        self.assertEquals(len(srv._nodes), 2)
        actor1.__del__()
        actor2.close()
        self.assertEquals(len(srv._nodes), 0)
        srv.stop()
Пример #4
0
 def __init__(self, config):
     Actor.__init__(self, config)
     self.withresponse_actor = None
     self.withresponse_msg = None
Пример #5
0
    def test_actor_async(self):
        import gevent
        from synapse.message import Message
        from synapse.node import AnnounceServer, Actor, async

        class DummyMessage(Message):
            type = 'dummy'

            def __init__(self, msg, id=None):
                Message.__init__(self, id)
                self.msg = msg

            @property
            def attrs(self):
                return {'msg': self.msg}

        srv_config = {
            'type': 'zmq',
            'codec': 'jsonrpc',
            'name': 'test_actor_announcer',
            'announce': {
                'server_uri': 'ipc://./test_asyncactor_srv.unix',
                'pubsub_uri': 'ipc://./test_asyncactor_pub.unix'
            }
        }
        srv = AnnounceServer(srv_config)
        srv.start()

        actor_config1 = srv_config.copy()
        actor_config1.update({
            'name': 'actor1',
            'uri': 'ipc://./test_asyncactor_1.unix'
        })
        actor_config2 = srv_config.copy()
        actor_config2.update({
            'name': 'actor2',
            'uri': 'ipc://./test_asyncactor_2.unix'
        })

        class Handler(object):
            def __init__(self):
                self.withresponse_actor = None
                self.withresponse_msg = None
                self.async_response = None

        hdl = Handler()

        @async
        def handler_withresponse(actor, msg):
            hdl.withresponse_actor = actor
            hdl.withresponse_msg = msg
            return DummyMessage("actor1 to actor2 response")

        def on_recv(msg):
            hdl.async_response = msg.msg

        actor1 = Actor(actor_config1, handler_withresponse)
        actor1.connect()

        actor2 = Actor(actor_config2, handler_withresponse)
        actor2.connect()

        # asyn method always return an ack
        dummy = DummyMessage('actor1 to actor2 request')
        response = actor1.sendrecv('actor2', dummy, on_recv)
        self.assertEquals(response.type, 'ack')
        self.assertEquals(hdl.withresponse_actor.name, 'actor2')

        actor2.sendrecv('actor1',
                        DummyMessage('actor2 async response', dummy.id))
        #gevent.sleep(0.) # force run in the next eventloop cycle
        gevent.sleep(1)

        self.assertEquals(hdl.async_response, 'actor2 async response')
        self.assertEquals(len(actor1._pendings), 0)

        self.assertEquals(len(srv._nodes), 2)
        actor1.close()
        actor2.close()
        self.assertEquals(len(srv._nodes), 0)
        srv.stop()
Пример #6
0
    def test_actor(self):
        import gevent
        from synapse.message import Message
        from synapse.node import AnnounceServer, Actor

        class DummyMessage(Message):
            type = 'dummy'

            def __init__(self, msg, id=None):
                Message.__init__(self, id)
                self.msg = msg

            @property
            def attrs(self):
                return {'msg': self.msg}

        srv_config = {
            'type': 'zmq',
            'codec': 'jsonrpc',
            'name': 'test_actor_announcer',
            'announce': {
                'server_uri': 'ipc://./test_actor_srv.unix',
                'pubsub_uri': 'ipc://./test_actor_pub.unix'
            }
        }
        srv = AnnounceServer(srv_config)
        srv.start()

        actor_config1 = srv_config.copy()
        actor_config1.update({
            'name': 'actor1',
            'uri': 'ipc://./test_actor_1.unix'
        })
        actor_config2 = srv_config.copy()
        actor_config2.update({
            'name': 'actor2',
            'uri': 'ipc://./test_actor_2.unix'
        })

        class Handler(object):
            def __init__(self):
                self.withresponse_actor = None
                self.withresponse_msg = None
                self.noresponse_actor = None
                self.noresponse_msg = None

            def handler_withresponse(self, actor, msg):
                self.withresponse_actor = actor
                self.withresponse_msg = msg
                return DummyMessage("actor1 to actor2 response")

            def handler_noresponse(self, actor, msg):
                self.noresponse_actor = actor
                self.noresponse_msg = msg
                #will return an ack if None return here

        hdl = Handler()

        actor1 = Actor(actor_config1, hdl.handler_withresponse)
        self.assertEquals(actor1.name, 'actor1')
        actor1.connect()

        actor2 = Actor(actor_config2, hdl.handler_noresponse)
        actor2.connect()

        dummy = DummyMessage('actor1 to actor2 request')
        response = actor1.sendrecv('actor2', dummy)
        self.assertEquals(response.type, 'ack')
        self.assertEquals(hdl.noresponse_msg.msg, 'actor1 to actor2 request')
        self.assertEquals(hdl.noresponse_actor.name, 'actor2')

        dummy = DummyMessage('actor2 to actor1 request')
        response = actor2.sendrecv('actor1', dummy)
        self.assertEquals(response.msg, "actor1 to actor2 response")
        self.assertEquals(response.type, 'dummy')

        self.assertEquals(hdl.withresponse_actor.name, 'actor1')
        self.assertEquals(hdl.withresponse_msg.msg, 'actor2 to actor1 request')

        self.assertEquals(len(srv._nodes), 2)
        actor1.__del__()
        actor2.close()
        self.assertEquals(len(srv._nodes), 0)
        srv.stop()