Ejemplo n.º 1
0
    def test_writing_and_reading(self):
        writer = TFWriter.create(LE_MSG_SPEC, BUFSIZE)
        writer.clear()  # should not be necessary

        buf = writer.buffer
        # reader and writer share same buffer
        reader = TFReader(LE_MSG_SPEC, BUFSIZE, buf)

        tstamp = int(time.time())
        node_id = bytearray(20)         # 160 bit
        RNG.next_bytes(node_id)  # .... random value
        key = bytearray(20)         # 160 bit
        RNG.next_bytes(key)  # .... random value
        length = RNG.next_int32()
        by_ = RNG.next_file_name(16)
        path = 'path/to/' + RNG.next_file_name(16)

        nnn = 0                           # 0-based field number
        # write a log entry into the buffer
        writer.put_next(nnn, tstamp)
        nnn = nnn + 1
        writer.put_next(nnn, node_id)
        nnn = nnn + 1
        writer.put_next(nnn, key)
        nnn = nnn + 1
        writer.put_next(nnn, length)
        nnn = nnn + 1
        writer.put_next(nnn, by_)
        nnn = nnn + 1
        writer.put_next(nnn, path)

        # now read the buffer to see what actually was written
        self.assertEqual(0, reader.position)

        reader.get_next()
        self.assertEqual(0, reader.field_nbr)
        self.assertEqual('fuint32', FieldStr.as_str(reader.field_type))
        self.assertEqual(tstamp, reader.value)
        self.assertEqual(5, reader.position)

        reader.get_next()
        self.assertEqual(1, reader.field_nbr)
        self.assertEqual('fbytes20', FieldStr.as_str(reader.field_type))
        self.assertEqual(node_id, reader.value)
        self.assertEqual(26, reader.position)

        reader.get_next()
        self.assertEqual(2, reader.field_nbr)
        self.assertEqual('fbytes20', FieldStr.as_str(reader.field_type))
        self.assertEqual(key, reader.value)
        self.assertEqual(47, reader.position)

        reader.get_next()
        self.assertEqual(3, reader.field_nbr)
        self.assertEqual('vuint32', FieldStr.as_str(reader.field_type))
        self.assertEqual(length, reader.value)

        reader.get_next()
        self.assertEqual(4, reader.field_nbr)
        self.assertEqual('lstring', FieldStr.as_str(reader.field_type))
        self.assertEqual(by_, reader.value)

        reader.get_next()
        self.assertEqual(5, reader.field_nbr)
        self.assertEqual('lstring', FieldStr.as_str(reader.field_type))
        self.assertEqual(path, reader.value)
Ejemplo n.º 2
0
 def test_reader_creator(self):
     BUFSIZE = 1024
     tf_reader = TFReader.create(TEST_MSG_SPEC, BUFSIZE)
     self.assertTrue(isinstance(tf_reader, TFReader))
     self.assertEqual(0, tf_reader.position)
     self.assertEqual(BUFSIZE, tf_reader.capacity)