コード例 #1
0
    def test_substream_for_single_thread_upload_large_block(self):
        FILE_PATH = 'largest_blob_from_path.temp.{}.dat'.format(
            str(uuid.uuid4()))
        with open(FILE_PATH, 'wb') as stream:
            largeStream = LargeStream(LARGE_BLOCK_SIZE, 4 * 1024 * 1024)
            chunk = largeStream.read()
            while chunk:
                stream.write(chunk)
                chunk = largeStream.read()

        with open(FILE_PATH, 'rb') as stream:
            substream = SubStream(stream, 0, 2 * 1024 * 1024, None)
            # this is to mimic stage large block: SubStream.read() is getting called by http client
            data1 = substream.read(2 * 1024 * 1024)
            substream.read(2 * 1024 * 1024)
            substream.read(2 * 1024 * 1024)

            # this is to mimic rewinding request body after connection error
            substream.seek(0)

            # this is to mimic retry: stage that large block from beginning
            data2 = substream.read(2 * 1024 * 1024)

            self.assertEqual(data1, data2)
        self._teardown(FILE_PATH)
コード例 #2
0
    def test_sub_stream_with_length_equal_to_buffer(self, resource_group, location, storage_account, storage_account_key):
        data = os.urandom(6 * 1024 * 1024)

        # assuming the max size of the buffer is 4MB, this test needs to be updated if that has changed
        # the block size is 2MB for this test
        expected_data = data[0: 2 * 1024 * 1024]
        wrapped_stream = BytesIO(expected_data)  # simulate stream given by user
        lockObj = Lock()  # simulate multi-threaded environment
        substream = SubStream(wrapped_stream, stream_begin_index=0, length=2 * 1024 * 1024, lockObj=lockObj)

        try:
            # substream should start with position at 0
            self.assertEqual(substream.tell(), 0)

            # reading a chunk that is smaller than the buffer
            data_chunk_1 = substream.read(1 * 1024 * 1024)
            self.assertEqual(len(data_chunk_1), 1 * 1024 * 1024)

            # reading a chunk that is bigger than the buffer, should not read anything beyond
            data_chunk_2 = substream.read(4 * 1024 * 1024)
            self.assertEqual(len(data_chunk_2), 1 * 1024 * 1024)

            # assert data is consistent
            self.assertEqual(data_chunk_1 + data_chunk_2, expected_data)

            # test seek
            substream.seek(1 * 1024 * 1024, SEEK_SET)
            data_chunk_2 = substream.read(1 * 1024 * 1024)

            # assert data is consistent
            self.assertEqual(data_chunk_1 + data_chunk_2, expected_data)

        finally:
            wrapped_stream.close()
            substream.close()
コード例 #3
0
    def test_sub_stream_with_length_larger_than_buffer(self, resource_group,
                                                       location,
                                                       storage_account,
                                                       storage_account_key):
        data = os.urandom(12 * 1024 * 1024)

        # assuming the max size of the buffer is 4MB, this test needs to be updated if that has changed
        # the block size is 6MB for this test
        expected_data = data[0:6 * 1024 * 1024]
        wrapped_stream = BytesIO(data)  # simulate stream given by user
        lockObj = Lock()  # simulate multi-threaded environment
        substream = SubStream(wrapped_stream,
                              stream_begin_index=0,
                              length=6 * 1024 * 1024,
                              lockObj=lockObj)

        try:
            # substream should start with position at 0
            self.assertEqual(substream.tell(), 0)

            # reading a chunk that is smaller than the buffer
            data_chunk_1 = substream.read(2 * 1024 * 1024)
            self.assertEqual(len(data_chunk_1), 2 * 1024 * 1024)

            # reading a chunk that is bigger than the data remaining in buffer, force a buffer swap
            data_chunk_2 = substream.read(4 * 1024 * 1024)
            self.assertEqual(len(data_chunk_2), 4 * 1024 * 1024)

            # assert data is consistent
            self.assertEqual(data_chunk_1 + data_chunk_2, expected_data)
            self.assertEqual(6 * 1024 * 1024, substream.tell())

            # attempt to read more than what the sub stream contains should return nothing
            empty_data = substream.read(1 * 1024 * 1024)
            self.assertEqual(0, len(empty_data))
            self.assertEqual(6 * 1024 * 1024, substream.tell())

            # test seek outside of current buffer, which is at the moment the last 2MB of data
            substream.seek(0, SEEK_SET)
            data_chunk_1 = substream.read(4 * 1024 * 1024)
            data_chunk_2 = substream.read(2 * 1024 * 1024)

            # assert data is consistent
            self.assertEqual(data_chunk_1 + data_chunk_2, expected_data)

            # test seek inside of buffer, which is at the moment the last 2MB of data
            substream.seek(4 * 1024 * 1024, SEEK_SET)
            data_chunk_2 = substream.read(2 * 1024 * 1024)

            # assert data is consistent
            self.assertEqual(data_chunk_1 + data_chunk_2, expected_data)

        finally:
            wrapped_stream.close()
            substream.close()