Beispiel #1
0
        def test_unicode(self):
            port = PORT.replace('%DIR%', self.tmpdirname)
            sim = SimSerial(port=port, source_file=self.logfile_filename,
                            use_timestamps=False)
            sim_thread = threading.Thread(target=sim.run)
            sim_thread.start()

            # Give it a moment to get started
            time.sleep(0.1)

            # For some reason, the test complains unless we actually read
            # from the port. This first SerialReader is here just to get
            # rid of the error message that pops up from SimSerial if we
            # don't use it.
            s = SerialReader(port=port)
            for line in SAMPLE_DATA.split('\n'):
                record = s.read()

            # Now we're going to create SerialReaders with stubbed
            # self.serial.readline methods so that when they're called,
            # we instead get the same bit of bad unicode over and over
            # again. We want to test that it performs correctly under
            # conditions.

            # A dummy serial readline that will feed us bad unicode
            def dummy_readline():
                return b'\xe2\x99\xa5\x99\xe2\x99\xa5\x00\xe2\x99\xa5\xe2\x99\xa5'

            # Create a SerialReader, then replace its serial reader with a stub so
            # we can feed it bad records.

            s = SerialReader(port=port)
            s.serial.readline = dummy_readline
            self.assertEqual('♥♥\x00♥♥', s.read())

            s = SerialReader(port=port, encoding_errors='replace')
            s.serial.readline = dummy_readline
            self.assertEqual('♥�♥\x00♥♥', s.read())

            s = SerialReader(port=port, encoding_errors='strict')
            s.serial.readline = dummy_readline
            with self.assertLogs(logging.getLogger(), logging.WARNING):
                self.assertEqual(None, s.read())

            # Don't decode at all - return raw bytes
            s = SerialReader(port=port, encoding=None)
            s.serial.readline = dummy_readline
            self.assertEqual(dummy_readline(), s.read())
Beispiel #2
0
  def test_read_bytes(self):
    port = PORT.replace('%DIR%', self.tmpdirname)
    sim = SimSerial(port=port, source_file=self.logfile_filename,
                    use_timestamps=False)
    sim_thread = threading.Thread(target=sim.run)
    sim_thread.start()

    # Give it a moment to get started
    time.sleep(0.1)

    slice = SliceTransform('1:')  # we'll want to strip out timestamp

    # Then read from serial port
    s = SerialReader(port=port, max_bytes=2)
    for data in SAMPLE_MAX_BYTES_2:
      record = s.read()
      logging.debug('data: %s, read: %s', data, record)
      self.assertEqual(data, record)
Beispiel #3
0
    def test_timeout(self):
        port = PORT.replace('%DIR%', self.tmpdirname)
        sim = SimSerial(port=port, source_file=self.logfile_filename)
        sim_thread = threading.Thread(target=sim.run)
        sim_thread.start()

        # Give it a moment to get started
        time.sleep(0.05)

        # Then read from serial port
        s = SerialReader(port=port, timeout=0.1)
        for line in SAMPLE_TIMEOUT:
            record = s.read()
            logging.debug('data: %s, read: %s', line, record)
            self.assertEqual(line, record, msg='Note: this is a time-sensitive '
                             'test that can fail non-deterministically. If the '
                             'test fails, try running from the command line, e.g. '
                             'as "logger/readers/test_serial_reader.py"')
Beispiel #4
0
    def test_use_timestamps(self):
        port = PORT.replace('%DIR%', self.tmpdirname)
        sim = SimSerial(port=port, source_file=self.logfile_filename)
        sim_thread = threading.Thread(target=sim.run)
        sim_thread.start()

        # Give it a moment to get started
        time.sleep(0.1)

        slice = SliceTransform('1:')  # we'll want to strip out timestamp

        # Then read from serial port
        s = serial.Serial(port=port)
        last_read = time.time() - 0.15
        for line in SAMPLE_DATA.split('\n'):
            data = slice.transform(line)
            record = s.readline().strip().decode('utf-8')
            now = time.time()
            logging.debug('time diff %f', now - last_read)
            self.assertAlmostEqual(now - last_read, 0.26, places=1)
            self.assertEqual(data, record)
            last_read = now