Skip to content

Commit 3e6852e

Browse files
authored
chore: misc test cleanup (#1899)
* Remove DataGeneration rule in favor of Generator fixture from StorageITRunner * Use import on @BucketFixtureAnnotations * Update ITBucketTest#testListBucket to not perform manual retries (Listing buckets is a strongly consistent operation)
1 parent be2aba0 commit 3e6852e

File tree

4 files changed

+25
-113
lines changed

4 files changed

+25
-113
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/DataGeneration.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBlobWriteChannelTest.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.google.cloud.storage.BlobId;
3535
import com.google.cloud.storage.BlobInfo;
3636
import com.google.cloud.storage.BucketInfo;
37-
import com.google.cloud.storage.DataGeneration;
37+
import com.google.cloud.storage.DataGenerator;
3838
import com.google.cloud.storage.HttpStorageOptions;
3939
import com.google.cloud.storage.PackagePrivateMethodWorkarounds;
4040
import com.google.cloud.storage.Storage;
@@ -58,10 +58,8 @@
5858
import java.io.IOException;
5959
import java.nio.ByteBuffer;
6060
import java.util.Optional;
61-
import java.util.Random;
6261
import java.util.concurrent.atomic.AtomicBoolean;
6362
import java.util.logging.Logger;
64-
import org.junit.Rule;
6563
import org.junit.Test;
6664
import org.junit.runner.RunWith;
6765
import org.threeten.bp.Clock;
@@ -89,8 +87,6 @@ public final class ITBlobWriteChannelTest {
8987

9088
@Inject public Generator generator;
9189

92-
@Rule public final DataGeneration dataGeneration = new DataGeneration(new Random(1234567890));
93-
9490
/**
9591
* Test for unexpected EOF at the beginning of trying to read the json response.
9692
*
@@ -146,9 +142,9 @@ public void testWriteChannelExistingBlob() throws IOException {
146142
DateTimeFormatter formatter =
147143
DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC));
148144
String nowString = formatter.format(now);
149-
BucketInfo bucketInfo = BucketInfo.of(dataGeneration.getBucketName());
145+
BucketInfo bucketInfo = BucketInfo.of(generator.randomBucketName());
150146
String blobPath = String.format("%s/%s/blob", generator.randomObjectName(), nowString);
151-
BlobId blobId = BlobId.of(dataGeneration.getBucketName(), blobPath);
147+
BlobId blobId = BlobId.of(bucketInfo.getName(), blobPath);
152148
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
153149
storage.create(bucketInfo);
154150
storage.create(blobInfo);
@@ -164,7 +160,7 @@ public void testWriteChannelExistingBlob() throws IOException {
164160
private void doJsonUnexpectedEOFTest(int contentSize, int cappedByteCount) throws IOException {
165161
String blobPath = String.format("%s/%s/blob", generator.randomObjectName(), NOW_STRING);
166162

167-
BucketInfo bucketInfo = BucketInfo.of(dataGeneration.getBucketName());
163+
BucketInfo bucketInfo = BucketInfo.of(generator.randomBucketName());
168164
BlobInfo blobInfoGen0 = BlobInfo.newBuilder(bucketInfo, blobPath, 0L).build();
169165

170166
RetryTestResource retryTestResource =
@@ -225,7 +221,7 @@ private void doJsonUnexpectedEOFTest(int contentSize, int cappedByteCount) throw
225221

226222
testStorage.create(bucketInfo);
227223

228-
ByteBuffer content = dataGeneration.randByteBuffer(contentSize);
224+
ByteBuffer content = DataGenerator.base64Characters().genByteBuffer(contentSize);
229225
// create a duplicate to preserve the initial offset and limit for assertion later
230226
ByteBuffer expected = content.duplicate();
231227

@@ -260,13 +256,13 @@ private void blobWriteChannel_handlesRecoveryOnLastChunkWhenGenerationIsPresent(
260256
DateTimeFormatter formatter =
261257
DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC));
262258
String nowString = formatter.format(now);
263-
BucketInfo bucketInfo = BucketInfo.of(dataGeneration.getBucketName());
259+
BucketInfo bucketInfo = BucketInfo.of(generator.randomBucketName());
264260
String blobPath = String.format("%s/%s/blob", generator.randomObjectName(), nowString);
265-
BlobId blobId = BlobId.of(dataGeneration.getBucketName(), blobPath);
261+
BlobId blobId = BlobId.of(bucketInfo.getName(), blobPath);
266262
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
267263

268-
ByteBuffer contentGen1 = dataGeneration.randByteBuffer(contentSize);
269-
ByteBuffer contentGen2 = dataGeneration.randByteBuffer(contentSize);
264+
ByteBuffer contentGen1 = DataGenerator.base64Characters().genByteBuffer(contentSize);
265+
ByteBuffer contentGen2 = DataGenerator.base64Characters().genByteBuffer(contentSize);
270266
ByteBuffer contentGen2Expected = contentGen2.duplicate();
271267
HttpStorageOptions baseStorageOptions =
272268
StorageOptions.http()
@@ -302,7 +298,6 @@ private void blobWriteChannel_handlesRecoveryOnLastChunkWhenGenerationIsPresent(
302298
* get on an object can result in a 404 because the object that is created while
303299
* the BlobWriteChannel is executing will be a new generation.
304300
*/
305-
@SuppressWarnings("UnstableApiUsage")
306301
@Override
307302
public StorageRpc create(final StorageOptions options) {
308303
return Reflection.newProxy(

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.Assert.assertNull;
2626
import static org.junit.Assert.assertTrue;
2727

28+
import com.google.api.gax.paging.Page;
2829
import com.google.cloud.Policy;
2930
import com.google.cloud.storage.Blob;
3031
import com.google.cloud.storage.BlobInfo;
@@ -38,10 +39,12 @@
3839
import com.google.cloud.storage.Storage;
3940
import com.google.cloud.storage.Storage.BlobField;
4041
import com.google.cloud.storage.Storage.BucketField;
42+
import com.google.cloud.storage.Storage.BucketListOption;
4143
import com.google.cloud.storage.Storage.BucketTargetOption;
4244
import com.google.cloud.storage.TransportCompatibility.Transport;
4345
import com.google.cloud.storage.it.runner.StorageITRunner;
4446
import com.google.cloud.storage.it.runner.annotations.Backend;
47+
import com.google.cloud.storage.it.runner.annotations.BucketFixture;
4548
import com.google.cloud.storage.it.runner.annotations.BucketType;
4649
import com.google.cloud.storage.it.runner.annotations.CrossRun;
4750
import com.google.cloud.storage.it.runner.annotations.Inject;
@@ -52,9 +55,9 @@
5255
import java.time.OffsetDateTime;
5356
import java.util.ArrayList;
5457
import java.util.Collections;
55-
import java.util.Iterator;
5658
import java.util.List;
5759
import java.util.Map;
60+
import java.util.stream.StreamSupport;
5861
import org.junit.Ignore;
5962
import org.junit.Test;
6063
import org.junit.runner.RunWith;
@@ -71,39 +74,26 @@ public class ITBucketTest {
7174
private static final Duration RETENTION_DURATION = Duration.ofSeconds(5);
7275

7376
@Inject
74-
@com.google.cloud.storage.it.runner.annotations.BucketFixture(BucketType.DEFAULT)
77+
@BucketFixture(BucketType.DEFAULT)
7578
public BucketInfo bucket;
7679

7780
@Inject
78-
@com.google.cloud.storage.it.runner.annotations.BucketFixture(BucketType.REQUESTER_PAYS)
81+
@BucketFixture(BucketType.REQUESTER_PAYS)
7982
public BucketInfo requesterPaysBucket;
8083

8184
@Inject public Storage storage;
8285
@Inject public Generator generator;
8386

84-
@Test(timeout = 5000)
85-
public void testListBuckets() throws InterruptedException {
86-
Iterator<Bucket> bucketIterator =
87-
storage
88-
.list(
89-
Storage.BucketListOption.prefix(bucket.getName()),
90-
Storage.BucketListOption.fields())
91-
.iterateAll()
92-
.iterator();
93-
while (!bucketIterator.hasNext()) {
94-
Thread.sleep(500);
95-
bucketIterator =
96-
storage
97-
.list(
98-
Storage.BucketListOption.prefix(bucket.getName()),
99-
Storage.BucketListOption.fields())
100-
.iterateAll()
101-
.iterator();
102-
}
103-
while (bucketIterator.hasNext()) {
104-
Bucket remoteBucket = bucketIterator.next();
105-
assertTrue(remoteBucket.getName().startsWith(bucket.getName()));
106-
}
87+
@Test
88+
public void testListBuckets() {
89+
Page<Bucket> page =
90+
storage.list(
91+
BucketListOption.prefix(bucket.getName()), BucketListOption.fields(BucketField.NAME));
92+
ImmutableList<String> bucketNames =
93+
StreamSupport.stream(page.iterateAll().spliterator(), false)
94+
.map(BucketInfo::getName)
95+
.collect(ImmutableList.toImmutableList());
96+
assertThat(bucketNames).contains(bucket.getName());
10797
}
10898

10999
@Test

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.cloud.storage.Bucket;
3232
import com.google.cloud.storage.BucketInfo;
3333
import com.google.cloud.storage.CopyWriter;
34-
import com.google.cloud.storage.DataGeneration;
3534
import com.google.cloud.storage.Storage;
3635
import com.google.cloud.storage.Storage.BlobField;
3736
import com.google.cloud.storage.StorageException;
@@ -47,9 +46,7 @@
4746
import java.io.IOException;
4847
import java.nio.ByteBuffer;
4948
import java.security.Key;
50-
import java.util.Random;
5149
import javax.crypto.spec.SecretKeySpec;
52-
import org.junit.Rule;
5350
import org.junit.Test;
5451
import org.junit.runner.RunWith;
5552

@@ -59,9 +56,6 @@
5956
backends = Backend.PROD)
6057
public class ITKmsTest {
6158

62-
private static final long seed = -7071346537822433445L;
63-
@Rule public final DataGeneration dataGeneration = new DataGeneration(new Random(seed));
64-
6559
private static final byte[] BLOB_BYTE_CONTENT = {0xD, 0xE, 0xA, 0xD};
6660
private static final String BLOB_STRING_CONTENT = "Hello Google Cloud Storage!";
6761
private static final String BASE64_KEY = "JVzfVl8NLD9FjedFuStegjRfES5ll5zc59CIXw572OA=";

0 commit comments

Comments
 (0)