示例#1
0
    def test_initialisation_split(self):
        """Test WriteFile() initialisation with splitting."""

        # Create prefix for data log.
        prefix = os.path.join(TMP_PATH, 'unittest')

        # Delete split log if it already exists (it shouldn't).
        tmp = prefix + '_000.tmp'
        self.delete_if_exists(tmp)

        # Limit logging by both entries and time.
        max_entries = 10
        max_time = 60

        # Create split logging object.
        wf = WriteFile(prefix,
                       UnitTestMessageA,
                       max_entries=max_entries,
                       max_time=max_time)
        self.assertEqual(tmp, wf._WriteFile__get_filename())
        self.assertEqual(wf.max_entries, max_entries)
        self.assertEqual(wf.max_time, max_time)

        # Split log file does not exist until data has been written.
        self.assertFalse(os.path.exists(tmp))
        wf.close()
示例#2
0
文件: test_file.py 项目: acfr/mcl
    def test_initialisation_split(self):
        """Test WriteFile() initialisation with splitting."""

        # Create prefix for data log.
        prefix = os.path.join(TMP_PATH, 'unittest')

        # Delete split log if it already exists (it shouldn't).
        tmp = prefix + '_000.tmp'
        self.delete_if_exists(tmp)

        # Limit logging by both entries and time.
        max_entries = 10
        max_time = 60

        # Create split logging object.
        wf = WriteFile(prefix, UnitTestMessageA,
                       max_entries=max_entries,
                       max_time=max_time)
        self.assertEqual(tmp, wf._WriteFile__get_filename())
        self.assertEqual(wf.max_entries, max_entries)
        self.assertEqual(wf.max_time, max_time)

        # Split log file does not exist until data has been written.
        self.assertFalse(os.path.exists(tmp))
        wf.close()
示例#3
0
    def test_bad_init(self):
        """Test WriteFile() catches bad initialisation."""

        prefix = os.path.join(TMP_PATH, 'unittest')

        # Ensure 'prefix' is not a path.
        with self.assertRaises(IOError):
            WriteFile(TMP_PATH, UnitTestMessageA)

        # Ensure path of 'prefix' exists.
        with self.assertRaises(IOError):
            bad_prefix = os.path.join(prefix, 'does', 'not', 'exist')
            WriteFile(bad_prefix, UnitTestMessageA)

        # Ensure 'prefix' does not contain extension.
        with self.assertRaises(TypeError):
            bad_prefix = os.path.join(TMP_PATH, 'unittest.log')
            WriteFile(bad_prefix, UnitTestMessageA)

        # Ensure 'connection' is specified properly.
        with self.assertRaises(TypeError):
            WriteFile(prefix, 'connection')

        # Ensure 'revision' is specified properly.
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, revision=0)

        # Ensure 'time_origin' is specified properly.
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, time_origin=0)

        # Ensure 'max_entries' is specified properly.
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, max_entries='a')
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, max_entries=0)

        # Ensure 'max_time' is specified properly.
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, max_time='a')
        with self.assertRaises(TypeError):
            WriteFile(prefix, UnitTestMessageA, max_time=0)
示例#4
0
文件: test_file.py 项目: acfr/mcl
    def test_initialisation(self):
        """Test WriteFile() initialisation with no splitting."""

        # Create prefix for data log.
        prefix = os.path.join(TMP_PATH, 'unittest')
        tmp = prefix + '.tmp'

        # Delete log if it already exists (it shouldn't).
        self.delete_if_exists(tmp)

        # Create logging object with connection.
        WriteFile(prefix, UnitTestMessageA.connection)

        # Create logging object with message.
        wf = WriteFile(prefix, UnitTestMessageA)
        self.assertEqual(tmp, wf._WriteFile__get_filename())

        # Log file does not exist until data has been written.
        self.assertFalse(os.path.exists(tmp))
        wf.close()
示例#5
0
文件: test_file.py 项目: acfr/mcl
    def read_header(self, prefix, connection, data):
        """Test WriteFile() header format."""

        # Delete log if it already exists (it shouldn't).
        log = prefix + '.log'
        self.delete_if_exists(log)

        # Create logging object.
        wf = WriteFile(prefix, connection)
        self.assertFalse(os.path.exists(log))
        wf.write(data)
        wf.close()

        # Read log file header.
        self.assertTrue(os.path.exists(log))
        with open(log, 'r') as f:
            lines = f.readlines()
            lines = [line for line in lines if line.startswith('#')]

        # Use hard-coded example for raw connections.
        if isinstance(connection, mcl.network.abstract.Connection):
            template = RAW_HEADER

        # Use hard-coded example for messages.
        else:
            template = MESSAGE_TEMPLATE

        # Make sure header is correctly formatted.
        for i, (line, template_line) in enumerate(zip(lines, template)):
            if i == 3:
                tokens = line.split()
                self.assertEqual('revision', tokens[2])
            elif i == 4:
                tokens = line.split()
                self.assertEqual('created', tokens[2])
            else:
                self.assertEqual(line, template_line + '\n')

        # Clean up after test.
        self.delete_if_exists(log)
示例#6
0
    def test_initialisation_existing(self):
        """Test WriteFile() initialisation with no existing file."""

        # Create prefix for data log.
        prefix = os.path.join(TMP_PATH, 'unittest')
        tmp = prefix + '.tmp'

        # Create a log file to clash with.
        if not os.path.exists(tmp):
            with open(tmp, 'w') as f:
                f.write('This file was created for unit-testing.\n')

        # Create logging object which will clash with existing data.
        with self.assertRaises(IOError):
            WriteFile(prefix, UnitTestMessageA.connection)

        # Clean up.
        self.delete_if_exists(tmp)
示例#7
0
    def test_initialisation(self):
        """Test WriteFile() initialisation with no splitting."""

        # Create prefix for data log.
        prefix = os.path.join(TMP_PATH, 'unittest')
        tmp = prefix + '.tmp'

        # Delete log if it already exists (it shouldn't).
        self.delete_if_exists(tmp)

        # Create logging object with connection.
        WriteFile(prefix, UnitTestMessageA.connection)

        # Create logging object with message.
        wf = WriteFile(prefix, UnitTestMessageA)
        self.assertEqual(tmp, wf._WriteFile__get_filename())

        # Log file does not exist until data has been written.
        self.assertFalse(os.path.exists(tmp))
        wf.close()
示例#8
0
    def read_header(self, prefix, connection, data):
        """Test WriteFile() header format."""

        # Delete log if it already exists (it shouldn't).
        log = prefix + '.log'
        self.delete_if_exists(log)

        # Create logging object.
        wf = WriteFile(prefix, connection)
        self.assertFalse(os.path.exists(log))
        wf.write(data)
        wf.close()

        # Read log file header.
        self.assertTrue(os.path.exists(log))
        with open(log, 'r') as f:
            lines = f.readlines()
            lines = [line for line in lines if line.startswith('#')]

        # Use hard-coded example for raw connections.
        if isinstance(connection, mcl.network.abstract.Connection):
            template = RAW_HEADER

        # Use hard-coded example for messages.
        else:
            template = MESSAGE_TEMPLATE

        # Make sure header is correctly formatted.
        for i, (line, template_line) in enumerate(zip(lines, template)):
            if i == 3:
                tokens = line.split()
                self.assertEqual('revision', tokens[2])
            elif i == 4:
                tokens = line.split()
                self.assertEqual('created', tokens[2])
            else:
                self.assertEqual(line, template_line + '\n')

        # Clean up after test.
        self.delete_if_exists(log)
示例#9
0
    def file_write(self,
                   prefix,
                   writes_per_split=2,
                   split_delay=None,
                   max_splits=2,
                   max_entries=None,
                   max_time=None):
        """Method for testing (split) logging."""

        # Create file names.
        prefix = os.path.join(TMP_PATH, 'unittest')

        # Create logging object.
        wf = WriteFile(prefix,
                       UnitTestMessageA,
                       max_entries=max_entries,
                       max_time=max_time)

        # Iterate through log file splits.
        for split in range(max_splits):

            # No splitting enabled
            if max_entries is None and max_time is None:
                tmp = prefix + '.tmp'
                log = prefix + '.log'

            #  Split encountered.
            else:
                tmp = prefix + '_%03i.tmp' % split
                log = prefix + '_%03i.log' % split

            # Ensure files do not exist.
            self.delete_if_exists(tmp)
            self.delete_if_exists(log)

            # Write data to log file.
            for i in range(writes_per_split):
                j = (split * writes_per_split) + i
                message = {
                    'time_received': None,
                    'topic': 'test',
                    'payload': UnitTestMessageA(data=j)
                }

                wf.write(message)

            # File has not been closed. Ensure the file still has a temporary
            # extension.
            self.assertTrue(os.path.exists(tmp))
            self.assertFalse(os.path.exists(log))

            # Re-read split/log file (skip header).
            with open(tmp, 'r') as f:
                lines = f.readlines()
                lines = [line for line in lines if not line.startswith('#')]

            # Ensure data has been written to the file correctly.
            for i in range(writes_per_split):
                j = (split * writes_per_split) + i
                recorded_time, topic, payload = lines[i].split()
                self.assertEqual(topic, "'" + message['topic'] + "'")
                self.assertEqual(
                    msgpack.loads(payload.decode('hex'))['data'],
                    UnitTestMessageA(data=j)['data'])

            # No splits enabled.
            if max_entries is None and max_time is None:
                break

            # Pause before creating next split.
            if split_delay is not None:
                time.sleep(split_delay)

        # Ensure log file gets 'closed' - rotated from '.tmp' to '.log'
        # extension.
        wf.close()
        self.assertTrue(os.path.exists(log))

        # Clean up after testing.
        for ext in ['tmp', 'log']:
            self.delete_if_exists(prefix + '.%s' % ext)
            for s_ext in ['_%03i.%s' % (i, ext) for i in range(max_splits)]:
                self.delete_if_exists(prefix + '%s' % s_ext)
示例#10
0
文件: test_file.py 项目: acfr/mcl
    def file_write(self, prefix,
                   writes_per_split=2,
                   split_delay=None,
                   max_splits=2,
                   max_entries=None,
                   max_time=None):
        """Method for testing (split) logging."""

        # Create file names.
        prefix = os.path.join(TMP_PATH, 'unittest')

        # Create logging object.
        wf = WriteFile(prefix, UnitTestMessageA,
                       max_entries=max_entries,
                       max_time=max_time)

        # Iterate through log file splits.
        for split in range(max_splits):

            # No splitting enabled
            if max_entries is None and max_time is None:
                tmp = prefix + '.tmp'
                log = prefix + '.log'

            #  Split encountered.
            else:
                tmp = prefix + '_%03i.tmp' % split
                log = prefix + '_%03i.log' % split

            # Ensure files do not exist.
            self.delete_if_exists(tmp)
            self.delete_if_exists(log)

            # Write data to log file.
            for i in range(writes_per_split):
                j = (split * writes_per_split) + i
                message = {'time_received': None,
                           'topic': 'test',
                           'payload': UnitTestMessageA(data=j)}

                wf.write(message)

            # File has not been closed. Ensure the file still has a temporary
            # extension.
            self.assertTrue(os.path.exists(tmp))
            self.assertFalse(os.path.exists(log))

            # Re-read split/log file (skip header).
            with open(tmp, 'r') as f:
                lines = f.readlines()
                lines = [line for line in lines if not line.startswith('#')]

            # Ensure data has been written to the file correctly.
            for i in range(writes_per_split):
                j = (split * writes_per_split) + i
                recorded_time, topic, payload = lines[i].split()
                self.assertEqual(topic, "'" + message['topic'] + "'")
                self.assertEqual(msgpack.loads(payload.decode('hex'))['data'],
                                 UnitTestMessageA(data=j)['data'])

            # No splits enabled.
            if max_entries is None and max_time is None:
                break

            # Pause before creating next split.
            if split_delay is not None:
                time.sleep(split_delay)

        # Ensure log file gets 'closed' - rotated from '.tmp' to '.log'
        # extension.
        wf.close()
        self.assertTrue(os.path.exists(log))

        # Clean up after testing.
        for ext in ['tmp', 'log']:
            self.delete_if_exists(prefix + '.%s' % ext)
            for s_ext in ['_%03i.%s' % (i, ext) for i in range(max_splits)]:
                self.delete_if_exists(prefix + '%s' % s_ext)