def test_close_callbacks_when_not_enabled(self):
     with open(self.filename) as f:
         chunk = ReadFileChunk(f, chunk_size=1, full_file_size=3,
                               enable_callbacks=False,
                               close_callbacks=[self.close_callback])
         chunk.close()
         self.assertEqual(self.num_close_callback_calls, 0)
Exemple #2
0
 def test_close_callbacks(self):
     with open(self.filename) as f:
         chunk = ReadFileChunk(f,
                               chunk_size=1,
                               full_file_size=3,
                               close_callbacks=[self.close_callback])
         chunk.close()
         self.assertEqual(self.num_close_callback_calls, 1)
Exemple #3
0
 def test_no_call_signal_not_transferring_to_underlying_fileobj(self):
     underlying_stream = mock.Mock(io.RawIOBase)
     underlying_stream.tell.return_value = 0
     chunk = ReadFileChunk(underlying_stream, 3, 3)
     try:
         chunk.signal_not_transferring()
     except AttributeError:
         self.fail('The stream should not have tried to call '
                   'signal_not_transferring to the underlying stream.')
Exemple #4
0
    def test_tell_and_seek_boundaries(self):
        # Test to ensure ReadFileChunk behaves the same as the
        # Python standard library around seeking and reading out
        # of bounds in a file object.
        data = b'abcdefghij12345678klmnopqrst'
        start_pos = 10
        chunk_size = 8

        # Create test file
        filename = os.path.join(self.tempdir, 'foo')
        with open(filename, 'wb') as f:
            f.write(data)

        # ReadFileChunk should be a substring of only numbers
        file_objects = [
            ReadFileChunk.from_filename(filename,
                                        start_byte=start_pos,
                                        chunk_size=chunk_size)
        ]

        # Uncomment next line to validate we match Python's io.BytesIO
        # file_objects.append(io.BytesIO(data[start_pos:start_pos+chunk_size]))

        for obj in file_objects:
            self._assert_whence_start_behavior(obj)
            self._assert_whence_end_behavior(obj)
            self._assert_whence_relative_behavior(obj)
            self._assert_boundary_behavior(obj)
Exemple #5
0
 def test_file_chunk_supports_context_manager(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'abc')
     with ReadFileChunk.from_filename(filename, start_byte=0,
                                      chunk_size=2) as chunk:
         val = chunk.read()
         self.assertEqual(val, b'ab')
 def test_iter_is_always_empty(self):
     # This tests the workaround for the httplib bug (see
     # the source for more info).
     filename = os.path.join(self.tempdir, 'foo')
     open(filename, 'wb').close()
     chunk = ReadFileChunk.from_filename(
         filename, start_byte=0, chunk_size=10)
     self.assertEqual(list(chunk), [])
 def test_callback_is_invoked_on_read(self):
     chunk = ReadFileChunk.from_filename(
         self.filename, start_byte=0, chunk_size=3,
         callbacks=[self.callback])
     chunk.read(1)
     chunk.read(1)
     chunk.read(1)
     self.assertEqual(self.amounts_seen, [1, 1, 1])
Exemple #8
0
 def test_close_callbacks_when_context_handler_is_used(self):
     with open(self.filename) as f:
         with ReadFileChunk(f,
                            chunk_size=1,
                            full_file_size=3,
                            close_callbacks=[self.close_callback]) as chunk:
             chunk.read(1)
         self.assertEqual(self.num_close_callback_calls, 1)
 def test_read_entire_chunk(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'onetwothreefourfivesixseveneightnineten')
     chunk = ReadFileChunk.from_filename(
         filename, start_byte=0, chunk_size=3)
     self.assertEqual(chunk.read(), b'one')
     self.assertEqual(chunk.read(), b'')
Exemple #10
0
 def test_file_chunk_supports_context_manager(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'abc')
     with ReadFileChunk.from_filename(filename,
                                      start_byte=0,
                                      chunk_size=2) as chunk:
         val = chunk.read()
         self.assertEqual(val, b'ab')
 def test_reset_stream_emulation(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'onetwothreefourfivesixseveneightnineten')
     chunk = ReadFileChunk.from_filename(
         filename, start_byte=11, chunk_size=4)
     self.assertEqual(chunk.read(), b'four')
     chunk.seek(0)
     self.assertEqual(chunk.read(), b'four')
 def test_callback_can_be_disabled(self):
     chunk = ReadFileChunk.from_filename(
         self.filename, start_byte=0, chunk_size=3,
         callbacks=[self.callback])
     chunk.disable_callback()
     # Now reading from the ReadFileChunk should not invoke
     # the callback.
     chunk.read()
     self.assertEqual(self.amounts_seen, [])
 def test_all_callbacks_invoked_on_read(self):
     chunk = ReadFileChunk.from_filename(
         self.filename, start_byte=0, chunk_size=3,
         callbacks=[self.callback, self.callback])
     chunk.read(1)
     chunk.read(1)
     chunk.read(1)
     # The list should be twice as long because there are two callbacks
     # recording the amount read.
     self.assertEqual(self.amounts_seen, [1, 1, 1, 1, 1, 1])
 def test_callback_will_also_be_triggered_by_seek(self):
     chunk = ReadFileChunk.from_filename(
         self.filename, start_byte=0, chunk_size=3,
         callbacks=[self.callback])
     chunk.read(2)
     chunk.seek(0)
     chunk.read(2)
     chunk.seek(1)
     chunk.read(2)
     self.assertEqual(self.amounts_seen, [2, -2, 2, -1, 2])
 def test_tell_and_seek(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'onetwothreefourfivesixseveneightnineten')
     chunk = ReadFileChunk.from_filename(
         filename, start_byte=36, chunk_size=100000)
     self.assertEqual(chunk.tell(), 0)
     self.assertEqual(chunk.read(), b'ten')
     self.assertEqual(chunk.tell(), 3)
     chunk.seek(0)
     self.assertEqual(chunk.tell(), 0)
Exemple #16
0
 def test_signal_transferring(self):
     chunk = ReadFileChunk.from_filename(self.filename,
                                         start_byte=0,
                                         chunk_size=3,
                                         callbacks=[self.callback])
     chunk.signal_not_transferring()
     chunk.read(1)
     self.assertEqual(self.amounts_seen, [])
     chunk.signal_transferring()
     chunk.read(1)
     self.assertEqual(self.amounts_seen, [1])
 def test_read_with_amount_size(self):
     filename = os.path.join(self.tempdir, 'foo')
     with open(filename, 'wb') as f:
         f.write(b'onetwothreefourfivesixseveneightnineten')
     chunk = ReadFileChunk.from_filename(
         filename, start_byte=11, chunk_size=4)
     self.assertEqual(chunk.read(1), b'f')
     self.assertEqual(chunk.read(1), b'o')
     self.assertEqual(chunk.read(1), b'u')
     self.assertEqual(chunk.read(1), b'r')
     self.assertEqual(chunk.read(1), b'')
Exemple #18
0
 def open_file_chunk_reader_from_fileobj(self,
                                         fileobj,
                                         chunk_size,
                                         full_file_size,
                                         callbacks,
                                         close_callbacks=None):
     return ReadFileChunk(fileobj,
                          chunk_size,
                          full_file_size,
                          callbacks=callbacks,
                          enable_callbacks=True,
                          close_callbacks=close_callbacks)
Exemple #19
0
    def test_callback_triggered_by_out_of_bound_seeks(self):
        data = b'abcdefghij1234567890klmnopqr'

        # Create test file
        filename = os.path.join(self.tempdir, 'foo')
        with open(filename, 'wb') as f:
            f.write(data)
        chunk = ReadFileChunk.from_filename(filename,
                                            start_byte=10,
                                            chunk_size=10,
                                            callbacks=[self.callback])

        # Seek calls that generate "0" progress are skipped by
        # invoke_progress_callbacks and won't appear in the list.
        expected_callback_prog = [10, -5, 5, -1, 1, -1, 1, -5, 5, -10]

        self._assert_out_of_bound_start_seek(chunk, expected_callback_prog)
        self._assert_out_of_bound_relative_seek(chunk, expected_callback_prog)
        self._assert_out_of_bound_end_seek(chunk, expected_callback_prog)
Exemple #20
0
 def test_signal_not_transferring_to_underlying_fileobj(self):
     underlying_stream = mock.Mock()
     underlying_stream.tell.return_value = 0
     chunk = ReadFileChunk(underlying_stream, 3, 3)
     chunk.signal_not_transferring()
     self.assertTrue(underlying_stream.signal_not_transferring.called)
Exemple #21
0
 def open_file_chunk_reader(self, filename, start_byte, size, callbacks):
     return ReadFileChunk.from_filename(filename,
                                        start_byte,
                                        size,
                                        callbacks,
                                        enable_callbacks=True)
Exemple #22
0
 def test_close_callbacks(self):
     with open(self.filename) as f:
         chunk = ReadFileChunk(f, chunk_size=1, full_file_size=3,
                               close_callbacks=[self.close_callback])
         chunk.close()
         self.assertEqual(self.num_close_callback_calls, 1)