12 from arvados import StreamReader, StreamFileReader
13 from arvados._ranges import Range
15 import arvados_testutil as tutil
16 import run_test_server
18 class StreamFileReaderTestCase(unittest.TestCase):
19 def make_count_reader(self):
20 stream = tutil.MockStreamReader('.', '01234', '34567', '67890')
21 return StreamFileReader(stream, [Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)],
24 def test_read_returns_first_block(self):
25 # read() calls will be aligned on block boundaries - see #3663.
26 sfile = self.make_count_reader()
27 self.assertEqual('123', sfile.read(10))
29 def test_small_read(self):
30 sfile = self.make_count_reader()
31 self.assertEqual('12', sfile.read(2))
33 def test_successive_reads(self):
34 sfile = self.make_count_reader()
35 for expect in ['123', '456', '789', '']:
36 self.assertEqual(expect, sfile.read(10))
38 def test_readfrom_spans_blocks(self):
39 sfile = self.make_count_reader()
40 self.assertEqual('6789', sfile.readfrom(5, 12))
42 def test_small_readfrom_spanning_blocks(self):
43 sfile = self.make_count_reader()
44 self.assertEqual('2345', sfile.readfrom(1, 4))
46 def test_readall(self):
47 sfile = self.make_count_reader()
48 self.assertEqual('123456789', ''.join(sfile.readall()))
50 def test_one_arg_seek(self):
51 self.test_relative_seek([])
53 def test_absolute_seek(self, args=[os.SEEK_SET]):
54 sfile = self.make_count_reader()
56 self.assertEqual('78', sfile.read(2))
58 self.assertEqual('56', sfile.read(2))
60 def test_relative_seek(self, args=[os.SEEK_CUR]):
61 sfile = self.make_count_reader()
62 self.assertEqual('12', sfile.read(2))
64 self.assertEqual('56', sfile.read(2))
66 def test_end_seek(self):
67 sfile = self.make_count_reader()
68 sfile.seek(-6, os.SEEK_END)
69 self.assertEqual('45', sfile.read(2))
71 def test_seek_min_zero(self):
72 sfile = self.make_count_reader()
73 sfile.seek(-2, os.SEEK_SET)
74 self.assertEqual(0, sfile.tell())
76 def test_seek_max_size(self):
77 sfile = self.make_count_reader()
78 sfile.seek(2, os.SEEK_END)
79 self.assertEqual(9, sfile.tell())
82 self.assertEqual(9, self.make_count_reader().size())
84 def test_tell_after_block_read(self):
85 sfile = self.make_count_reader()
87 self.assertEqual(3, sfile.tell())
89 def test_tell_after_small_read(self):
90 sfile = self.make_count_reader()
92 self.assertEqual(1, sfile.tell())
94 def test_no_read_after_close(self):
95 sfile = self.make_count_reader()
97 self.assertRaises(ValueError, sfile.read, 2)
99 def test_context(self):
100 with self.make_count_reader() as sfile:
101 self.assertFalse(sfile.closed, "reader is closed inside context")
102 self.assertEqual('12', sfile.read(2))
103 self.assertTrue(sfile.closed, "reader is open after context")
105 def make_newlines_reader(self):
106 stream = tutil.MockStreamReader('.', 'one\ntwo\n\nth', 'ree\nfour\n\n')
107 return StreamFileReader(stream, [Range(0, 0, 11), Range(11, 11, 10)], 'count.txt')
109 def check_lines(self, actual):
110 self.assertEqual(['one\n', 'two\n', '\n', 'three\n', 'four\n', '\n'],
113 def test_readline(self):
114 reader = self.make_newlines_reader()
117 data = reader.readline()
121 self.check_lines(actual)
123 def test_readlines(self):
124 self.check_lines(self.make_newlines_reader().readlines())
126 def test_iteration(self):
127 self.check_lines(list(iter(self.make_newlines_reader())))
129 def test_readline_size(self):
130 reader = self.make_newlines_reader()
131 self.assertEqual('on', reader.readline(2))
132 self.assertEqual('e\n', reader.readline(4))
133 self.assertEqual('two\n', reader.readline(6))
134 self.assertEqual('\n', reader.readline(8))
135 self.assertEqual('thre', reader.readline(4))
137 def test_readlines_sizehint(self):
138 result = self.make_newlines_reader().readlines(8)
139 self.assertEqual(['one\n', 'two\n'], result[:2])
140 self.assertNotIn('three\n', result)
142 def test_name_attribute(self):
143 # Test both .name and .name() (for backward compatibility)
144 stream = tutil.MockStreamReader()
145 sfile = StreamFileReader(stream, [Range(0, 0, 0)], 'nametest')
146 self.assertEqual('nametest', sfile.name)
147 self.assertEqual('nametest', sfile.name())
149 def check_decompression(self, compress_ext, compress_func):
150 test_text = 'decompression\ntest\n'
151 test_data = compress_func(test_text)
152 stream = tutil.MockStreamReader('.', test_data)
153 reader = StreamFileReader(stream, [Range(0, 0, len(test_data))],
154 'test.' + compress_ext)
155 self.assertEqual(test_text, ''.join(reader.readall_decompressed()))
158 def gzip_compress(data):
159 compressed_data = io.BytesIO()
160 with gzip.GzipFile(fileobj=compressed_data, mode='wb') as gzip_file:
161 gzip_file.write(data)
162 return compressed_data.getvalue()
164 def test_no_decompression(self):
165 self.check_decompression('log', lambda s: s)
167 def test_gzip_decompression(self):
168 self.check_decompression('gz', self.gzip_compress)
170 def test_bz2_decompression(self):
171 self.check_decompression('bz2', bz2.compress)
174 class StreamRetryTestMixin(object):
175 # Define reader_for(coll_name, **kwargs)
176 # and read_for_test(reader, size, **kwargs).
177 API_COLLECTIONS = run_test_server.fixture('collections')
179 def keep_client(self):
180 return arvados.KeepClient(proxy='http://[%s]:1' % (tutil.TEST_HOST,),
183 def manifest_for(self, coll_name):
184 return self.API_COLLECTIONS[coll_name]['manifest_text']
187 def test_success_without_retries(self):
188 with tutil.mock_get_responses('bar', 200):
189 reader = self.reader_for('bar_file')
190 self.assertEqual('bar', self.read_for_test(reader, 3))
193 def test_read_no_default_retry(self):
194 with tutil.mock_get_responses('', 500):
195 reader = self.reader_for('user_agreement')
196 with self.assertRaises(arvados.errors.KeepReadError):
197 self.read_for_test(reader, 10)
200 def test_read_with_instance_retries(self):
201 with tutil.mock_get_responses('foo', 500, 200):
202 reader = self.reader_for('foo_file', num_retries=3)
203 self.assertEqual('foo', self.read_for_test(reader, 3))
206 def test_read_with_method_retries(self):
207 with tutil.mock_get_responses('foo', 500, 200):
208 reader = self.reader_for('foo_file')
209 self.assertEqual('foo',
210 self.read_for_test(reader, 3, num_retries=3))
213 def test_read_instance_retries_exhausted(self):
214 with tutil.mock_get_responses('bar', 500, 500, 500, 500, 200):
215 reader = self.reader_for('bar_file', num_retries=3)
216 with self.assertRaises(arvados.errors.KeepReadError):
217 self.read_for_test(reader, 3)
220 def test_read_method_retries_exhausted(self):
221 with tutil.mock_get_responses('bar', 500, 500, 500, 500, 200):
222 reader = self.reader_for('bar_file')
223 with self.assertRaises(arvados.errors.KeepReadError):
224 self.read_for_test(reader, 3, num_retries=3)
227 def test_method_retries_take_precedence(self):
228 with tutil.mock_get_responses('', 500, 500, 500, 200):
229 reader = self.reader_for('user_agreement', num_retries=10)
230 with self.assertRaises(arvados.errors.KeepReadError):
231 self.read_for_test(reader, 10, num_retries=1)
234 class StreamReaderTestCase(unittest.TestCase, StreamRetryTestMixin):
235 def reader_for(self, coll_name, **kwargs):
236 return StreamReader(self.manifest_for(coll_name).split(),
237 self.keep_client(), **kwargs)
239 def read_for_test(self, reader, byte_count, **kwargs):
240 return reader.readfrom(0, byte_count, **kwargs)
242 def test_manifest_text_without_keep_client(self):
243 mtext = self.manifest_for('multilevel_collection_1')
244 for line in mtext.rstrip('\n').split('\n'):
245 reader = StreamReader(line.split())
246 self.assertEqual(line + '\n', reader.manifest_text())
249 class StreamFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
250 def reader_for(self, coll_name, **kwargs):
251 return StreamReader(self.manifest_for(coll_name).split(),
252 self.keep_client(), **kwargs).all_files()[0]
254 def read_for_test(self, reader, byte_count, **kwargs):
255 return reader.read(byte_count, **kwargs)
258 class StreamFileReadFromTestCase(StreamFileReadTestCase):
259 def read_for_test(self, reader, byte_count, **kwargs):
260 return reader.readfrom(0, byte_count, **kwargs)
263 class StreamFileReadAllTestCase(StreamFileReadTestCase):
264 def read_for_test(self, reader, byte_count, **kwargs):
265 return ''.join(reader.readall(**kwargs))
268 class StreamFileReadAllDecompressedTestCase(StreamFileReadTestCase):
269 def read_for_test(self, reader, byte_count, **kwargs):
270 return ''.join(reader.readall_decompressed(**kwargs))
273 class StreamFileReadlinesTestCase(StreamFileReadTestCase):
274 def read_for_test(self, reader, byte_count, **kwargs):
275 return ''.join(reader.readlines(**kwargs))
277 if __name__ == '__main__':