|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import threading |
|
import time |
|
from concurrent.futures import CancelledError |
|
from io import BytesIO |
|
|
|
from s3transfer.manager import TransferConfig |
|
from tests import ( |
|
NonSeekableReader, |
|
RecordingSubscriber, |
|
skip_if_using_serial_implementation, |
|
) |
|
from tests.integration import ( |
|
BaseTransferManagerIntegTest, |
|
WaitForTransferStart, |
|
) |
|
|
|
|
|
class TestUpload(BaseTransferManagerIntegTest): |
|
def setUp(self): |
|
super().setUp() |
|
self.multipart_threshold = 5 * 1024 * 1024 |
|
self.config = TransferConfig( |
|
multipart_threshold=self.multipart_threshold |
|
) |
|
|
|
def get_input_fileobj(self, size, name=''): |
|
return self.files.create_file_with_size(name, size) |
|
|
|
def test_upload_below_threshold(self): |
|
transfer_manager = self.create_transfer_manager(self.config) |
|
file = self.get_input_fileobj(size=1024 * 1024, name='1mb.txt') |
|
future = transfer_manager.upload(file, self.bucket_name, '1mb.txt') |
|
self.addCleanup(self.delete_object, '1mb.txt') |
|
|
|
future.result() |
|
self.assertTrue(self.object_exists('1mb.txt')) |
|
|
|
def test_upload_above_threshold(self): |
|
transfer_manager = self.create_transfer_manager(self.config) |
|
file = self.get_input_fileobj(size=20 * 1024 * 1024, name='20mb.txt') |
|
future = transfer_manager.upload(file, self.bucket_name, '20mb.txt') |
|
self.addCleanup(self.delete_object, '20mb.txt') |
|
|
|
future.result() |
|
self.assertTrue(self.object_exists('20mb.txt')) |
|
|
|
@skip_if_using_serial_implementation( |
|
'Exception is thrown once the transfer is submitted. ' |
|
'However for the serial implementation, transfers are performed ' |
|
'in main thread meaning the transfer will complete before the ' |
|
'KeyboardInterrupt being thrown.' |
|
) |
|
def test_large_upload_exits_quicky_on_exception(self): |
|
transfer_manager = self.create_transfer_manager(self.config) |
|
|
|
filename = self.get_input_fileobj( |
|
name='foo.txt', size=20 * 1024 * 1024 |
|
) |
|
|
|
timeout = 10 |
|
bytes_transferring = threading.Event() |
|
subscriber = WaitForTransferStart(bytes_transferring) |
|
try: |
|
with transfer_manager: |
|
future = transfer_manager.upload( |
|
filename, |
|
self.bucket_name, |
|
'20mb.txt', |
|
subscribers=[subscriber], |
|
) |
|
if not bytes_transferring.wait(timeout): |
|
future.cancel() |
|
raise RuntimeError( |
|
"Download transfer did not start after waiting for " |
|
"%s seconds." % timeout |
|
) |
|
|
|
|
|
start_time = time.time() |
|
raise KeyboardInterrupt() |
|
except KeyboardInterrupt: |
|
pass |
|
end_time = time.time() |
|
|
|
|
|
|
|
max_allowed_exit_time = 5 |
|
actual_time_to_exit = end_time - start_time |
|
self.assertLess( |
|
actual_time_to_exit, |
|
max_allowed_exit_time, |
|
"Failed to exit under {}. Instead exited in {}.".format( |
|
max_allowed_exit_time, actual_time_to_exit |
|
), |
|
) |
|
|
|
try: |
|
future.result() |
|
self.skipTest( |
|
'Upload completed before interrupted and therefore ' |
|
'could not cancel the upload' |
|
) |
|
except CancelledError as e: |
|
self.assertEqual(str(e), 'KeyboardInterrupt()') |
|
|
|
|
|
self.assertTrue(self.object_not_exists('20mb.txt')) |
|
|
|
@skip_if_using_serial_implementation( |
|
'Exception is thrown once the transfers are submitted. ' |
|
'However for the serial implementation, transfers are performed ' |
|
'in main thread meaning the transfers will complete before the ' |
|
'KeyboardInterrupt being thrown.' |
|
) |
|
def test_many_files_exits_quicky_on_exception(self): |
|
|
|
|
|
|
|
self.config.max_request_queue_size = 1 |
|
self.config.max_submission_concurrency = 1 |
|
transfer_manager = self.create_transfer_manager(self.config) |
|
|
|
fileobjs = [] |
|
keynames = [] |
|
futures = [] |
|
for i in range(10): |
|
filename = 'file' + str(i) |
|
keynames.append(filename) |
|
fileobjs.append( |
|
self.get_input_fileobj(name=filename, size=1024 * 1024) |
|
) |
|
|
|
try: |
|
with transfer_manager: |
|
for i, fileobj in enumerate(fileobjs): |
|
futures.append( |
|
transfer_manager.upload( |
|
fileobj, self.bucket_name, keynames[i] |
|
) |
|
) |
|
|
|
|
|
start_time = time.time() |
|
raise KeyboardInterrupt() |
|
except KeyboardInterrupt: |
|
pass |
|
end_time = time.time() |
|
|
|
|
|
max_allowed_exit_time = 5 |
|
self.assertLess( |
|
end_time - start_time, |
|
max_allowed_exit_time, |
|
"Failed to exit under {}. Instead exited in {}.".format( |
|
max_allowed_exit_time, end_time - start_time |
|
), |
|
) |
|
|
|
|
|
with self.assertRaisesRegex(CancelledError, 'KeyboardInterrupt()'): |
|
for future in futures: |
|
future.result() |
|
|
|
|
|
self.assertTrue(self.object_not_exists(future.meta.call_args.key)) |
|
|
|
def test_progress_subscribers_on_upload(self): |
|
subscriber = RecordingSubscriber() |
|
transfer_manager = self.create_transfer_manager(self.config) |
|
file = self.get_input_fileobj(size=20 * 1024 * 1024, name='20mb.txt') |
|
future = transfer_manager.upload( |
|
file, self.bucket_name, '20mb.txt', subscribers=[subscriber] |
|
) |
|
self.addCleanup(self.delete_object, '20mb.txt') |
|
|
|
future.result() |
|
|
|
|
|
|
|
|
|
self.assertEqual(subscriber.calculate_bytes_seen(), 20 * 1024 * 1024) |
|
|
|
|
|
class TestUploadSeekableStream(TestUpload): |
|
def get_input_fileobj(self, size, name=''): |
|
return BytesIO(b'0' * size) |
|
|
|
|
|
class TestUploadNonSeekableStream(TestUpload): |
|
def get_input_fileobj(self, size, name=''): |
|
return NonSeekableReader(b'0' * size) |
|
|