1717import unittest
1818import mock
1919import io
20- from google .cloud .storage .fileio import BlobReader , BlobWriter , SlidingBuffer
2120import string
2221
22+ from google .cloud .storage .fileio import BlobReader , BlobWriter , SlidingBuffer
23+ from google .api_core .exceptions import RequestRangeNotSatisfiable
24+
2325TEST_TEXT_DATA = string .ascii_lowercase + "\n " + string .ascii_uppercase + "\n "
2426TEST_BINARY_DATA = TEST_TEXT_DATA .encode ("utf-8" )
2527TEST_MULTIBYTE_TEXT_DATA = u"あいうえおかきくけこさしすせそたちつてと"
@@ -50,7 +52,7 @@ def read_from_fake_data(start=0, end=None, **_):
5052 # Read and trigger the first download of chunk_size.
5153 self .assertEqual (reader .read (1 ), TEST_BINARY_DATA [0 :1 ])
5254 blob .download_as_bytes .assert_called_once_with (
53- start = 0 , end = 8 , ** download_kwargs
55+ start = 0 , end = 8 , checksum = None , ** download_kwargs
5456 )
5557
5658 # Read from buffered data only.
@@ -61,21 +63,36 @@ def read_from_fake_data(start=0, end=None, **_):
6163 self .assertEqual (reader .read (8 ), TEST_BINARY_DATA [4 :12 ])
6264 self .assertEqual (reader ._pos , 12 )
6365 self .assertEqual (blob .download_as_bytes .call_count , 2 )
64- blob .download_as_bytes .assert_called_with (start = 8 , end = 16 , ** download_kwargs )
66+ blob .download_as_bytes .assert_called_with (
67+ start = 8 , end = 16 , checksum = None , ** download_kwargs
68+ )
6569
6670 # Read a larger amount, requiring a download larger than chunk_size.
6771 self .assertEqual (reader .read (16 ), TEST_BINARY_DATA [12 :28 ])
6872 self .assertEqual (reader ._pos , 28 )
6973 self .assertEqual (blob .download_as_bytes .call_count , 3 )
70- blob .download_as_bytes .assert_called_with (start = 16 , end = 28 , ** download_kwargs )
74+ blob .download_as_bytes .assert_called_with (
75+ start = 16 , end = 28 , checksum = None , ** download_kwargs
76+ )
7177
7278 # Read all remaining data.
7379 self .assertEqual (reader .read (), TEST_BINARY_DATA [28 :])
7480 self .assertEqual (blob .download_as_bytes .call_count , 4 )
75- blob .download_as_bytes .assert_called_with (start = 28 , end = None , ** download_kwargs )
81+ blob .download_as_bytes .assert_called_with (
82+ start = 28 , end = None , checksum = None , ** download_kwargs
83+ )
7684
7785 reader .close ()
7886
87+ def test_416_error_handled (self ):
88+ blob = mock .Mock ()
89+ blob .download_as_bytes = mock .Mock (
90+ side_effect = RequestRangeNotSatisfiable ("message" )
91+ )
92+
93+ reader = BlobReader (blob )
94+ self .assertEqual (reader .read (), b"" )
95+
7996 def test_readline (self ):
8097 blob = mock .Mock ()
8198
@@ -87,12 +104,12 @@ def read_from_fake_data(start=0, end=None, **_):
87104
88105 # Read a line. With chunk_size=10, expect three chunks downloaded.
89106 self .assertEqual (reader .readline (), TEST_BINARY_DATA [:27 ])
90- blob .download_as_bytes .assert_called_with (start = 20 , end = 30 )
107+ blob .download_as_bytes .assert_called_with (start = 20 , end = 30 , checksum = None )
91108 self .assertEqual (blob .download_as_bytes .call_count , 3 )
92109
93110 # Read another line.
94111 self .assertEqual (reader .readline (), TEST_BINARY_DATA [27 :])
95- blob .download_as_bytes .assert_called_with (start = 50 , end = 60 )
112+ blob .download_as_bytes .assert_called_with (start = 50 , end = 60 , checksum = None )
96113 self .assertEqual (blob .download_as_bytes .call_count , 6 )
97114
98115 blob .size = len (TEST_BINARY_DATA )
@@ -101,7 +118,7 @@ def read_from_fake_data(start=0, end=None, **_):
101118 # Read all lines. The readlines algorithm will attempt to read past the end of the last line once to verify there is no more to read.
102119 self .assertEqual (b"" .join (reader .readlines ()), TEST_BINARY_DATA )
103120 blob .download_as_bytes .assert_called_with (
104- start = len (TEST_BINARY_DATA ), end = len (TEST_BINARY_DATA ) + 10
121+ start = len (TEST_BINARY_DATA ), end = len (TEST_BINARY_DATA ) + 10 , checksum = None
105122 )
106123 self .assertEqual (blob .download_as_bytes .call_count , 13 )
107124
0 commit comments