Esempio n. 1
0
    def test_read_single(self):
        """Test ReadDirectory() read single files."""

        # Read all items in directory.
        rd = ReadDirectory(LOG_PATH)
        rd_msg = ReadDirectory(LOG_PATH, message=True)

        # Read UnitTestMessageA messages.
        for i in range(0, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')
            self.assertTrue(isinstance(rd_msg.read()['payload'], UnitTestMessageA))

        # Read UnitTestMessageB messages.
        for i in range(0, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i + 1)
            self.assertEqual(round(10 * message['payload']['timestamp']), i + 1)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')
            self.assertTrue(isinstance(rd_msg.read()['payload'], UnitTestMessageB))

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)
Esempio n. 2
0
    def test_read_split(self):
        """Test ReadDirectory() read split files."""

        # Read all split-logs in directory.
        rd = ReadDirectory(SPT_PATH)

        # Read UnitTestMessageA messages.
        for i in range(10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')

        # Read UnitTestMessageB messages.
        for i in range(10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i + 1)
            self.assertEqual(round(10 * message['payload']['timestamp']),
                             i + 1)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)
Esempio n. 3
0
    def test_bad_init(self):
        """Test ReadDirectory() catches bad initialisation."""

        # Raise error when raw logs are encountered.
        with self.assertRaises(TypeError):
            ReadDirectory(LOG_PATH, ignore_raw=False)

        # Ensure failure if source is not a string.
        with self.assertRaises(TypeError):
            ReadDirectory(5)

        # Ensure failure on files.
        with self.assertRaises(IOError):
            ReadDirectory(os.path.join(LOG_PATH, 'UnitTestMessageA.log'))

        # Ensure failure on directories that do not exit.
        with self.assertRaises(IOError):
            ReadDirectory(os.path.join(LOG_PATH, 'missing_dataset'))

        # Ensure failure on non-numeric minimum times.
        with self.assertRaises(TypeError):
            ReadDirectory(LOG_PATH, min_time='a')

        # Ensure failure on non-numeric maximum times.
        with self.assertRaises(TypeError):
            ReadDirectory(LOG_PATH, max_time='a')

        # Ensure failure on smaller max time than min time.
        with self.assertRaises(ValueError):
            ReadDirectory(LOG_PATH, min_time=10, max_time=5)

        # Ensure initialisation fails on non-boolean input for message.
        with self.assertRaises(TypeError):
            ReadDirectory(LOG_PATH, message='fail')
Esempio n. 4
0
    def test_read_time(self):
        """Test ReadDirectory() read min/max time."""

        # Path to valid log file.
        rd = ReadDirectory(LOG_PATH, min_time=0.045, max_time=0.35)

        # Read UnitTestMessageA messages.
        for i in range(5, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')

        # Read UnitTestMessageB messages.
        for i in range(1, 4):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i)
            self.assertEqual(round(10 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)
Esempio n. 5
0
    def test_start_pause_stop(self):
        """Test Replay() start/pause/stop."""

        # Create objects for reading data.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Create object for replay.
        replay = Replay(reader)

        # Start replaying data.
        self.assertTrue(replay.start())
        self.assertTrue(replay.is_alive())
        self.assertFalse(replay.start())

        # Pause replaying data.
        self.assertTrue(replay.pause())
        self.assertFalse(replay.is_alive())
        self.assertFalse(replay.pause())
        self.assertTrue(replay.start())

        # Stop replaying data.
        self.assertTrue(replay.stop())
        self.assertFalse(replay.is_alive())
        self.assertFalse(replay.stop())

        # Allow threads to fully shut down.
        time.sleep(0.1)
Esempio n. 6
0
    def test_start_stop(self):
        """Test ScheduleBroadcasts() start/stop."""

        # Create objects for scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        scheduler = ScheduleBroadcasts(data.queue)

        # Start scheduling data.
        self.assertTrue(scheduler.start())
        self.assertTrue(scheduler.is_alive())
        self.assertFalse(scheduler.start())

        # Stop scheduling data.
        self.assertTrue(scheduler.stop())
        self.assertFalse(scheduler.is_alive())
        self.assertFalse(scheduler.stop())

        # Allow threads to fully shut down.
        time.sleep(0.1)
Esempio n. 7
0
    def test_read_split(self):
        """Test ReadDirectory() read split files."""

        # Read all split-logs in directory.
        rd = ReadDirectory(SPT_PATH)

        # Read UnitTestMessageA messages.
        for i in range(10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')

        # Read UnitTestMessageB messages.
        for i in range(10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i + 1)
            self.assertEqual(round(10 * message['payload']['timestamp']), i + 1)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)
Esempio n. 8
0
    def test_read_time(self):
        """Test ReadDirectory() read min/max time."""

        # Path to valid log file.
        rd = ReadDirectory(LOG_PATH, min_time=0.045, max_time=0.35)

        # Read UnitTestMessageA messages.
        for i in range(5, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')

        # Read UnitTestMessageB messages.
        for i in range(1, 4):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i)
            self.assertEqual(round(10 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)
Esempio n. 9
0
    def schedule(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data):
            buffer_A.append(data)

        def callback_B(data):
            buffer_B.append(data)

        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for reading and scheduling data.
        read = ReadDirectory(LOG_PATH, message=True)
        data = BufferData(read)
        data.start()

        # Wait for buffer to fill.
        start_wait = time.time()
        while not data.is_ready() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Schedule data.
        if not speed:
            speed = 1.0
            scheduler = ScheduleBroadcasts(data.queue)
        else:
            scheduler = ScheduleBroadcasts(data.queue, speed=speed)
        scheduler.start()

        # Wait for broadcasts to end.
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if scheduler.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.1)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)
Esempio n. 10
0
    def test_initialisation(self):
        """Test ReadDirectory() initialisation."""

        # Ensure the object does not identify available message types.
        rd = ReadDirectory(LOG_PATH)
        self.assertEqual(rd.messages, None)
        self.assertEqual(rd.min_time, None)
        self.assertEqual(rd.max_time, None)

        # Ensure the object correctly identifies available message types.
        rd = ReadDirectory(LOG_PATH, message=True)
        self.assertEqual(rd.messages, [UnitTestMessageA, UnitTestMessageB])
        self.assertEqual(rd.min_time, None)
        self.assertEqual(rd.max_time, None)

        # Ensure the object correctly identifies time range.
        min_time = 0.1
        max_time = 1.0
        rd = ReadDirectory(LOG_PATH, min_time=min_time, max_time=max_time)
        self.assertEqual(rd.messages, None)
        self.assertEqual(rd.min_time, min_time)
        self.assertEqual(rd.max_time, max_time)
Esempio n. 11
0
    def replay(self, speed=None):
        """Schedule broadcasts."""

        # Listen for message broadcasts.
        listener_A = MessageListener(UnitTestMessageA)
        listener_B = MessageListener(UnitTestMessageB)
        buffer_A = list()
        buffer_B = list()

        # Subscribe callbacks.
        def callback_A(data):
            buffer_A.append(data)

        def callback_B(data):
            buffer_B.append(data)

        listener_A.subscribe(callback_A)
        listener_B.subscribe(callback_B)

        # Create objects for replaying data.
        reader = ReadDirectory(LOG_PATH, message=True)
        if not speed:
            speed = 1.0
            replay = Replay(reader)
        else:
            replay = Replay(reader, speed=speed)

        # Start replay and Wait for broadcasts to end.
        replay.start()
        run_time = 1.0 / float(speed)
        start_wait = time.time()
        while True:
            duration = time.time() - start_wait
            if replay.is_alive() and duration < 2.0 * run_time:
                time.sleep(0.05)
            else:
                break

        # Ensure all messages were received.
        self.assertEqual(len(buffer_A) + len(buffer_B), 20)

        # Ensure timing is approximately correct.
        self.assertGreaterEqual(duration, 0.5 * run_time)
        self.assertLessEqual(duration, 2.0 * run_time)

        # Ensure replay can be restarted successfully after finishing a replay.
        self.assertTrue(replay.start())
        time.sleep(0.05)
        self.assertTrue(replay.is_alive())
        self.assertTrue(replay.stop())
Esempio n. 12
0
    def test_init(self):
        """Test BufferData() instantiation."""

        # Create data reader object.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Initialise buffer object.
        BufferData(reader)

        # Initialise buffer object with queue length.
        buf = BufferData(reader, length=100)
        self.assertEqual(buf.length, 100)
        with self.assertRaises(TypeError):
            BufferData(reader, length='100')
Esempio n. 13
0
    def test_reset(self):
        """Test BufferData() can be reset."""

        # Create data reader object.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Initialise buffer object.
        buf = BufferData(reader, length=10)

        # Start buffering data and reset without stopping. BufferData should
        # stop and flush the queue.
        buf.start()
        self.assertTrue(buf.is_alive())
        time.sleep(0.05)
        buf.reset()
        self.assertFalse(buf.is_alive())
        self.assertEqual(buf.queue.qsize(), 0)

        # Start buffering data and wait until buffer is full.
        buf.start()
        start_wait = time.time()
        while not buf.queue.full() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Read data from queue.
        #
        # Check capacity of buffer/queue and read all data from the queue. Note
        # that BufferData() is still buffering and aught to buffer the
        # remaining data to the queue while the first few elements are being
        # checked here.
        buf.stop()
        self.assertEqual(buf.queue.qsize(), 10)
        for i in range(0, 10):
            message = buf.queue.get()
            self.assertEqual(int(100 * message['elapsed_time']), i)

        # Reset buffer and start buffering data from the beginning of the file
        # again.
        buf.reset()
        buf.start()
        start_wait = time.time()
        while not buf.queue.full() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        buf.stop()
        self.assertEqual(buf.queue.qsize(), 10)
        for i in range(0, 10):
            message = buf.queue.get()
            self.assertEqual(int(100 * message['elapsed_time']), i)
Esempio n. 14
0
    def test_read_blocks(self):
        """Test BufferData() can buffer data from resource in blocks (start/stop)."""

        # Create data reader object.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Initialise buffer object.
        buf = BufferData(reader, length=10)
        self.assertFalse(buf.is_ready())

        # Start buffering data and wait until buffer is full.
        buf.start()
        start_wait = time.time()
        while not buf.queue.full() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Read data from queue.
        #
        # Check capacity of buffer/queue and read all data from the queue. Note
        # that BufferData() is still buffering and aught to buffer the
        # remaining data to the queue while the first few elements are being
        # checked here.
        #
        self.assertTrue(buf.is_ready())
        self.assertEqual(buf.queue.qsize(), 10)
        for i in range(0, 10):
            message = buf.queue.get()
            self.assertEqual(int(100 * message['elapsed_time']), i)

        # Wait until remaining data has been read.
        start_wait = time.time()
        while buf.is_alive() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Read remaining data from queue.
        for i in range(1, 11):
            message = buf.queue.get()
            self.assertEqual(int(10 * message['elapsed_time']), i)

        # Check capacity of buffer.
        self.assertTrue(buf.is_ready())
        self.assertFalse(buf.is_data_pending())
        self.assertEqual(buf.queue.qsize(), 0)
Esempio n. 15
0
    def test_init(self):
        """Test Replay() instantiation."""

        # Create objects for reading data.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Create object for replay.
        Replay(reader)

        # Create object for replay with a non-default speed.
        self.assertEqual(Replay(reader, speed=5).speed, 5)

        # Ensure the reader object is validated.
        with self.assertRaises(Exception):
            Replay(dict)

        # Ensure the speed argument is validated.
        with self.assertRaises(Exception):
            Replay(reader, speed=-1.0)
Esempio n. 16
0
    def test_start_stop(self):
        """Test BufferData() start/stop."""

        # Create data reader object.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Initialise buffer object.
        buf = BufferData(reader)
        self.assertFalse(buf.is_alive())

        # Start buffering data.
        self.assertTrue(buf.start())
        self.assertTrue(buf.is_alive())
        self.assertFalse(buf.start())

        # Stop buffering data.
        self.assertTrue(buf.stop())
        self.assertFalse(buf.is_alive())
        self.assertFalse(buf.stop())

        # Allow threads to fully shut down.
        time.sleep(0.1)
Esempio n. 17
0
    def test_read(self):
        """Test BufferData() can buffer data from resource."""

        # Create data reader object.
        reader = ReadDirectory(LOG_PATH, message=True)

        # Initialise buffer object.
        buf = BufferData(reader)

        # Check capacity of buffer/queue.
        self.assertFalse(buf.is_ready())
        self.assertTrue(buf.is_data_pending)
        self.assertTrue(buf.queue.empty())
        self.assertFalse(buf.queue.full())

        # Start buffering data and wait until files have been completely read.
        buf.start()
        start_wait = time.time()
        while buf.is_alive() and ((time.time() - start_wait) < TIMEOUT):
            time.sleep(0.1)

        # Check capacity of buffer/queue.
        self.assertTrue(buf.is_ready())
        self.assertFalse(buf.is_data_pending())
        self.assertFalse(buf.queue.empty())
        self.assertEqual(buf.queue.qsize(), 20)

        # Read data from queue.
        for i in range(0, 10):
            message = buf.queue.get()
            self.assertEqual(int(100 * message['elapsed_time']), i)

        # Read data from queue.
        for i in range(0, 10):
            message = buf.queue.get()
            self.assertEqual(int(10 * message['elapsed_time']), i + 1)
Esempio n. 18
0
    def test_read_single(self):
        """Test ReadDirectory() read single files."""

        # Read all items in directory.
        rd = ReadDirectory(LOG_PATH)
        rd_msg = ReadDirectory(LOG_PATH, message=True)

        # Read UnitTestMessageA messages.
        for i in range(0, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageA')
            self.assertTrue(
                isinstance(rd_msg.read()['payload'], UnitTestMessageA))

        # Read UnitTestMessageB messages.
        for i in range(0, 10):
            self.assertTrue(rd.is_data_pending())
            message = rd.read()
            self.assertEqual(round(10 * message['elapsed_time']), i + 1)
            self.assertEqual(round(10 * message['payload']['timestamp']),
                             i + 1)
            self.assertTrue(message['payload']['name'], 'UnitTestMessageB')
            self.assertTrue(
                isinstance(rd_msg.read()['payload'], UnitTestMessageB))

        # Ensure None is returned when all data has been read.
        self.assertFalse(rd.is_data_pending())
        message = rd.read()
        self.assertEqual(message, None)