Esempio n. 1
0
    def _testSeekBack(self, initial_reads, buffer_size, seek_back_amount):
        """Tests reading then seeking backwards.

    This function simulates an upload that is resumed after a connection break.
    It reads one transfer buffer at a time until it reaches initial_position,
    then seeks backwards (as if the server did not receive some of the bytes)
    and reads to the end of the file, ensuring the data read after the seek
    matches the original file.

    Args:
      initial_reads: List of integers containing read sizes to perform
          before seek.
      buffer_size: Maximum buffer size for the wrapper.
      seek_back_amount: Number of bytes to seek backward.

    Raises:
      AssertionError on wrong data returned by the wrapper.
    """
        tmp_file = self._GetTestFile()
        initial_position = 0
        for read_size in initial_reads:
            initial_position += read_size
        self.assertGreaterEqual(
            buffer_size, seek_back_amount,
            'seek_back_amount must be less than initial position %s '
            '(but was actually: %s)' % (buffer_size, seek_back_amount))
        self.assertLess(
            initial_position, self._temp_test_file_len,
            'initial_position must be less than test file size %s '
            '(but was actually: %s)' %
            (self._temp_test_file_len, initial_position))

        with open(tmp_file, 'rb') as stream:
            wrapper = ResumableStreamingJsonUploadWrapper(
                stream, buffer_size, test_small_buffer=True)
            position = 0
            for read_size in initial_reads:
                data = wrapper.read(read_size)
                self.assertEqual(
                    self._temp_test_file_contents[position:position +
                                                  read_size], data,
                    'Data from position %s to %s did not match file contents.'
                    % (position, position + read_size))
                position += len(data)
            wrapper.seek(initial_position - seek_back_amount)
            self.assertEqual(wrapper.tell(),
                             initial_position - seek_back_amount)
            data = wrapper.read()
            self.assertEqual(
                self._temp_test_file_len -
                (initial_position - seek_back_amount), len(data),
                'Unexpected data length with initial pos %s seek_back_amount %s. '
                'Expected: %s, actual: %s.' %
                (initial_position, seek_back_amount, self._temp_test_file_len -
                 (initial_position - seek_back_amount), len(data)))
            self.assertEqual(
                self._temp_test_file_contents[-len(data):], data,
                'Data from position %s to EOF did not match file contents.' %
                position)
  def _testSeekBack(self, initial_reads, buffer_size, seek_back_amount):
    """Tests reading then seeking backwards.

    This function simulates an upload that is resumed after a connection break.
    It reads one transfer buffer at a time until it reaches initial_position,
    then seeks backwards (as if the server did not receive some of the bytes)
    and reads to the end of the file, ensuring the data read after the seek
    matches the original file.

    Args:
      initial_reads: List of integers containing read sizes to perform
          before seek.
      buffer_size: Maximum buffer size for the wrapper.
      seek_back_amount: Number of bytes to seek backward.

    Raises:
      AssertionError on wrong data returned by the wrapper.
    """
    tmp_file = self._GetTestFile()
    initial_position = 0
    for read_size in initial_reads:
      initial_position += read_size
    self.assertGreaterEqual(
        buffer_size, seek_back_amount,
        'seek_back_amount must be less than initial position %s '
        '(but was actually: %s)' % (buffer_size, seek_back_amount))
    self.assertLess(
        initial_position, self._temp_test_file_len,
        'initial_position must be less than test file size %s '
        '(but was actually: %s)' % (self._temp_test_file_len, initial_position))

    with open(tmp_file, 'rb') as stream:
      wrapper = ResumableStreamingJsonUploadWrapper(
          stream, buffer_size, test_small_buffer=True)
      position = 0
      for read_size in initial_reads:
        data = wrapper.read(read_size)
        self.assertEqual(
            self._temp_test_file_contents[position:position + read_size],
            data, 'Data from position %s to %s did not match file contents.' %
            (position, position + read_size))
        position += len(data)
      wrapper.seek(initial_position - seek_back_amount)
      self.assertEqual(wrapper.tell(),
                       initial_position - seek_back_amount)
      data = wrapper.read()
      self.assertEqual(
          self._temp_test_file_len - (initial_position - seek_back_amount),
          len(data),
          'Unexpected data length with initial pos %s seek_back_amount %s. '
          'Expected: %s, actual: %s.' %
          (initial_position, seek_back_amount,
           self._temp_test_file_len - (initial_position - seek_back_amount),
           len(data)))
      self.assertEqual(
          self._temp_test_file_contents[-len(data):], data,
          'Data from position %s to EOF did not match file contents.' %
          position)