1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import mock
import unittest
import threading
import re
from concurrent.futures import Future
from botocore.session import Session
from s3transfer.subscribers import BaseSubscriber
from __tests__ import FileCreator
from __tests__ import requires_crt, HAS_CRT
if HAS_CRT:
import s3transfer.crt
import awscrt
class submitThread(threading.Thread):
def __init__(self, transfer_manager, futures, callargs):
threading.Thread.__init__(self)
self._transfer_manager = transfer_manager
self._futures = futures
self._callargs = callargs
def run(self):
self._futures.append(self._transfer_manager.download(*self._callargs))
class RecordingSubscriber(BaseSubscriber):
def __init__(self):
self.on_queued_called = False
self.on_done_called = False
self.bytes_transferred = 0
self.on_queued_future = None
self.on_done_future = None
def on_queued(self, future, **kwargs):
self.on_queued_called = True
self.on_queued_future = future
def on_done(self, future, **kwargs):
self.on_done_called = True
self.on_done_future = future
@requires_crt
class TestCRTTransferManager(unittest.TestCase):
def setUp(self):
self.region = 'us-west-2'
self.bucket = "test_bucket"
self.key = "test_key"
self.files = FileCreator()
self.filename = self.files.create_file('myfile', 'my content')
self.expected_path = "/" + self.bucket + "/" + self.key
self.expected_host = "s3.%s.amazonaws.com" % (self.region)
self.s3_request = mock.Mock(awscrt.s3.S3Request)
self.s3_crt_client = mock.Mock(awscrt.s3.S3Client)
self.s3_crt_client.make_request.return_value = self.s3_request
self.session = Session()
self.session.set_config_variable('region', self.region)
self.request_serializer = s3transfer.crt.BotocoreCRTRequestSerializer(
self.session)
self.transfer_manager = s3transfer.crt.CRTTransferManager(
crt_s3_client=self.s3_crt_client,
crt_request_serializer=self.request_serializer)
self.record_subscriber = RecordingSubscriber()
def tearDown(self):
self.files.remove_all()
def _assert_subscribers_called(self, expected_future=None):
self.assertTrue(self.record_subscriber.on_queued_called)
self.assertTrue(self.record_subscriber.on_done_called)
if expected_future:
self.assertIs(
self.record_subscriber.on_queued_future,
expected_future)
self.assertIs(
self.record_subscriber.on_done_future,
expected_future)
def _invoke_done_callbacks(self, **kwargs):
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
on_done = callargs_kwargs["on_done"]
on_done(error=None)
def _simulate_file_download(self, recv_filepath):
self.files.create_file(recv_filepath, "fake resopnse")
def _simulate_make_request_side_effect(self, **kwargs):
if kwargs.get('recv_filepath'):
self._simulate_file_download(kwargs['recv_filepath'])
self._invoke_done_callbacks()
return mock.DEFAULT
def test_upload(self):
self.s3_crt_client.make_request.side_effect = self._simulate_make_request_side_effect
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, [self.record_subscriber])
future.result()
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
self.assertEqual(callargs_kwargs["send_filepath"], self.filename)
self.assertIsNone(callargs_kwargs["recv_filepath"])
self.assertEqual(callargs_kwargs["type"],
awscrt.s3.S3RequestType.PUT_OBJECT)
crt_request = callargs_kwargs["request"]
self.assertEqual("PUT", crt_request.method)
self.assertEqual(self.expected_path, crt_request.path)
self.assertEqual(self.expected_host, crt_request.headers.get("host"))
self._assert_subscribers_called(future)
def test_download(self):
self.s3_crt_client.make_request.side_effect = self._simulate_make_request_side_effect
future = self.transfer_manager.download(
self.bucket, self.key, self.filename, {}, [self.record_subscriber])
future.result()
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
# the recv_filepath will be set to a temporary file path with some
# random suffix
self.assertTrue(re.match(self.filename + ".*",
callargs_kwargs["recv_filepath"]))
self.assertIsNone(callargs_kwargs["send_filepath"])
self.assertEqual(callargs_kwargs["type"],
awscrt.s3.S3RequestType.GET_OBJECT)
crt_request = callargs_kwargs["request"]
self.assertEqual("GET", crt_request.method)
self.assertEqual(self.expected_path, crt_request.path)
self.assertEqual(self.expected_host, crt_request.headers.get("host"))
self._assert_subscribers_called(future)
with open(self.filename, 'rb') as f:
# Check the fake response overwrites the file because of download
self.assertEqual(f.read(), b'fake resopnse')
def test_delete(self):
self.s3_crt_client.make_request.side_effect = self._simulate_make_request_side_effect
future = self.transfer_manager.delete(
self.bucket, self.key, {}, [self.record_subscriber])
future.result()
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
self.assertIsNone(callargs_kwargs["send_filepath"])
self.assertIsNone(callargs_kwargs["recv_filepath"])
self.assertEqual(callargs_kwargs["type"],
awscrt.s3.S3RequestType.DEFAULT)
crt_request = callargs_kwargs["request"]
self.assertEqual("DELETE", crt_request.method)
self.assertEqual(self.expected_path, crt_request.path)
self.assertEqual(self.expected_host, crt_request.headers.get("host"))
self._assert_subscribers_called(future)
def test_blocks_when_max_requests_processes_reached(self):
futures = []
callargs = (self.bucket, self.key, self.filename, {}, [])
max_request_processes = 128 # the hard coded max processes
all_concurrent = max_request_processes + 1
threads = []
for i in range(0, all_concurrent):
thread = submitThread(self.transfer_manager, futures, callargs)
thread.start()
threads.append(thread)
self.assertLessEqual(
self.s3_crt_client.make_request.call_count,
max_request_processes)
# Release lock
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
on_done = callargs_kwargs["on_done"]
on_done(error=None)
for thread in threads:
thread.join()
self.assertEqual(
self.s3_crt_client.make_request.call_count,
all_concurrent)
def _cancel_function(self):
self.cancel_called = True
self.s3_request.finished_future.set_exception(
awscrt.exceptions.from_code(0))
self._invoke_done_callbacks()
def test_cancel(self):
self.s3_request.finished_future = Future()
self.cancel_called = False
self.s3_request.cancel = self._cancel_function
try:
with self.transfer_manager:
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, [])
raise KeyboardInterrupt()
except KeyboardInterrupt:
pass
with self.assertRaises(awscrt.exceptions.AwsCrtError):
future.result()
self.assertTrue(self.cancel_called)
def test_serializer_error_handling(self):
class SerializationException(Exception):
pass
class ExceptionRaisingSerializer(s3transfer.crt.BaseCRTRequestSerializer):
def serialize_http_request(self, transfer_type, future):
raise SerializationException()
not_impl_serializer = ExceptionRaisingSerializer()
transfer_manager = s3transfer.crt.CRTTransferManager(
crt_s3_client=self.s3_crt_client,
crt_request_serializer=not_impl_serializer)
future = transfer_manager.upload(
self.filename, self.bucket, self.key, {}, [])
with self.assertRaises(SerializationException):
future.result()
def test_crt_s3_client_error_handling(self):
self.s3_crt_client.make_request.side_effect = awscrt.exceptions.from_code(
0)
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, [])
with self.assertRaises(awscrt.exceptions.AwsCrtError):
future.result()
|