def test_caching(self): self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) # XXX A HACK WHILE WE CHANGE INTERFACE ------------ msg_spec = self.str_obj_model.msgs[0] name = msg_spec.name cls0 = make_msg_class(self.str_obj_model, name) # DEBUG print("Constructed Clz0 name is '%s'" % cls0.name) # END self.assertEqual(name, cls0.name) cls1 = make_msg_class(self.str_obj_model, name) self.assertEqual(name, cls1.name) # END HACK ---------------------------------------- # we cache classe, so the two should be the same self.assertEqual(id(cls0), id(cls1)) # chan = Channel(BUFSIZE) values = self.lil_big_msg_values() lil_big_msg0 = cls0(values) lil_big_msg1 = cls0(values) # we don't cache instances, so these will differ self.assertNotEqual(id(lil_big_msg0), id(lil_big_msg1)) field_spec = msg_spec[0] dotted_name = "%s.%s" % (self.proto_name, msg_spec.name) f0cls = make_field_class(dotted_name, field_spec) f1cls = make_field_class(dotted_name, field_spec) self.assertEqual(id(f0cls), id(f1cls))
def test_caching(self): # SETUP data = StringIO(ZOGGERY_PROTO_SPEC) ppp = StringProtoSpecParser(data) # data should be file-like self.str_obj_model = ppp.parse() # object model from string serialization self.proto_name = self.str_obj_model.name # the dotted name of the protocol # END SETUp self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) msg_spec = self.str_obj_model.msgs[0] name = msg_spec.name cls0 = make_msg_class(self.str_obj_model, name) cls1 = make_msg_class(self.str_obj_model, name) # we cache classe, so the two should be the same self.assertEqual(id(cls0), id(cls1)) # chan = Channel(BUFSIZE) values = self.le_msg_values() le_msg0 = cls0(values) le_msg1 = cls0(values) # we don't cache instances, so these will differ self.assertNotEqual(id(le_msg0), id(le_msg1)) field_spec = msg_spec[0] dotted_name = "%s.%s" % (self.proto_name, msg_spec.name) f0cls = make_field_class(dotted_name, field_spec) f1cls = make_field_class(dotted_name, field_spec) self.assertEqual(id(f0cls), id(f1cls))
def test_ring_data_proto_serialization(self): str_obj_model = self.make_str_obj_model() proto_name = str_obj_model.name outer_msg_spec = str_obj_model.msgs[0] inner_msg_spec = str_obj_model.msgs[0].msgs[0] outer_msg_cls = make_msg_class(str_obj_model, outer_msg_spec.name) # NOTE change in parent inner_msg_cls = make_msg_class(outer_msg_spec, inner_msg_spec.name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create a message instance --------------------------------- # create some HostInfo instances count = 2 + RNG.next_int16(7) # so 2 .. 8 ring = [] for nnn in range(count): # should avoid dupes values = host_info_values() ring.append(inner_msg_cls(values)) outer_msg = outer_msg_cls([ring]) # a list whose member is a list # serialize the object to the channel ----------------------- nnn = outer_msg.write_stand_alone(chan) old_position = chan.position chan.flip() self.assertEqual(old_position, chan.limit) self.assertEqual(0, chan.position) # deserialize the channel, making a clone of the message ---- (read_back, nn2) = outer_msg_cls.read(chan, str_obj_model) self.assertIsNotNone(read_back) # verify that the messages are identical -------------------- self.assertTrue(outer_msg.__eq__(read_back)) self.assertEqual(nnn, nn2) # produce another message from the same values -------------- outer_msg2 = outer_msg_cls([ring]) chan2 = Channel(BUFSIZE) nnn = outer_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = outer_msg_cls.read(chan2, str_obj_model) self.assertTrue(outer_msg.__eq__(copy2)) self.assertTrue(outer_msg2.__eq__(copy2)) # GEEP
def test_caching(self): self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) msg_spec = self.str_obj_model.msgs[0] name = msg_spec.name cls0 = make_msg_class(self.str_obj_model, name) cls1 = make_msg_class(self.str_obj_model, name) # we cache classe, so the two should be the same self.assertEqual(id(cls0), id(cls1)) # chan = Channel(BUFSIZE) values = self.le_msg_values() le_msg0 = cls0(values) le_msg1 = cls0(values) # we don't cache instances, so these will differ self.assertNotEqual(id(le_msg0), id(le_msg1)) field_spec = msg_spec[0] dotted_name = "%s.%s" % (self.proto_name, msg_spec.name) f0cls = make_field_class(dotted_name, field_spec) f1cls = make_field_class(dotted_name, field_spec) self.assertEqual(id(f0cls), id(f1cls)) # GEEP
def test_caching(self): """ verify that classes with the same definition are cached """ str_obj_model = self.make_str_obj_model() proto_name = str_obj_model.name self.assertTrue(isinstance(str_obj_model, M.ProtoSpec)) outer_msg_spec = str_obj_model.msgs[0] inner_msg_spec = str_obj_model.msgs[0].msgs[0] outer_msg_cls = make_msg_class(str_obj_model, outer_msg_spec.name) # NOTE change in parent inner_msg_cls = make_msg_class(outer_msg_spec, inner_msg_spec.name) # TEST INNER MESSAGE ######################################## cls0 = make_msg_class(outer_msg_spec, inner_msg_spec.name) cls1 = make_msg_class(outer_msg_spec, inner_msg_spec.name) # we cache classes, so the two should be the same self.assertEqual(id(cls0), id(cls1)) # test that msg instances created from the same value lists differ values = host_info_values() inner_msg0 = cls0(values) inner_msg1 = cls0(values) # we don't cache instances, so these will differ self.assertNotEqual(id(inner_msg0), id(inner_msg1)) # verify that field classes are cached field_spec = inner_msg_spec[0] dotted_name = '%s.%s' % (proto_name, inner_msg_spec.name) f0cls = make_field_class(dotted_name, field_spec) f1cls = make_field_class(dotted_name, field_spec) self.assertEqual(id(f0cls), id(f1cls)) # GEEP # TEST OUTER MESSAGE ######################################## cls2 = make_msg_class(str_obj_model, outer_msg_spec.name) cls3 = make_msg_class(str_obj_model, outer_msg_spec.name) # we cache classe, so the two should be the same self.assertEqual(id(cls2), id(cls3)) # test that msg instances created from the same value lists differ ring = ring_data_values() # a list of random hosts # 'values' is a list of field values. In this case, the single # value is itself a list, a list of HostInfo value lists. values = [ring] # a list whose only member is a list outer_msg0 = cls2(values) outer_msg1 = cls2(values) # we don't cache instances, so these will differ self.assertNotEqual(id(outer_msg0), id(outer_msg1)) field_spec = outer_msg_spec[0] dotted_name = '%s.%s' % (proto_name, outer_msg_spec.name) f0cls = make_field_class(dotted_name, field_spec) f1cls = make_field_class(dotted_name, field_spec) self.assertEqual(id(f0cls), id(f1cls)) # GEEP
'ZONE_MISMATCH_MSG', 'CORRUPT_LIST_MSG', 'SHUTDOWN_MSG', ] __version__ = '0.2.20' __version_date__ = '2018-03-11' BUFSIZE = 16 * 1024 # must allow for all using protocols # SYNTACTIC MACHINERY ----------------------------------------------- PROTO_TEXT = StringIO(ALERTZ_PROTO_SPEC) # file-like STR_PS_PARSER = StringProtoSpecParser(PROTO_TEXT) # object model from string serialization STR_OBJ_MODEL = STR_PS_PARSER.parse() # the dotted name of the protocol PROTO_NAME = STR_OBJ_MODEL.name ZONE_MISMATCH_MSG = make_msg_class(STR_OBJ_MODEL, 'zoneMismatch') CORRUPT_LIST_MSG = make_msg_class(STR_OBJ_MODEL, 'corruptList') SHUTDOWN_MSG = make_msg_class(STR_OBJ_MODEL, 'shutdown') # the maximum number of bytes in a message ALERTZ_MAX_MSG = 512 ALERTZ_PORT = 55555 # -- NAME SPACE ----------------------------------------------------- # code.activestate.com/recipes/577887-a-simple-namespace-class offers # a seemingly neat solution, but it just doesn't work class Namespace(dict): def __init__(self, pairs=None):
] __version__ = '0.2.12' __version_date__ = '2016-11-08' BUFSIZE = 16 * 1024 # must allow for all using protocols # SYNTACTIC MACHINERY ----------------------------------------------- PROTO_TEXT = StringIO(ALERTZ_PROTO_SPEC) # file-like STR_PS_PARSER = StringProtoSpecParser(PROTO_TEXT) # object model from string serialization STR_OBJ_MODEL = STR_PS_PARSER.parse() # the dotted name of the protocol PROTO_NAME = STR_OBJ_MODEL.name ZONE_MISMATCH_MSG = make_msg_class(STR_OBJ_MODEL, 'zoneMismatch') CORRUPT_LIST_MSG = make_msg_class(STR_OBJ_MODEL, 'corruptList') SHUTDOWN_MSG = make_msg_class(STR_OBJ_MODEL, 'shutdown') # the maximum number of bytes in a message ALERTZ_MAX_MSG = 512 ALERTZ_PORT = 55555 # -- NAME SPACE ----------------------------------------------------- # code.activestate.com/recipes/577887-a-simple-namespace-class offers # a seemingly neat solution, but it just doesn't work class Namespace(dict): def __init__(self, pairs={}):
def test_zoggery_serialization(self): # XXX These comments are generally out of date. # Testing MsgSpec with simple fields. Verify that getter, # putter, lenFunc, and pLenFunc work for the basic types # (ie, that they are correctly imported into this reg) and # they are work for the newly defined single-msg protoSpec. # Use ZOGGERY_PROTO_SPEC # parse the protoSpec # verify that this adds 1 (msg) + 5 (field count) to the number # of entries in getters, putters, etc self.assertIsNotNone(self.str_obj_model) self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.zoggery', self.str_obj_model.name) self.assertEqual(len(self.str_obj_model.enums), 0) self.assertEqual(len(self.str_obj_model.msgs), 1) self.assertEqual(len(self.str_obj_model.seqs), 0) msg_spec = self.str_obj_model.msgs[0] msg_name = msg_spec.name self.assertEqual('logEntry', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the LogEntryMsg class ------------------------------ log_entry_msg_cls = make_msg_class(self.str_obj_model, msg_name) # DEBUG print("testZoggery: LogEntryMsg is of type ", type(log_entry_msg_cls)) # END # create a message instance --------------------------------- values = self.le_msg_values() # a list of quasi-random values le_msg = log_entry_msg_cls(values) # DEBUG print("type of LogEntryMsg: ", type(log_entry_msg_cls)) print("type of leMsg: ", type(le_msg)) # END self.assertTrue(isinstance(le_msg, log_entry_msg_cls)) (timestamp, key, length, node_id, by_, path) = tuple(values) # F A I L S: # msg_spec.name is 'logEntry' # le_msg._name is '[148639516, [227, 217, ...[230 chars].gz'] self.assertEqual(msg_spec.name, le_msg._name) # we don't have any nested enums or messages # XXX FAIL: properties have no len() # self.assertEqual(0, len(leMsg.enums)) # DEBUG print("leMsg.enums: ", le_msg.enums) # END # self.assertEqual(0, len(leMsg.msgs)) self.assertEqual(6, len(le_msg.field_classes)) self.assertEqual(6, len(le_msg)) # number of fields in instance for i in range(len(le_msg)): # DEBUG print("value %d is %s" % (i, values[i])) # FAILS: is a PROPERTY OBJECT print("leMsg %d is %s" % (i, le_msg[i].value)) # END self.assertEqual(values[i], le_msg[i].value) # FAILS # verify fields are accessible in the object ---------------- (timestamp, node_id, key, length, by_, path) = tuple(values) self.assertEqual(timestamp, le_msg.timestamp) self.assertEqual(node_id, le_msg.node_id) self.assertEqual(key, le_msg.key) self.assertEqual(length, le_msg.length) self.assertEqual(by_, le_msg.by_) self.assertEqual(path, le_msg.path) # serialize the object to the channel ----------------------- buf = chan.buffer chan.clear() nnn = le_msg.write_stand_alone(chan) self.assertEqual(0, nnn) old_position = chan.position # TESTING flip() chan.flip() self.assertEqual(old_position, chan.limit) # TESTING flip() self.assertEqual(0, chan.position) # TESTING flip() actual = chan.limit print("ACTUAL LENGTH OF SERIALIZED OBJECT: %u" % actual) # deserialize the channel, making a clone of the message ---- (read_back, nn2) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) self.assertTrue(le_msg.__eq__(read_back)) # produce another message from the same values -------------- le_msg2 = log_entry_msg_cls(values) chan2 = Channel(BUFSIZE) nnn = le_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = log_entry_msg_cls.read(chan2, self.str_obj_model) self.assertTrue(le_msg.__eq__(read_back)) self.assertTrue(le_msg2.__eq__(copy2)) self.assertEqual(nnn, nn3)
def test_little_big(self): self.assertIsNotNone(self.str_obj_model) self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.fieldz.test.littleBigProto', self.str_obj_model.name) self.assertEqual(0, len(self.str_obj_model.enums)) self.assertEqual(1, len(self.str_obj_model.msgs)) self.assertEqual(0, len(self.str_obj_model.seqs)) msg_spec = self.str_obj_model.msgs[0] # Create a channel ------------------------------------------ # its buffer will be used for both serializing the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the LittleBigMsg class ------------------------------ little_big_msg_cls = make_msg_class(self.str_obj_model, msg_spec.name) # ------------------------------------------------------------- # XXX the following fails because field 2 is seen as a property # instead of a list if False: # DEBUGGING print('\nLittleBigMsg CLASS DICTIONARY') for (ndx, key) in enumerate(little_big_msg_cls.__dict__.keys()): print( "%3u: %-20s %s" % (ndx, key, little_big_msg_cls.__dict__[key])) # ------------------------------------------------------------- # create a message instance --------------------------------- values = self.lil_big_msg_values() # quasi-random values lil_big_msg = little_big_msg_cls(values) # __setattr__ in MetaMsg raises exception on any attempt # to add new attributes. This works at the class level but # NOT at the instance level # if True: try: lil_big_msg.foo = 42 self.fail( "ERROR: attempt to assign new instance attribute succeeded") except AttributeError as a_exc: # DEBUG print( "ATTR ERROR ATTEMPTING TO SET lilBigMsg.foo: " + str(a_exc)) # END pass if '__dict__' in dir(lil_big_msg): print('\nlilBigMsg INSTANCE DICTIONARY') for exc in list(lil_big_msg.__dict__.keys()): print("%-20s %s" % (exc, lil_big_msg.__dict__[exc])) # lilBigMsg.name is a property try: lil_big_msg.name = 'boo' self.fail("ERROR: attempt to change message name succeeded") except AttributeError: pass self.assertEqual(msg_spec.name, lil_big_msg.name) # we don't have any nested enums or messages self.assertEqual(0, len(lil_big_msg.enums)) self.assertEqual(0, len(lil_big_msg.msgs)) self.assertEqual(17, len(lil_big_msg.field_classes)) # number of fields in instance self.assertEqual(17, len(lil_big_msg)) for i in range(len(lil_big_msg)): self.assertEqual(values[i], lil_big_msg[i].value) # serialize the object to the channel ----------------------- print("\nDEBUG: PHASE A ######################################") nnn = lil_big_msg.write_stand_alone(chan) old_position = chan.position chan.flip() self.assertEqual(old_position, chan.limit) self.assertEqual(0, chan.position) # deserialize the channel, making a clone of the message ---- (read_back, nn2) = little_big_msg_cls.read( chan, self.str_obj_model) # sOM is protoSpec self.assertIsNotNone(read_back) self.assertEqual(nnn, nn2) # verify that the messages are identical -------------------- self.assertTrue(lil_big_msg.__eq__(read_back)) print("\nDEBUG: PHASE B ######################################") # produce another message from the same values -------------- lil_big_msg2 = little_big_msg_cls(values) chan2 = Channel(BUFSIZE) nnn = lil_big_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = little_big_msg_cls.read(chan2, self.str_obj_model) self.assertIsNotNone(copy2) self.assertEqual(nnn, nn3) self.assertTrue(lil_big_msg.__eq__(copy2)) self.assertTrue(lil_big_msg2.__eq__(copy2)) # test clear() chan2.position = 97 chan2.limit = 107 chan2.clear() self.assertEqual(0, chan2.limit) self.assertEqual(0, chan2.position)
def test_msg_impl(self): self.assertIsNotNone(self.str_obj_model) self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.zoggery', self.str_obj_model.name) self.assertEqual(0, len(self.str_obj_model.enums)) self.assertEqual(1, len(self.str_obj_model.msgs)) self.assertEqual(0, len(self.str_obj_model.seqs)) msg_spec = self.str_obj_model.msgs[0] # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the LogEntryMsg class ------------------------------ log_entry_msg_cls = make_msg_class(self.str_obj_model, msg_spec.name) # __setattr__ in MetaMsg raises exception on any attempt # to add new attributes # TEST TEMPORARILY DISABLED # try: # LogEntryMsg.foo = 42 # self.fail( # "ERROR: attempt to assign new class attribute succeeded") # except AttributeError as ae: # # # DEBUG # print( # "success: attr error attempting to set LogEntryMsg.foo: " + # str(ae)) # # END # pass # GEEP # create a message instance --------------------------------- values = self.le_msg_values() # quasi-random values le_msg = log_entry_msg_cls(values) # __setattr__ in MetaMsg raises exception on any attempt # to add new attributes. This works at the class level but # NOT at the instance level if False: try: le_msg.foo = 42 self.fail( "ERROR: attempt to assign new instance attribute succeeded") except AttributeError as a_exc: # DEBUG print("ATTR ERROR ATTEMPTING TO SET leMsg.foo: " + str(a_exc)) # END # pass # leMsg._name is a property # TEST TEMPORARILY DISABLED # try: # leMsg._name = 'boo' # self.fail("ERROR: attempt to change message name succeeded") # except AttributeError: # pass self.assertEqual(msg_spec.name, le_msg._name) # we don't have any nested enums or messages self.assertEqual(0, len(le_msg.enums)) self.assertEqual(0, len(le_msg.msgs)) self.assertEqual(6, len(le_msg.field_classes)) self.assertEqual(6, len(le_msg)) # number of fields in instance # TEST TEMPORARILY DISABLED # for i in range(len(leMsg)): # self.assertEqual(values[i], leMsg[i].value) ################################ # XXX FIELDS ARE NOT AS EXPECTED ################################ # verify fields are accessible in the object ---------------- # DEBUG for field in le_msg._fieldClasses: print("FIELD: %s = %s " % (field.name, field.value)) # END (timestamp, node_id, key, length, by_, path) = tuple(values) # FAILS: null timestamp self.assertEqual(timestamp, le_msg.timestamp) self.assertEqual(node_id, le_msg.node_id) self.assertEqual(key, le_msg.key) self.assertEqual(length, le_msg.length) self.assertEqual(by_, le_msg.by_) self.assertEqual(path, le_msg.path) # serialize the object to the channel ----------------------- # XXX not a public method expected_msg_len = le_msg._wire_len() print("EXPECTED LENGTH OF SERIALIZED OBJECT: %u" % expected_msg_len) buf = chan.buffer chan.clear() nnn = le_msg.write_stand_alone(chan) # n is class index old_position = chan.position # TESTING flip() chan.flip() self.assertEqual(old_position, chan.limit) # TESTING flip() self.assertEqual(0, chan.position) # TESTING flip() actual = chan.limit # deserialize the channel, making a clone of the message ---- # XXX FAILS BECAUSE HEADER IS PRESENT: #(readBack,n2) = LogEntryMsg.read(chan, self.sOM) (read_back, nn2) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) self.assertTrue(le_msg.__eq__(read_back)) self.assertEqual(nnn, nn2) # produce another message from the same values -------------- le_msg2 = log_entry_msg_cls(values) chan2 = Channel(BUFSIZE) nnn = le_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = log_entry_msg_cls.read(chan2, self.str_obj_model) self.assertTrue(le_msg.__eq__(copy2)) self.assertTrue(le_msg2.__eq__(copy2))
def test_zone_mismatch_msg(self): # DEBUG print("\nTEST_ZONE_MISMATCH_MSG") # END # from setUp(): ============================================= # end stuff from setup ====================================== # ----------------------------------------------------------- # XXX This code has been crudely hacked from another test # module, and so needs careful review # ----------------------------------------------------------- # verify that this adds 1 (msg) + 3 (field count) to the number # of entries in getters, putters, etc self.assertIsNotNone(self.str_obj_model) self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.alertz', self.str_obj_model.name) self.assertEqual(0, len(self.str_obj_model.enums)) self.assertEqual(16, len(self.str_obj_model.msgs)) self.assertEqual(0, len(self.str_obj_model.seqs)) msg_spec = self.str_obj_model.msgs[0] msg_name = msg_spec.name self.assertEqual('zoneMismatch', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the ZoneMismatchMsg class ------------------------------ zone_mismatch_msg_cls = make_msg_class(self.str_obj_model, msg_name) # create a message instance --------------------------------- values = self.zone_mismatch_fields() # list of quasi-random values zmm_msg = zone_mismatch_msg_cls(values) self.assertEqual(msg_spec.name, zmm_msg.name) # we don't have any nested enums or messages # pylint: disable=no-member self.assertEqual(0, len(zmm_msg.enums)) self.assertEqual(0, len(zmm_msg.msgs)) self.assertEqual(5, len(zmm_msg.field_classes)) self.assertEqual(5, len(zmm_msg)) # number of fields in instance self.assertEqual(values[0], zmm_msg.timestamp) self.assertEqual(values[1], zmm_msg.seq_nbr) self.assertEqual(values[2], zmm_msg.zone_name) self.assertEqual(values[3], zmm_msg.expected_serial) self.assertEqual(values[4], zmm_msg.actual_serial) # serialize the object to the channel ----------------------- # XXX WRITE HEADER FIRST! # DEBUG print("DIR (ZMM_MSG): ", end=' ') print(dir(zmm_msg)) # END zmm_msg.write_stand_alone(chan) chan.flip() # deserialize the channel, making a clone of the message ---- (read_back, _) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) # DEBUG print("position after mis-match read back is %d" % chan.position) # END # verify that the messages are identical -------------------- self.assertTrue(zmm_msg.__eq__(read_back)) # produce another message from the same values -------------- zmm_msg2 = zone_mismatch_msg_cls(values) chan2 = Channel(BUFSIZE) zmm_msg2.write_stand_alone(chan2) chan2.flip() (copy2, _) = MsgImpl.read(chan2, self.str_obj_model) self.assertTrue(zmm_msg.__eq__(copy2)) self.assertTrue(zmm_msg2.__eq__(copy2)) # GEEP
def test_shutdown_msg(self): # DEBUG print("\nTEST_SHUTDOWN_MSG") # END # ----------------------------------------------------------- # XXX This code has been crudely hacked from another test # module, and so needs careful review # ----------------------------------------------------------- # verify that this adds 1 (msg) + 3 (field count) to the number # of entries in getters, writeStandAlones, etc msg_spec = self.str_obj_model.msgs[2] # <------ msg_name = msg_spec.name self.assertEqual('shutdown', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the CorruptListMsg class ------------------------------ shutdown_msg_cls = make_msg_class(self.str_obj_model, msg_name) # create a message instance --------------------------------- values = [ RNG.next_file_name(8), ] # list of quasi-random values sd_msg = shutdown_msg_cls(values) self.assertEqual(msg_name, sd_msg.name) # we don't have any nested enums or messages # pylint: disable=no-member self.assertEqual(0, len(sd_msg.enums)) self.assertEqual(0, len(sd_msg.msgs)) self.assertEqual(1, len(sd_msg.field_classes)) # <--- self.assertEqual(1, len(sd_msg)) # number of fields in instance self.assertEqual(values[0], sd_msg.remarks) # serialize the object to the channel ----------------------- sd_msg.write_stand_alone(chan) chan.flip() # deserialize the channel, making a clone of the message ---- (read_back, _) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) # DEBUG print("position after shutdown read back is %d" % chan.position) # END # verify that the messages are identical -------------------- self.assertTrue(sd_msg.__eq__(read_back)) # produce another message from the same values -------------- sd_msg2 = shutdown_msg_cls(values) chan2 = Channel(BUFSIZE) sd_msg2.write_stand_alone(chan2) chan2.flip() (copy2, _) = MsgImpl.read(chan2, self.str_obj_model) self.assertTrue(sd_msg.__eq__(copy2)) self.assertTrue(sd_msg2.__eq__(copy2)) # GEEP GEEP GEEP
def do_test_ack(self, text): # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the ack_msg_cls class ------------------------------ ack_spec = self.s_obj_model.msgs[0] msg_name = ack_spec.name self.assertEqual('ack', msg_name) ack_msg_cls = make_msg_class(self.s_obj_model, 'ack') # create a message instance --------------------------------- values = [text] ack = ack_msg_cls(values) (_) = tuple(values) # WAS text1 # pylint: disable=no-member self.assertEqual(ack_spec.name, ack.name) # we don't have any nested enums or messages # pylint: disable=no-member self.assertEqual(0, len(ack.enums)) # pylint: disable=no-member self.assertEqual(0, len(ack.msgs)) # pylint: disable=no-member self.assertEqual(1, len(ack.fieldClasses)) self.assertEqual(1, len(ack)) # number of fields in instance for ndx, value in enumerate(ack): self.assertEqual(values[ndx], value) # verify fields are accessible in the object ---------------- # pylint: disable=no-member self.assertEqual(text, ack.text) # serialize the object to the channel ----------------------- buf = chan.buffer chan.clear() nnn = ack.write_stand_alone(chan) self.assertEqual(0, nnn) # returns msg index chan.flip() print("ACTUAL LENGTH OF SERIALIZED ACK OBJECT: %u" % chan.limit) # deserialize the channel, making a clone of the message ---- (readback, _) = MsgImpl.read(chan, self.s_obj_model) self.assertIsNotNone(readback) self.assertTrue(ack.__eq__(readback)) # produce another message from the same values -------------- ack2 = ack_msg_cls(values) chan2 = Channel(BUFSIZE) nnn = ack2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = ack_msg_cls.read(chan2, self.s_obj_model) self.assertTrue(ack.__eq__(readback)) self.assertTrue(ack2.__eq__(copy2)) self.assertEqual(nnn, nn3) # GEEP
def test_le_msg_serialization(self): # parse the protoSpec # XXX highly questionable: # verify that this adds 1 (msg) + 5 (field count) to the number # of entries in getters, putters, etc self.assertIsNotNone(self.s_obj_model) self.assertTrue(isinstance(self.s_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.ringd', self.s_obj_model.name) self.assertEqual(0, len(self.s_obj_model.enums)) self.assertEqual(11, len(self.s_obj_model.msgs)) self.assertEqual(0, len(self.s_obj_model.seqs)) # XXX a foolish test, but we use the variable 'leMsgSpec' below le_msg_spec = self.s_obj_model.msgs[5] msg_name = le_msg_spec.name self.assertEqual('logEntry', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the LogEntryMsg class ------------------------------ log_entry_msg = make_msg_class(self.s_obj_model, 'logEntry') # create a message instance --------------------------------- values = self.le_msg_values() # a list of quasi-random values le_msg = log_entry_msg(values) (timestamp, key, length, node_id, src, path) = tuple(values) # DEBUG print("TIMESTAMP = %d" % timestamp) print("KEY = %s" % key) print("LENGTH = %s" % length) # END # pylint: disable=no-member self.assertEqual(le_msg_spec.name, le_msg.name) # we don't have any nested enums or messages self.assertEqual(0, len(le_msg.enums)) self.assertEqual(0, len(le_msg.msgs)) # pylint: disable=no-member self.assertEqual(6, len(le_msg.fieldClasses)) self.assertEqual(6, len(le_msg)) # number of fields in instance for ndx, value in enumerate(le_msg): self.assertEqual(value, values[ndx]) # verify fields are accessible in the object ---------------- #(timestamp, key, length, nodeID, src, path) = tuple(values) self.assertEqual(timestamp, le_msg.timestamp) # pylint: disable=no-member self.assertEqual(key, le_msg.key) # pylint: disable=no-member self.assertEqual(length, le_msg.length) # pylint: disable=no-member self.assertEqual(node_id, le_msg.node_id) # pylint: disable=no-member self.assertEqual(src, le_msg.src) # pylint: disable=no-member self.assertEqual(path, le_msg.path) # serialize the object to the channel ----------------------- buf = chan.buffer chan.clear() nnn = le_msg.write_stand_alone(chan) self.assertEqual(5, nnn) # returns msg index old_position = chan.position # TESTING flip() chan.flip() self.assertEqual(old_position, chan.limit) # TESTING flip() self.assertEqual(0, chan.position) # TESTING flip() actual = chan.limit print("ACTUAL LENGTH OF SERIALIZED OBJECT: %u" % actual) # deserialize the channel, making a clone of the message ---- (readback, _) = MsgImpl.read(chan, self.s_obj_model) self.assertIsNotNone(readback) self.assertTrue(le_msg.__eq__(readback)) # produce another message from the same values -------------- le_msg2 = log_entry_msg(values) chan2 = Channel(BUFSIZE) nnn = le_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = log_entry_msg.read(chan2, self.s_obj_model) self.assertTrue(le_msg.__eq__(readback)) self.assertTrue(le_msg2.__eq__(copy2)) self.assertEqual(nnn, nn3) # GEEP
def test_zone_mismatch_msg(self): # DEBUG print("\nTEST_ZONE_MISMATCH_MSG") # END # from setUp(): ============================================= # end stuff from setup ====================================== # ----------------------------------------------------------- # XXX This code has been crudely hacked from another test # module, and so needs careful review # ----------------------------------------------------------- # verify that this adds 1 (msg) + 3 (field count) to the number # of entries in getters, putters, etc self.assertIsNotNone(self.str_obj_model) self.assertTrue(isinstance(self.str_obj_model, M.ProtoSpec)) self.assertEqual('org.xlattice.alertz', self.str_obj_model.name) self.assertEqual(0, len(self.str_obj_model.enums)) self.assertEqual(16, len(self.str_obj_model.msgs)) self.assertEqual(0, len(self.str_obj_model.seqs)) msg_spec = self.str_obj_model.msgs[0] msg_name = msg_spec.name self.assertEqual('zoneMismatch', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the ZoneMismatchMsg class ------------------------------ zone_mismatch_msg_cls = make_msg_class(self.str_obj_model, msg_name) # create a message instance --------------------------------- values = self.zone_mismatch_fields() # list of quasi-random values zmm_msg = zone_mismatch_msg_cls(values) self.assertEqual(msg_spec.name, zmm_msg._name) # we don't have any nested enums or messages self.assertEqual(0, len(zmm_msg.enums)) self.assertEqual(0, len(zmm_msg.msgs)) self.assertEqual(5, len(zmm_msg.field_classes)) self.assertEqual(5, len(zmm_msg)) # number of fields in instance self.assertEqual(values[0], zmm_msg.timestamp) self.assertEqual(values[1], zmm_msg.seq_nbr) self.assertEqual(values[2], zmm_msg.zone_name) self.assertEqual(values[3], zmm_msg.expected_serial) self.assertEqual(values[4], zmm_msg.actual_serial) # serialize the object to the channel ----------------------- # XXX WRITE HEADER FIRST! # DEBUG print("DIR (ZMM_MSG): ", end=' ') print(dir(zmm_msg)) # END zmm_msg.write_stand_alone(chan) chan.flip() # deserialize the channel, making a clone of the message ---- (read_back, nn2) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) # DEBUG print("position after mis-match read back is %d" % chan.position) # END # verify that the messages are identical -------------------- self.assertTrue(zmm_msg.__eq__(read_back)) # produce another message from the same values -------------- zmm_msg2 = zone_mismatch_msg_cls(values) chan2 = Channel(BUFSIZE) zmm_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn3) = MsgImpl.read(chan2, self.str_obj_model) self.assertTrue(zmm_msg.__eq__(copy2)) self.assertTrue(zmm_msg2.__eq__(copy2)) # GEEP
def test_shutdown_msg(self): # DEBUG print("\nTEST_SHUTDOWN_MSG") # END # ----------------------------------------------------------- # XXX This code has been crudely hacked from another test # module, and so needs careful review # ----------------------------------------------------------- # verify that this adds 1 (msg) + 3 (field count) to the number # of entries in getters, writeStandAlones, etc msg_spec = self.str_obj_model.msgs[2] # <------ msg_name = msg_spec.name self.assertEqual('shutdown', msg_name) # Create a channel ------------------------------------------ # its buffer will be used for both serializing # the instance # data and, by deserializing it, for creating a second instance. chan = Channel(BUFSIZE) buf = chan.buffer self.assertEqual(BUFSIZE, len(buf)) # create the CorruptListMsg class ------------------------------ shutdown_msg_cls = make_msg_class(self.str_obj_model, msg_name) # create a message instance --------------------------------- values = [RNG.next_file_name(8), ] # list of quasi-random values sd_msg = shutdown_msg_cls(values) self.assertEqual(msg_name, sd_msg._name) # we don't have any nested enums or messages self.assertEqual(0, len(sd_msg.enums)) self.assertEqual(0, len(sd_msg.msgs)) self.assertEqual(1, len(sd_msg.field_classes)) # <--- self.assertEqual(1, len(sd_msg)) # number of fields in instance self.assertEqual(values[0], sd_msg.remarks) # serialize the object to the channel ----------------------- sd_msg.write_stand_alone(chan) chan.flip() # deserialize the channel, making a clone of the message ---- (read_back, nn5) = MsgImpl.read(chan, self.str_obj_model) self.assertIsNotNone(read_back) # DEBUG print("position after shutdown read back is %d" % chan.position) # END # verify that the messages are identical -------------------- self.assertTrue(sd_msg.__eq__(read_back)) # produce another message from the same values -------------- sd_msg2 = shutdown_msg_cls(values) chan2 = Channel(BUFSIZE) sd_msg2.write_stand_alone(chan2) chan2.flip() (copy2, nn6) = MsgImpl.read(chan2, self.str_obj_model) self.assertTrue(sd_msg.__eq__(copy2)) self.assertTrue(sd_msg2.__eq__(copy2)) # GEEP GEEP GEEP