1414
1515package com .google .cloud .hadoop .gcsio ;
1616
17+ import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .BUCKET_NAME ;
1718import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .JSON_FACTORY ;
19+ import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .OBJECT_NAME ;
1820import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .createReadChannel ;
1921import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .dataRangeResponse ;
2022import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .dataResponse ;
23+ import static com .google .cloud .hadoop .gcsio .GoogleCloudStorageTestUtils .metadataResponse ;
2124import static com .google .common .truth .Truth .assertThat ;
2225import static java .util .stream .Collectors .toList ;
2326
2427import com .google .api .client .http .HttpRequest ;
2528import com .google .api .client .testing .http .MockHttpTransport ;
29+ import com .google .api .client .util .DateTime ;
2630import com .google .api .services .storage .Storage ;
31+ import com .google .api .services .storage .model .StorageObject ;
2732import com .google .cloud .hadoop .gcsio .GoogleCloudStorageReadOptions .Fadvise ;
2833import java .io .IOException ;
34+ import java .math .BigInteger ;
2935import java .nio .ByteBuffer ;
3036import java .util .ArrayList ;
3137import java .util .Arrays ;
38+ import java .util .Date ;
3239import java .util .List ;
3340import org .junit .Test ;
3441import org .junit .runner .RunWith ;
3845@ RunWith (JUnit4 .class )
3946public class GoogleCloudStorageReadChannelTest {
4047
48+ @ Test
49+ public void metadataInitialization_eager () throws IOException {
50+ MockHttpTransport transport =
51+ GoogleCloudStorageTestUtils .mockTransport (
52+ metadataResponse (
53+ new StorageObject ()
54+ .setBucket (BUCKET_NAME )
55+ .setName (OBJECT_NAME )
56+ .setSize (new BigInteger ("123" ))
57+ .setGeneration (1L )
58+ .setMetageneration (1L )
59+ .setUpdated (new DateTime (new Date ()))));
60+
61+ List <HttpRequest > requests = new ArrayList <>();
62+
63+ Storage storage = new Storage (transport , JSON_FACTORY , requests ::add );
64+
65+ GoogleCloudStorageReadOptions options =
66+ GoogleCloudStorageReadOptions .builder ().setFastFailOnNotFound (true ).build ();
67+
68+ GoogleCloudStorageReadChannel readChannel = createReadChannel (storage , options );
69+
70+ assertThat (readChannel .size ()).isEqualTo (123 );
71+ assertThat (requests ).hasSize (1 );
72+ }
73+
74+ @ Test
75+ public void metadataInitialization_lazy () throws IOException {
76+ MockHttpTransport transport = GoogleCloudStorageTestUtils .mockTransport ();
77+
78+ List <HttpRequest > requests = new ArrayList <>();
79+
80+ Storage storage = new Storage (transport , JSON_FACTORY , requests ::add );
81+
82+ GoogleCloudStorageReadOptions options =
83+ GoogleCloudStorageReadOptions .builder ().setFastFailOnNotFound (false ).build ();
84+
85+ GoogleCloudStorageReadChannel readChannel = createReadChannel (storage , options );
86+
87+ assertThat (readChannel .size ()).isEqualTo (-1 );
88+ assertThat (requests ).isEmpty ();
89+ }
90+
4191 @ Test
4292 public void fadviseAuto_onForwardRead_switchesToRandom () throws IOException {
4393 int seekPosition = 5 ;
@@ -57,6 +107,7 @@ public void fadviseAuto_onForwardRead_switchesToRandom() throws IOException {
57107
58108 GoogleCloudStorageReadOptions options =
59109 GoogleCloudStorageReadOptions .builder ()
110+ .setFastFailOnNotFound (false )
60111 .setFadvise (Fadvise .AUTO )
61112 .setMinRangeRequestSize (1 )
62113 .setInplaceSeekLimit (2 )
@@ -102,6 +153,7 @@ public void fadviseAuto_onBackwardRead_switchesToRandom() throws IOException {
102153
103154 GoogleCloudStorageReadOptions options =
104155 GoogleCloudStorageReadOptions .builder ()
156+ .setFastFailOnNotFound (false )
105157 .setFadvise (Fadvise .AUTO )
106158 .setMinRangeRequestSize (1 )
107159 .build ();
@@ -148,6 +200,7 @@ public void footerPrefetch_reused() throws IOException {
148200
149201 GoogleCloudStorageReadOptions options =
150202 GoogleCloudStorageReadOptions .builder ()
203+ .setFastFailOnNotFound (false )
151204 .setFadvise (Fadvise .RANDOM )
152205 .setMinRangeRequestSize (footeSize )
153206 .build ();
0 commit comments