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")
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'])
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())
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) """
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'])
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'])
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)
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'])
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)
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
class DummyActor(object): name = 'dummy' _codec = makeCodec({'type': 'jsonrpc'})