2828import com .google .common .collect .ImmutableList ;
2929import com .google .common .collect .ImmutableMap ;
3030import com .google .common .collect .ImmutableSet ;
31+ import com .google .common .collect .Iterators ;
3132import com .google .common .collect .Lists ;
3233import com .google .gcloud .Page ;
3334import com .google .gcloud .ReadChannel ;
@@ -287,8 +288,8 @@ public void testGetBlobFailNonExistingGeneration() {
287288 assertTrue (remoteBlob .delete ());
288289 }
289290
290- @ Test
291- public void testListBlobsSelectedFields () {
291+ @ Test ( timeout = 5000 )
292+ public void testListBlobsSelectedFields () throws InterruptedException {
292293 String [] blobNames = {"test-list-blobs-selected-fields-blob1" ,
293294 "test-list-blobs-selected-fields-blob2" };
294295 ImmutableMap <String , String > metadata = ImmutableMap .of ("k" , "v" );
@@ -307,19 +308,27 @@ public void testListBlobsSelectedFields() {
307308 Page <Blob > page = storage .list (BUCKET ,
308309 Storage .BlobListOption .prefix ("test-list-blobs-selected-fields-blob" ),
309310 Storage .BlobListOption .fields (BlobField .METADATA ));
310- int index = 0 ;
311+ // Listing blobs is eventually consistent, we loop until the list is of the expected size. The
312+ // test fails if timeout is reached.
313+ while (Iterators .size (page .values ().iterator ()) != 2 ) {
314+ Thread .sleep (500 );
315+ page = storage .list (BUCKET ,
316+ Storage .BlobListOption .prefix ("test-list-blobs-selected-fields-blob" ),
317+ Storage .BlobListOption .fields (BlobField .METADATA ));
318+ }
319+ Set <String > blobSet = ImmutableSet .of (blobNames [0 ], blobNames [1 ]);
311320 for (Blob remoteBlob : page .values ()) {
312321 assertEquals (BUCKET , remoteBlob .bucket ());
313- assertEquals ( blobNames [ index ++], remoteBlob .name ());
322+ assertTrue ( blobSet . contains ( remoteBlob .name () ));
314323 assertEquals (metadata , remoteBlob .metadata ());
315324 assertNull (remoteBlob .contentType ());
316325 }
317326 assertTrue (remoteBlob1 .delete ());
318327 assertTrue (remoteBlob2 .delete ());
319328 }
320329
321- @ Test
322- public void testListBlobsEmptySelectedFields () {
330+ @ Test ( timeout = 5000 )
331+ public void testListBlobsEmptySelectedFields () throws InterruptedException {
323332 String [] blobNames = {"test-list-blobs-empty-selected-fields-blob1" ,
324333 "test-list-blobs-empty-selected-fields-blob2" };
325334 BlobInfo blob1 = BlobInfo .builder (BUCKET , blobNames [0 ])
@@ -335,17 +344,25 @@ public void testListBlobsEmptySelectedFields() {
335344 Page <Blob > page = storage .list (BUCKET ,
336345 Storage .BlobListOption .prefix ("test-list-blobs-empty-selected-fields-blob" ),
337346 Storage .BlobListOption .fields ());
338- int index = 0 ;
347+ // Listing blobs is eventually consistent, we loop until the list is of the expected size. The
348+ // test fails if timeout is reached.
349+ while (Iterators .size (page .values ().iterator ()) != 2 ) {
350+ Thread .sleep (500 );
351+ page = storage .list (BUCKET ,
352+ Storage .BlobListOption .prefix ("test-list-blobs-empty-selected-fields-blob" ),
353+ Storage .BlobListOption .fields ());
354+ }
355+ Set <String > blobSet = ImmutableSet .of (blobNames [0 ], blobNames [1 ]);
339356 for (Blob remoteBlob : page .values ()) {
340357 assertEquals (BUCKET , remoteBlob .bucket ());
341- assertEquals ( blobNames [ index ++], remoteBlob .name ());
358+ assertTrue ( blobSet . contains ( remoteBlob .name () ));
342359 assertNull (remoteBlob .contentType ());
343360 }
344361 assertTrue (remoteBlob1 .delete ());
345362 assertTrue (remoteBlob2 .delete ());
346363 }
347364
348- @ Test
365+ @ Test ( timeout = 10000 )
349366 public void testListBlobsVersioned () throws ExecutionException , InterruptedException {
350367 String bucketName = RemoteGcsHelper .generateBucketName ();
351368 Bucket bucket = storage .create (BucketInfo .builder (bucketName ).versioningEnabled (true ).build ());
@@ -366,6 +383,14 @@ public void testListBlobsVersioned() throws ExecutionException, InterruptedExcep
366383 Page <Blob > page = storage .list (bucketName ,
367384 Storage .BlobListOption .prefix ("test-list-blobs-versioned-blob" ),
368385 Storage .BlobListOption .versions (true ));
386+ // Listing blobs is eventually consistent, we loop until the list is of the expected size. The
387+ // test fails if timeout is reached.
388+ while (Iterators .size (page .values ().iterator ()) != 3 ) {
389+ Thread .sleep (500 );
390+ page = storage .list (bucketName ,
391+ Storage .BlobListOption .prefix ("test-list-blobs-versioned-blob" ),
392+ Storage .BlobListOption .versions (true ));
393+ }
369394 Set <String > blobSet = ImmutableSet .of (blobNames [0 ], blobNames [1 ]);
370395 for (Blob remoteBlob : page .values ()) {
371396 assertEquals (bucketName , remoteBlob .bucket ());
0 commit comments