def testreadfiles(self): origschm = schema.parse(open("src/test/schemata/interop.avsc").read()) for file in os.listdir(_DATAFILE_DIR): print "Validating:", file.__str__() dr = io.DataFileReader(open(_DATAFILE_DIR + file, "rb"), self.__datumreader()) count = int(dr.getmeta("count")) decodedSchm = schema.parse(dr.getmeta("schema")) self.assertEquals(origschm, decodedSchm) for i in range(0, count): datum = dr.next() self.assertTrue(self.__validator(origschm, datum)) # validate reading of blocking arrays, blocking maps for file in os.listdir(_BLOCKINGFILE_DIR): print "Validating:", file.__str__() reader = open(_BLOCKINGFILE_DIR + file, "rb") decoder = io.Decoder(reader) dreader = self.__datumreader() dreader.setschema(origschm) count = int( decoder.readlong()) #metadata:the count of objects in the file blockcount = decoder.readlong() for i in range(0, count): while blockcount == 0: blockcount = decoder.readlong() blockcount -= 1 datum = dreader.read(decoder) self.assertTrue(self.__validator(origschm, datum))
def request(self, msgname, req): """Writes a request message and reads a response or error message.""" processed = False while not processed: buf = cStringIO.StringIO() encoder = io.Encoder(buf) self.__writehandshake(encoder) requestmeta = dict() _META_WRITER.write(requestmeta, encoder) m = self.__localproto.getmessages().get(msgname) if m is None: raise schema.AvroException("Not a local message: " + msgname.__str__()) encoder.writeutf8(m.getname()) self.writerequest(m.getrequest(), req, encoder) response = self.__transceiver.transceive(buf.getvalue()) decoder = io.Decoder(cStringIO.StringIO(response)) processed = self.__readhandshake(decoder) responsemeta = _META_READER.read(decoder) m = self.getremote().getmessages().get(msgname) if m is None: raise schema.AvroException("Not a remote message: " + msgname.__str__()) if not decoder.readboolean(): return self.readresponse(m.getresponse(), decoder) else: raise self.readerror(m.geterrors(), decoder)
def checkser(self, schm, randomdata): datum = randomdata.next() self.assertTrue(self.__validator(schm, datum)) w = self.__datumwriter(schm) writer = cStringIO.StringIO() w.write(datum, io.Encoder(writer)) r = self.__datumreader(schm) reader = cStringIO.StringIO(writer.getvalue()) ob = r.read(io.Decoder(reader)) if self.__assertdata: self.assertEquals(datum, ob)
def checkdefault(self, schemajson, defaultjson, defaultvalue): self.check(schemajson) actual = schema.parse("{\"type\":\"record\", \"name\":\"Foo\"," + "\"fields\":[]}") expected = schema.parse("{\"type\":\"record\", \"name\":\"Foo\"," + "\"fields\":[{\"name\":\"f\", " + "\"type\":" + schemajson + ", " + "\"default\":" + defaultjson + "}]}") reader = genericio.DatumReader(actual, expected) record = reader.read(io.Decoder(cStringIO.StringIO())) self.assertEquals(defaultvalue, record.get("f"))
def respond(self, transceiver): """Called by a server to deserialize a request, compute and serialize * a response or error.""" transreq = transceiver.readbuffers() reader = cStringIO.StringIO(transreq) decoder = io.Decoder(reader) buf = cStringIO.StringIO() encoder = io.Encoder(buf) error = None responsemeta = dict() try: remoteproto = self.__handshake(transceiver, decoder, encoder) if remoteproto is None: #handshake failed return buf.getvalue() #read request using remote protocol specification requestmeta = _META_READER.read(decoder) msgname = decoder.readutf8() m = remoteproto.getmessages().get(msgname) if m is None: raise schema.AvroException("No such remote message: " + msgname.__str__()) req = self.readrequest(m.getrequest(), decoder) #read response using local protocol specification m = self.__localproto.getmessages().get(msgname) if m is None: raise schema.AvroException("No such local message: " + msgname.__str__()) try: response = self.invoke(m, req) except AvroRemoteException, e: error = e except Exception, e: error = AvroRemoteException(unicode(e.__str__()))