Exemplo n.º 1
0
    def setUpClass(cls): # pylint: disable-msg=C0103

        """This method is executed once at the start of this Unit Test."""

        debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY, debug.TESTUNIT, "STARTING %s..." % TestSboDataSourceReplay.__name__)

        # Create an instance of SboDataSourceReplay class.
        cls.sbo_data_source_replay = SboDataSourceReplay()

        # Use the constants defined in the sbo_data_source_replay.py module to determine the current capture directory.
        base_directory = os.sys.path[0]
        capture_folder = sbo_data_source_replay.CAPTURE_FOLDER
        capture_current_subfolder = sbo_data_source_replay.CAPTURE_CURRENT_SUBFOLDER

        cls.capture_current_path = os.path.join(base_directory, capture_folder, capture_current_subfolder)
Exemplo n.º 2
0
    def test_05_playback_fast(self):

        """Test the playback of captured data files in fast replay mode."""

        debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY, debug.TESTUNIT, "STARTING %s:" % "test_05_playback_fast")

        # In Fast Replay Mode, the minimum request periods should be ignored.
        # This means that playback should always return data without having to wait.

        # Define the parameters for fast replay mode.
        replay_folder_path = 'test_data/playback_files/set_003'
        replay_mode = FAST_REPLAY_MODE
        minimum_request_period = [datetime.timedelta(seconds = MINIMUM_REQUEST_PERIOD_LIVE), datetime.timedelta(seconds = MINIMUM_REQUEST_PERIOD_NON_LIVE)]

        # Attempt to successfully initialise the playback() method.
        self.reset_playback(replay_folder_path, replay_mode, minimum_request_period)

        # Attempt to playback data file 1.
        raw_data = self.sbo_data_source_replay.playback()

        # A: Test that the length of the returned raw data is as expected for data file 1.
        actual_result = len(raw_data)
        expected_result = 1003
        self.assertEqual(actual_result, expected_result, "[A] The actual result doesn't match the expected result.")

        # Attempt to playback data file 2.
        raw_data = self.sbo_data_source_replay.playback()

        # B: Test that the length of the returned raw data is as expected for data file 2.
        actual_result = len(raw_data)
        expected_result = 59
        self.assertEqual(actual_result, expected_result, "[B] The actual result doesn't match the expected result.")

        # Attempt to playback data file 3.
        raw_data = self.sbo_data_source_replay.playback()

        # C: Test that the length of the returned raw data is as expected for data file 3.
        actual_result = len(raw_data)
        expected_result = 50
        self.assertEqual(actual_result, expected_result, "[C] The actual result doesn't match the expected result.")

        # Attempt to playback data file 4.
        raw_data = self.sbo_data_source_replay.playback()

        # D: Test that the length of the returned raw data is as expected for data file 4.
        actual_result = len(raw_data)
        expected_result = 67
        self.assertEqual(actual_result, expected_result, "[D] The actual result doesn't match the expected result.")

        # Create a temporary instance of the class.
        # Force the Playback file number to a high value to check how the class handles running out of numbers.
        temp_sbo_data_source_replay = SboDataSourceReplay()
        temp_sbo_data_source_replay.replay_file_number[PLAYBACK_FILE] = 99996

        # Define the parameters for fast replay mode.
        replay_folder_path = 'test_data/playback_files/set_004'
        replay_mode = FAST_REPLAY_MODE
        minimum_request_period = [datetime.timedelta(seconds = MINIMUM_REQUEST_PERIOD_LIVE), datetime.timedelta(seconds = MINIMUM_REQUEST_PERIOD_NON_LIVE)]

        # Attempt to successfully initialise the playback() method.
        temp_sbo_data_source_replay.initalise_playback(replay_folder_path, replay_mode, minimum_request_period)

        # Attempt to playback data file 99997.
        raw_data = temp_sbo_data_source_replay.playback()

        # E: Test that the length of the returned raw data is as expected for data file 99997.
        actual_result = len(raw_data)
        expected_result = 1115
        self.assertEqual(actual_result, expected_result, "[E] The actual result doesn't match the expected result.")

        # Attempt to playback data file 99998.
        raw_data = temp_sbo_data_source_replay.playback()

        # F: Test that the length of the returned raw data is as expected for data file 99998.
        actual_result = len(raw_data)
        expected_result = 59
        self.assertEqual(actual_result, expected_result, "[F] The actual result doesn't match the expected result.")

        # Attempt to playback data file 99999.
        raw_data = temp_sbo_data_source_replay.playback()

        # G: Test that the length of the returned raw data is as expected for data file 99999.
        actual_result = len(raw_data)
        expected_result = 60
        self.assertEqual(actual_result, expected_result, "[G] The actual result doesn't match the expected result.")

        # H: Test that trying to read again after reaching the limit raises an EndOfReplay exception.
        self.assertRaises(SboDataSourceReplay.EndOfReplay, temp_sbo_data_source_replay.playback)
Exemplo n.º 3
0
    def test_02_capture(self):

        """Test the capturing of raw data to file."""

        debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY, debug.TESTUNIT, "STARTING %s:" % "test_02_capture")

        # Define valid parameters for the capture() method.
        valid_raw_data = "$Page.onUpdate([35210,0,1,[[[3,'TEST LEAGUE 1','','']],0],[[1158696,1158767],[],[1158767,1162975,1164090],0],[[],[],[]]]);"
        valid_frame_type = LIVE_DATA_FRAME

        # A: Test that the method can cope with invalid inputs by raising an exception.
        for number, invalid_raw_data in enumerate(self.test_types, start=1):

            # Test type 6 is a valid value of raw data.
            if number == 6:
                continue

            debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY_EXTRA, debug.TESTUNIT, "Testing raw data with invalid type %s of %s." % (number, len(self.test_types)))
            self.assertRaises(SboDataSourceReplay.DataCaptureError, self.sbo_data_source_replay.capture, invalid_raw_data, valid_frame_type)

        # B: Test that the method can cope with invalid inputs by raising an exception.
        for number, invalid_frame_type in enumerate(self.test_types, start=1):

            # Test types 2 to 6 are valid values of frame types.
            if number in range(2, 6):
                continue

            debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY_EXTRA, debug.TESTUNIT, "Testing frame type with invalid type %s of %s." % (number, len(self.test_types)))
            self.assertRaises(SboDataSourceReplay.DataCaptureError, self.sbo_data_source_replay.capture, valid_raw_data, invalid_frame_type)

        # Send the valid parameters to the capture() method.
        self.sbo_data_source_replay.capture(valid_raw_data, valid_frame_type)

        # This will be the first capture since the object was created, so it should attempt to check/create the capture directory structure.
        expected_capture_path = os.path.join(self.sbo_data_source_replay.base_directory, 'sbo_data_captured/current')

        # C: Test that the expected capture path exists.
        actual_result = os.path.exists(expected_capture_path)
        self.assertTrue(actual_result, "[C] The expected capture path does not exist.")

        # The first file should be numbered 1, padded with zeros to total 5 digits.
        expected_capture_file_path = os.path.join(expected_capture_path, 'today-double-data-00001.dat')

        # D: Test that the expected capture file exists.
        actual_result = os.path.exists(expected_capture_file_path)
        self.assertTrue(actual_result, "[D] The expected capture file does not exist.")

        # Read the contents of the captured file.
        data_identifier, datestamp, raw_data = self.read_file(expected_capture_file_path)

        # E: Test that the data identifier is as expected.
        actual_result = data_identifier
        expected_result = 'IN PLAY DATA'
        self.assertEqual(actual_result, expected_result, "[E] The actual result doesn't match the expected result.")

        # F: Test that the date stamp is as expected.
        actual_result = len(datestamp)
        expected_result = 26
        self.assertEqual(actual_result, expected_result, "[F] The actual result doesn't match the expected result.")

        # G: Test that the raw data is as expected.
        actual_result = raw_data
        expected_result = valid_raw_data
        self.assertEqual(actual_result, expected_result, "[G] The actual result doesn't match the expected result.")

        # Send non-live data to be captured.
        frame_type = NON_LIVE_DATA_FRAME
        self.sbo_data_source_replay.capture(valid_raw_data, frame_type)

        # H: Test that the expected capture file exists.
        expected_capture_file_path = os.path.join(expected_capture_path, 'today-double-data-00002.dat')
        actual_result = os.path.exists(expected_capture_file_path)
        self.assertTrue(actual_result, "[H] The expected capture file does not exist.")

        # Read the contents of the captured file.
        data_identifier, datestamp, raw_data = self.read_file(expected_capture_file_path)

        # I: Test that the data identifier is as expected.
        actual_result = data_identifier
        expected_result = 'NOT IN PLAY DATA'
        self.assertEqual(actual_result, expected_result, "[I] The actual result doesn't match the expected result.")

        # Send empty data to be captured.
        raw_data = ''
        self.sbo_data_source_replay.capture(raw_data, valid_frame_type)

        # J: Test that the expected capture file exists.
        expected_capture_file_path = os.path.join(expected_capture_path, 'today-double-data-00003.dat')
        actual_result = os.path.exists(expected_capture_file_path)
        self.assertTrue(actual_result, "[J] The expected capture file does not exist.")

        # Read the contents of the captured file.
        data_identifier, datestamp, raw_data = self.read_file(expected_capture_file_path)

        # K: Test that the data identifier is as expected.
        actual_result = data_identifier
        expected_result = 'CONNECTION TO SERVER LOST'
        self.assertEqual(actual_result, expected_result, "[K] The actual result doesn't match the expected result.")

        # Archived capture folders are date stamped to the nearest second.
        # Allow some time to pass before archiving again to avoid trying to overwrite previous results.
        seconds = 2
        debug.message(debug_flags.TEST_SBO_DATA_SOURCE_REPLAY, debug.TESTUNIT, "Waiting for %s seconds..." % seconds)
        time.sleep(seconds)

        # Create a temporary instance of the class.
        # Force the Capture file number to a high value to check how the class handles running out of numbers.
        temp_sbo_data_source_replay = SboDataSourceReplay()
        temp_sbo_data_source_replay.replay_file_number[CAPTURE_FILE] = 99997

        # Call the Capture method 4 times to ensure the class runs out of Capture file numbers.
        # On the first call, the start number will be incremented and filename 'today-double-data-99998.dat' should be used.
        # On the second call, 'today-double-data-99999.dat' should be used. This is the last available file name.
        # On any subsequent calls, data should not be captured.
        # A warning should be displayed on the console and the application should not crash.
        temp_sbo_data_source_replay.capture(valid_raw_data, valid_frame_type)
        temp_sbo_data_source_replay.capture(valid_raw_data, valid_frame_type)
        temp_sbo_data_source_replay.capture(valid_raw_data, valid_frame_type)
        temp_sbo_data_source_replay.capture(valid_raw_data, valid_frame_type)

        # Get a list of filenames in the current capture directory.
        current_filenames = os.listdir(self.capture_current_path)

        # L: Test that the current capture directory contains the expected files.
        actual_result = sorted(current_filenames)
        expected_result = ['today-double-data-99998.dat', 'today-double-data-99999.dat']
        self.assertListEqual(actual_result, expected_result, "[L] The actual result doesn't match the expected result.")