def test_equivalence_after_round_trip(self): # 1. Given a string, parse it to get Avro protocol "original". # 2. Serialize "original" to a string and parse that string # to generate Avro protocol "round trip". # 3. Ensure "original" and "round trip" protocols are equivalent. print '' print 'TEST ROUND TRIP' print '===============' print '' num_correct = 0 for example in VALID_EXAMPLES: original_protocol = protocol.parse(example.protocol_string) round_trip_protocol = protocol.parse(str(original_protocol)) if original_protocol == round_trip_protocol: num_correct += 1 debug_msg = "%s: ROUND TRIP SUCCESS" % example.name else: self.fail("Round trip failure: %s %s %s", (example.name, example.protocol_string, str(original_protocol))) fail_msg = "Round trip success on %d out of %d protocols" % \ (num_correct, len(VALID_EXAMPLES)) self.assertEqual(num_correct, len(VALID_EXAMPLES), fail_msg)
def test_parse(self): num_correct = 0 for example in EXAMPLES: try: protocol.parse(example.protocol_string) if example.valid: num_correct += 1 else: self.fail("Parsed invalid protocol: %s" % (example.name, )) except Exception, e: if not example.valid: num_correct += 1 else: self.fail("Coudl not parse valid protocol: %s" % (example.name, ))
def process_handshake(self, decoder, encoder): handshake_request = HANDSHAKE_RESPONDER_READER.read(decoder) handshake_response = {} # determine the remote protocol client_hash = handshake_request.get('clientHash') client_protocol = handshake_request.get('clientProtocol') remote_protocol = self.get_protocol_cache(client_hash) if remote_protocol is None and client_protocol is not None: remote_protocol = protocol.parse(client_protocol) self.set_protocol_cache(client_hash, remote_protocol) # evaluate remote's guess of the local protocol server_hash = handshake_request.get('serverHash') if self.local_hash == server_hash: if remote_protocol is None: handshake_response['match'] = 'NONE' else: handshake_response['match'] = 'BOTH' else: if remote_protocol is None: handshake_response['match'] = 'NONE' else: handshake_response['match'] = 'CLIENT' if handshake_response['match'] != 'BOTH': handshake_response['serverProtocol'] = str(self.local_protocol) handshake_response['serverHash'] = self.local_hash HANDSHAKE_RESPONDER_WRITER.write(handshake_response, encoder) return remote_protocol
def test_inner_namespace_set(self): print '' print 'TEST INNER NAMESPACE' print '===================' print '' proto = protocol.parse(HELLO_WORLD.protocol_string) self.assertEqual(proto.namespace, "com.acme") greeting_type = proto.types_dict['Greeting'] self.assertEqual(greeting_type.namespace, 'com.acme')
def test_parse(self): num_correct = 0 for example in EXAMPLES: try: protocol.parse(example.protocol_string) if example.valid: num_correct += 1 else: self.fail("Parsed invalid protocol: %s" % (example.name, )) except Exception as e: if not example.valid: num_correct += 1 else: self.fail("Coudl not parse valid protocol: %s" % (example.name, )) fail_msg = "Parse behavior correct on %d out of %d protocols." % \ (num_correct, len(EXAMPLES)) self.assertEqual(num_correct, len(EXAMPLES), fail_msg)
def test_valid_cast_to_string_after_parse(self): # Test that the string generated by an Avro Protocol object # is, in fact, a valid Avro protocol. print '' print 'TEST CAST TO STRING' print '===================' print '' num_correct = 0 for example in VALID_EXAMPLES: protocol_data = protocol.parse(example.protocol_string) try: try: protocol.parse(str(protocol_data)) debug_msg = "%s: STRING CAST SUCCESS" % example.name num_correct += 1 except: debug_msg = "%s: STRING CAST FAILURE" % example.name finally: print debug_msg fail_msg = "Cast to string success on %d out of %d protocols" % \ (num_correct, len(VALID_EXAMPLES)) self.assertEqual(num_correct, len(VALID_EXAMPLES), fail_msg)
def read_handshake_response(self, decoder): handshake_response = HANDSHAKE_REQUESTOR_READER.read(decoder) match = handshake_response.get('match') if match == 'BOTH': self.send_protocol = False return True elif match == 'CLIENT': if self.send_protocol: raise schema.AvroException('Handshake failure.') self.remote_protocol = protocol.parse( handshake_response.get('serverProtocol')) self.remote_hash = handshake_response.get('serverHash') self.send_protocol = False return True elif match == 'NONE': if self.send_protocol: raise schema.AvroException('Handshake failure.') self.remote_protocol = protocol.parse( handshake_response.get('serverProtocol')) self.remote_hash = handshake_response.get('serverHash') self.send_protocol = True return False else: raise schema.AvroException('Unexpected match: %s' % match)
inputProtocol = None outputProtocol = None TaskType = None if (inputProtocol is None): pfile = os.path.split(__file__)[0] + os.sep + "InputProtocol.avpr" if not (os.path.exists(pfile)): raise Exception( "Could not locate the InputProtocol: {0} does not exist".format( pfile)) with file(pfile, 'r') as hf: prototxt = hf.read() inputProtocol = protocol.parse(prototxt) # use a named tuple to represent the tasktype enumeration taskschema = inputProtocol.types_dict["TaskType"] _ttype = collections.namedtuple("_tasktype", taskschema.symbols) TaskType = _ttype(*taskschema.symbols) if (outputProtocol is None): pfile = os.path.split(__file__)[0] + os.sep + "OutputProtocol.avpr" if not (os.path.exists(pfile)): raise Exception( "Could not locate the OutputProtocol: {0} does not exist".format( pfile)) with file(pfile, 'r') as hf:
} ], "messages": { "send": { "request": [{"name": "message", "type": "Message"}], "response": "string" }, "replay": { "request": [], "response": "string" } } } """ MAIL_PROTOCOL = protocol.parse(MAIL_PROTOCOL_JSON) SERVER_HOST = 'localhost' SERVER_PORT = 9090 class UsageError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) def make_requestor(server_host, server_port, protocol): client = txipc.TwistedHTTPTransceiver(SERVER_HOST, SERVER_PORT) return txipc.TwistedRequestor(protocol, client)
def test_inner_namespace_not_rendered(self): proto = protocol.parse(HELLO_WORLD.protocol_string) self.assertEqual('com.acme.Greeting', proto.types[0].fullname) self.assertEqual('Greeting', proto.types[0].name) # but there shouldn't be 'namespace' rendered to json on the inner type self.assertFalse('namespace' in proto.to_json()['types'][0])
def __init__(self, proto, msg, datum): proto_json = file(proto, 'r').read() ipc.Responder.__init__(self, protocol.parse(proto_json)) self.msg = msg self.datum = datum
def send_message(uri, proto, msg, datum): url_obj = urlparse.urlparse(uri) client = ipc.HTTPTransceiver(url_obj.hostname, url_obj.port) proto_json = file(proto, 'r').read() requestor = ipc.Requestor(protocol.parse(proto_json), client) print requestor.request(msg, datum)