Example #1
0
    def test_makeCodec(self):
        from synapse.message import (makeCodec, MessageCodec, Message,
                                     MessageCodecJSONRPC, CodecException,
                                     CodecInvalidException
                                     )
        m = MessageCodec()
        self.assertRaises(NotImplementedError, m.loads, '')
        self.assertRaises(NotImplementedError, m.dumps, {})

        codec = makeCodec({'type': 'jsonrpc'})
        self.assertTrue(isinstance(codec, MessageCodecJSONRPC))

        class GoodMessage(Message):
            type = 'test_serialize'

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

            @property
            def attrs(self):
                return {'dummystr': self.dummystr,
                        'dummyint': self.dummyint
                        }

        class BadMessage:
            type = 'not_a_msg'

        m = GoodMessage("dummy", 1)
        s = codec.dumps(m)
        m2 = codec.loads(s)
        self.assertTrue(isinstance(m2, GoodMessage))
        self.assertEquals(m.dummystr, m2.dummystr)
        self.assertEquals(m.dummyint, m.dummyint)

        class BadMessage:
            pass

        m = BadMessage()
        self.assertRaises(CodecException, codec.dumps, m)

        self.assertRaises(CodecException, codec.loads, "this is not a msg")
        self.assertRaises(CodecInvalidException, makeCodec,
                          {'type': 'unsupported'})
        try:
            makeCodec({'type': 'unsupported'})
        except CodecInvalidException, e:
            self.assertEquals(str(e), "codec 'unsupported' is not supported")
Example #2
0
    def test_makeCodec(self):
        from synapse.message import (makeCodec, MessageCodec, Message,
                                     MessageCodecJSONRPC, CodecException,
                                     CodecInvalidException
                                     )
        m = MessageCodec()
        self.assertRaises(NotImplementedError, m.loads, '')
        self.assertRaises(NotImplementedError, m.dumps, {})

        codec = makeCodec({'type': 'jsonrpc'})
        self.assertTrue(isinstance(codec, MessageCodecJSONRPC))

        class GoodMessage(Message):
            type = 'test_serialize'

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

            @property
            def attrs(self):
                return {'dummystr': self.dummystr,
                        'dummyint': self.dummyint
                        }

        class BadMessage:
            type = 'not_a_msg'

        m = GoodMessage("dummy", 1)
        s = codec.dumps(m)
        m2 = codec.loads(s)
        self.assertTrue(isinstance(m2, GoodMessage))
        self.assertEquals(m.dummystr, m2.dummystr)
        self.assertEquals(m.dummyint, m.dummyint)

        class BadMessage:
            pass

        m = BadMessage()
        self.assertRaises(CodecException, codec.dumps, m)

        self.assertRaises(CodecException, codec.loads, "this is not a msg")
        self.assertRaises(CodecInvalidException, makeCodec,
                          {'type': 'unsupported'})
        try:
            makeCodec({'type': 'unsupported'})
        except CodecInvalidException, e:
            self.assertEquals(str(e), "codec 'unsupported' is not supported")
Example #3
0
    def _test_json_datetime(self, date):
        msg = message.makeMessage({
                'type': 'reply',
                'src': 'tester',
                'data': date,
                'id': 0})
        codec = message.makeCodec({'type': 'jsonrpc'})
        msgstring = codec.dumps(msg)
        json_msg = json.loads(msgstring)

        self.assertEqual(date.isoformat(), json_msg['params']['data'])
Example #4
0
def seed(msg=None):
    common_config = yaml.load(file('config.yaml'))
    client = node.makeNode({'type': common_config['type'], 'uri': actor_config1['uri'], 'role': 'client'})
    if not msg:
        codec = message.makeCodec({'type': 'jsonrpc'})
        msg = codec.dumps(message.makeMessage({'type': 'test'}))
    client.connect()
    logging.debug('seed: connected to forwarder node')
    client.send(msg)
    logging.debug('seed: message sent')
    logging.debug('seed: message received (%s)' % client.recv())
Example #5
0
    def test_json_xmlrpclib_datetime(self):
        import xmlrpclib

        xmlrpc_date = xmlrpclib.DateTime()
        msg = message.makeMessage({
                'type': 'reply',
                'src': 'tester',
                'data': xmlrpc_date,
                'id': 0})
        codec = message.makeCodec({'type': 'jsonrpc'})
        msgstring = codec.dumps(msg)
        """
Example #6
0
    def _test_json_datetime(self, date):
        import json
        from synapse import message
        msg = message.makeMessage({
                'type': 'reply',
                'src': 'tester',
                'data': date,
                'id': 0})
        codec = message.makeCodec({'type': 'jsonrpc'})
        msgstring = codec.dumps(msg)
        json_msg = json.loads(msgstring)

        self.assertEqual(date.isoformat(), json_msg['params']['data'])
Example #7
0
    def test_json_xmlrpclib_datetime(self):
        import xmlrpclib

        xmlrpc_date = xmlrpclib.DateTime()
        msg = message.makeMessage({
                'type': 'reply',
                'src': 'tester',
                'data': xmlrpc_date,
                'id': 0})
        codec = message.makeCodec({'type': 'jsonrpc'})
        msgstring = codec.dumps(msg)
        json_msg = json.loads(msgstring)
        date = datetime.datetime.strptime(xmlrpc_date.value, "%Y%m%dT%H:%M:%S")

        self.assertEqual(date.isoformat(), json_msg['params']['data'])
Example #8
0
 def __init__(self, config, handler=None):
     self._uri = config['uri']
     self._name = config['name']
     self._codec = makeCodec({'type': config['codec']})
     self._mailbox = makeNode(
         {
             'name': config['name'],
             'type': config['type'],
             'uri': self._uri,
             'role': config.get('role', 'server')
         }, self.on_message)
     config['type'] = 'zmq'
     self._announce = AnnounceClient(config, self.on_message)
     self._nodes = NodeDirectory(config, self._announce)
     self._handler = handler if handler else getattr(
         self, 'handle_message', None)
     self._pendings = {}
     self._log = logging.getLogger(self.name)
Example #9
0
    def test_json_xmlrpclib_datetime(self):
        import xmlrpclib
        import json
        from datetime import datetime
        from synapse import message

        xmlrpc_date = xmlrpclib.DateTime()
        msg = message.makeMessage({
                'type': 'reply',
                'src': 'tester',
                'data': xmlrpc_date,
                'id': 0})
        codec = message.makeCodec({'type': 'jsonrpc'})
        msgstring = codec.dumps(msg)

        json_msg = json.loads(msgstring)
        date = datetime.strptime(xmlrpc_date.value, "%Y%m%dT%H:%M:%S")

        self.assertEqual(date.isoformat(), json_msg['params']['data'])
Example #10
0
    def __init__(self, config):
        self.name = 'announce.server'
        self._codec = makeCodec({'type': config['codec']})
        self._server = makeNode(
            {
                'name': self.name,
                'type': config['type'],
                'uri': config['announce']['server_uri'],
                'role': 'server'
            }, self.handle_message)
        self._publisher = makeNode({
            'name': 'announce.publisher',
            'type': config['type'],
            'uri': config['announce']['pubsub_uri'],
            'role': 'publish'
        })

        self._nodes = {}  # NodeDirectory(config)
        self._log = logging.getLogger(self.name)
Example #11
0
 def __init__(self, config, handler=None):
     self.name = '%s.announce' % config['name']
     self._codec = makeCodec({'type': config['codec']})
     self._nodes = []
     self._client = makeNode({
         'name': self.name,
         'type': config['type'],
         'uri': config['announce']['server_uri'],
         'role': 'client'
     })
     self._nodes.append(self._client)
     self._subscriber = makeNode(
         {
             'name': '%s.subscribe' % config['name'],
             'type': config['type'],
             'uri': config['announce']['pubsub_uri'],
             'role': 'subscribe',
         }, self.handle_announce) if handler else None
     if self._subscriber:
         self._nodes.append(self._client)
     self._handler = handler
     self._log = logging.getLogger(self.name)
     self.connected = False
Example #12
0
 class DummyActor(object):
     name = 'dummy'
     _codec = makeCodec({'type': 'jsonrpc'})