Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(676)

Side by Side Diff: third_party/gsutil/third_party/boto/tests/unit/glacier/test_concurrent.py

Issue 1377933002: [catapult] - Copy Telemetry's gsutilz over to third_party. (Closed) Base URL: https://github.com/catapult-project/catapult.git@master
Patch Set: Rename to gsutil. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22 #
23 import tempfile
24 from boto.compat import Queue
25
26 from tests.compat import mock, unittest
27 from tests.unit import AWSMockServiceTestCase
28
29 from boto.glacier.concurrent import ConcurrentUploader, ConcurrentDownloader
30 from boto.glacier.concurrent import UploadWorkerThread
31 from boto.glacier.concurrent import _END_SENTINEL
32
33
34 class FakeThreadedConcurrentUploader(ConcurrentUploader):
35 def _start_upload_threads(self, results_queue, upload_id,
36 worker_queue, filename):
37 self.results_queue = results_queue
38 self.worker_queue = worker_queue
39 self.upload_id = upload_id
40
41 def _wait_for_upload_threads(self, hash_chunks, result_queue, total_parts):
42 for i in range(total_parts):
43 hash_chunks[i] = b'foo'
44
45
46 class FakeThreadedConcurrentDownloader(ConcurrentDownloader):
47 def _start_download_threads(self, results_queue, worker_queue):
48 self.results_queue = results_queue
49 self.worker_queue = worker_queue
50
51 def _wait_for_download_threads(self, filename, result_queue, total_parts):
52 pass
53
54
55 class TestConcurrentUploader(unittest.TestCase):
56
57 def setUp(self):
58 super(TestConcurrentUploader, self).setUp()
59 self.stat_patch = mock.patch('os.stat')
60 self.addCleanup(self.stat_patch.stop)
61 self.stat_mock = self.stat_patch.start()
62 # Give a default value for tests that don't care
63 # what the file size is.
64 self.stat_mock.return_value.st_size = 1024 * 1024 * 8
65
66 def test_calculate_required_part_size(self):
67 self.stat_mock.return_value.st_size = 1024 * 1024 * 8
68 uploader = ConcurrentUploader(mock.Mock(), 'vault_name')
69 total_parts, part_size = uploader._calculate_required_part_size(
70 1024 * 1024 * 8)
71 self.assertEqual(total_parts, 2)
72 self.assertEqual(part_size, 4 * 1024 * 1024)
73
74 def test_calculate_required_part_size_too_small(self):
75 too_small = 1 * 1024 * 1024
76 self.stat_mock.return_value.st_size = 1024 * 1024 * 1024
77 uploader = ConcurrentUploader(mock.Mock(), 'vault_name',
78 part_size=too_small)
79 total_parts, part_size = uploader._calculate_required_part_size(
80 1024 * 1024 * 1024)
81 self.assertEqual(total_parts, 256)
82 # Part size if 4MB not the passed in 1MB.
83 self.assertEqual(part_size, 4 * 1024 * 1024)
84
85 def test_work_queue_is_correctly_populated(self):
86 uploader = FakeThreadedConcurrentUploader(mock.MagicMock(),
87 'vault_name')
88 uploader.upload('foofile')
89 q = uploader.worker_queue
90 items = [q.get() for i in range(q.qsize())]
91 self.assertEqual(items[0], (0, 4 * 1024 * 1024))
92 self.assertEqual(items[1], (1, 4 * 1024 * 1024))
93 # 2 for the parts, 10 for the end sentinels (10 threads).
94 self.assertEqual(len(items), 12)
95
96 def test_correct_low_level_api_calls(self):
97 api_mock = mock.MagicMock()
98 uploader = FakeThreadedConcurrentUploader(api_mock, 'vault_name')
99 uploader.upload('foofile')
100 # The threads call the upload_part, so we're just verifying the
101 # initiate/complete multipart API calls.
102 api_mock.initiate_multipart_upload.assert_called_with(
103 'vault_name', 4 * 1024 * 1024, None)
104 api_mock.complete_multipart_upload.assert_called_with(
105 'vault_name', mock.ANY, mock.ANY, 8 * 1024 * 1024)
106
107 def test_downloader_work_queue_is_correctly_populated(self):
108 job = mock.MagicMock()
109 job.archive_size = 8 * 1024 * 1024
110 downloader = FakeThreadedConcurrentDownloader(job)
111 downloader.download('foofile')
112 q = downloader.worker_queue
113 items = [q.get() for i in range(q.qsize())]
114 self.assertEqual(items[0], (0, 4 * 1024 * 1024))
115 self.assertEqual(items[1], (1, 4 * 1024 * 1024))
116 # 2 for the parts, 10 for the end sentinels (10 threads).
117 self.assertEqual(len(items), 12)
118
119
120 class TestUploaderThread(unittest.TestCase):
121 def setUp(self):
122 self.fileobj = tempfile.NamedTemporaryFile()
123 self.filename = self.fileobj.name
124
125 def test_fileobj_closed_when_thread_shuts_down(self):
126 thread = UploadWorkerThread(mock.Mock(), 'vault_name',
127 self.filename, 'upload_id',
128 Queue(), Queue())
129 fileobj = thread._fileobj
130 self.assertFalse(fileobj.closed)
131 # By settings should_continue to False, it should immediately
132 # exit, and we can still verify cleanup behavior.
133 thread.should_continue = False
134 thread.run()
135 self.assertTrue(fileobj.closed)
136
137 def test_upload_errors_have_exception_messages(self):
138 api = mock.Mock()
139 job_queue = Queue()
140 result_queue = Queue()
141 upload_thread = UploadWorkerThread(
142 api, 'vault_name', self.filename,
143 'upload_id', job_queue, result_queue, num_retries=1,
144 time_between_retries=0)
145 api.upload_part.side_effect = Exception("exception message")
146 job_queue.put((0, 1024))
147 job_queue.put(_END_SENTINEL)
148
149 upload_thread.run()
150 result = result_queue.get(timeout=1)
151 self.assertIn("exception message", str(result))
152
153 def test_num_retries_is_obeyed(self):
154 # total attempts is 1 + num_retries so if I have num_retries of 2,
155 # I'll attempt the upload once, and if that fails I'll retry up to
156 # 2 more times for a total of 3 attempts.
157 api = mock.Mock()
158 job_queue = Queue()
159 result_queue = Queue()
160 upload_thread = UploadWorkerThread(
161 api, 'vault_name', self.filename,
162 'upload_id', job_queue, result_queue, num_retries=2,
163 time_between_retries=0)
164 api.upload_part.side_effect = Exception()
165 job_queue.put((0, 1024))
166 job_queue.put(_END_SENTINEL)
167
168 upload_thread.run()
169 self.assertEqual(api.upload_part.call_count, 3)
170
171
172 if __name__ == '__main__':
173 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698