|
16 | 16 |
|
17 | 17 | package com.google.cloud.storage.it;
|
18 | 18 |
|
| 19 | +import static com.google.cloud.storage.TestUtils.assertAll; |
19 | 20 | import static com.google.common.truth.Truth.assertThat;
|
20 | 21 | import static org.junit.Assert.assertEquals;
|
21 |
| -import static org.junit.Assert.assertFalse; |
22 | 22 | import static org.junit.Assert.assertNotNull;
|
23 |
| -import static org.junit.Assert.assertNull; |
24 | 23 | import static org.junit.Assert.assertTrue;
|
25 | 24 | import static org.junit.Assert.fail;
|
26 | 25 |
|
|
29 | 28 | import com.google.cloud.storage.BlobInfo;
|
30 | 29 | import com.google.cloud.storage.BucketInfo;
|
31 | 30 | import com.google.cloud.storage.Storage;
|
| 31 | +import com.google.cloud.storage.Storage.BlobTargetOption; |
32 | 32 | import com.google.cloud.storage.StorageBatch;
|
33 | 33 | import com.google.cloud.storage.StorageBatchResult;
|
34 | 34 | import com.google.cloud.storage.StorageException;
|
|
38 | 38 | import com.google.cloud.storage.it.runner.annotations.Inject;
|
39 | 39 | import com.google.cloud.storage.it.runner.annotations.SingleBackend;
|
40 | 40 | import com.google.cloud.storage.it.runner.annotations.StorageFixture;
|
41 |
| -import com.google.common.collect.Lists; |
42 |
| -import java.util.List; |
| 41 | +import com.google.cloud.storage.it.runner.registry.Generator; |
| 42 | +import com.google.common.collect.ImmutableMap; |
43 | 43 | import org.junit.Before;
|
44 | 44 | import org.junit.Test;
|
45 | 45 | import org.junit.runner.RunWith;
|
46 | 46 |
|
47 | 47 | @RunWith(StorageITRunner.class)
|
48 | 48 | @SingleBackend(Backend.PROD)
|
49 | 49 | public class ITBatchTest {
|
50 |
| - private static final int MAX_BATCH_SIZE = 100; |
51 | 50 | private static final String CONTENT_TYPE = "text/plain";
|
52 | 51 |
|
53 | 52 | @Inject
|
54 | 53 | @StorageFixture(Transport.HTTP)
|
55 | 54 | public Storage storage;
|
56 | 55 |
|
57 | 56 | @Inject public BucketInfo bucket;
|
| 57 | + @Inject public Generator generator; |
58 | 58 |
|
59 | 59 | private String bucketName;
|
60 | 60 |
|
@@ -108,67 +108,56 @@ public void testBatchRequest() {
|
108 | 108 | }
|
109 | 109 |
|
110 | 110 | @Test
|
111 |
| - public void testBatchRequestManyOperations() { |
112 |
| - List<StorageBatchResult<Boolean>> deleteResults = |
113 |
| - Lists.newArrayListWithCapacity(MAX_BATCH_SIZE); |
114 |
| - List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2); |
115 |
| - List<StorageBatchResult<Blob>> updateResults = |
116 |
| - Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2); |
| 111 | + public void testBatchRequestManyOperations() throws Exception { |
| 112 | + // define some object ids for use in the batch operations |
| 113 | + BlobId id1 = BlobId.of(bucketName, generator.randomObjectName()); |
| 114 | + BlobId id2 = BlobId.of(bucketName, generator.randomObjectName()); |
| 115 | + BlobId id3 = BlobId.of(bucketName, generator.randomObjectName()); |
| 116 | + BlobId id4 = BlobId.of(bucketName, generator.randomObjectName()); |
| 117 | + BlobId id5 = BlobId.of(bucketName, generator.randomObjectName()); |
| 118 | + |
| 119 | + ImmutableMap<String, String> ka = ImmutableMap.of("k", "a"); |
| 120 | + ImmutableMap<String, String> kB = ImmutableMap.of("k", "B"); |
| 121 | + |
| 122 | + // Create objects which exist before the batch operations |
| 123 | + BlobInfo info1 = BlobInfo.newBuilder(id1).setMetadata(ka).build(); |
| 124 | + BlobInfo info2 = BlobInfo.newBuilder(id2).setMetadata(ka).build(); |
| 125 | + BlobInfo info3 = BlobInfo.newBuilder(id3).setMetadata(ka).build(); |
| 126 | + Blob obj1 = storage.create(info1, BlobTargetOption.doesNotExist()); |
| 127 | + Blob obj2 = storage.create(info2, BlobTargetOption.doesNotExist()); |
| 128 | + Blob obj3 = storage.create(info3, BlobTargetOption.doesNotExist()); |
| 129 | + |
| 130 | + // Define our batch operations |
117 | 131 | StorageBatch batch = storage.batch();
|
118 |
| - for (int i = 0; i < MAX_BATCH_SIZE; i++) { |
119 |
| - BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i); |
120 |
| - deleteResults.add(batch.delete(blobId)); |
121 |
| - } |
122 |
| - for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) { |
123 |
| - BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i); |
124 |
| - getResults.add(batch.get(blobId)); |
125 |
| - } |
126 |
| - for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) { |
127 |
| - BlobInfo blob = |
128 |
| - BlobInfo.newBuilder(BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i)) |
129 |
| - .build(); |
130 |
| - updateResults.add(batch.update(blob)); |
131 |
| - } |
132 |
| - |
133 |
| - String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1"; |
134 |
| - String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2"; |
135 |
| - BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucketName, sourceBlobName1).build(); |
136 |
| - BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucketName, sourceBlobName2).build(); |
137 |
| - assertNotNull(storage.create(sourceBlob1)); |
138 |
| - assertNotNull(storage.create(sourceBlob2)); |
139 |
| - BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build(); |
140 | 132 |
|
141 |
| - StorageBatchResult<Blob> getResult = batch.get(bucketName, sourceBlobName1); |
142 |
| - StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2); |
| 133 | + StorageBatchResult<Blob> get1Success = batch.get(id1); |
| 134 | + StorageBatchResult<Blob> update2Success = |
| 135 | + batch.update( |
| 136 | + obj2.toBuilder().setMetadata(kB).build(), BlobTargetOption.metagenerationMatch()); |
| 137 | + StorageBatchResult<Boolean> delete3Success = batch.delete(id3); |
| 138 | + StorageBatchResult<Blob> get4Error = batch.get(id4); |
| 139 | + StorageBatchResult<Boolean> delete5Error = batch.delete(id5); |
143 | 140 |
|
| 141 | + // submit the batch |
144 | 142 | batch.submit();
|
145 | 143 |
|
146 |
| - // Check deletes |
147 |
| - for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) { |
148 |
| - assertFalse(failedDeleteResult.get()); |
149 |
| - } |
150 |
| - |
151 |
| - // Check gets |
152 |
| - for (StorageBatchResult<Blob> failedGetResult : getResults) { |
153 |
| - assertNull(failedGetResult.get()); |
154 |
| - } |
155 |
| - Blob remoteBlob1 = getResult.get(); |
156 |
| - assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket()); |
157 |
| - assertEquals(sourceBlob1.getName(), remoteBlob1.getName()); |
158 |
| - |
159 |
| - // Check updates |
160 |
| - for (StorageBatchResult<Blob> failedUpdateResult : updateResults) { |
161 |
| - try { |
162 |
| - failedUpdateResult.get(); |
163 |
| - fail("Expected StorageException"); |
164 |
| - } catch (StorageException ex) { |
165 |
| - // expected |
166 |
| - } |
167 |
| - } |
168 |
| - Blob remoteUpdatedBlob2 = updateResult.get(); |
169 |
| - assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket()); |
170 |
| - assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName()); |
171 |
| - assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType()); |
| 144 | + // verify our expected results |
| 145 | + assertAll( |
| 146 | + () -> { |
| 147 | + Blob blob = get1Success.get(); |
| 148 | + assertThat(blob.getBucket()).isEqualTo(bucketName); |
| 149 | + assertThat(blob.getName()).isEqualTo(id1.getName()); |
| 150 | + assertThat(blob.getMetadata()).isEqualTo(ka); |
| 151 | + }, |
| 152 | + () -> { |
| 153 | + Blob blob = update2Success.get(); |
| 154 | + assertThat(blob.getBucket()).isEqualTo(bucketName); |
| 155 | + assertThat(blob.getName()).isEqualTo(id2.getName()); |
| 156 | + assertThat(blob.getMetadata()).isEqualTo(kB); |
| 157 | + }, |
| 158 | + () -> assertThat(delete3Success.get()).isTrue(), |
| 159 | + () -> assertThat(get4Error.get()).isNull(), |
| 160 | + () -> assertThat(delete5Error.get()).isFalse()); |
172 | 161 | }
|
173 | 162 |
|
174 | 163 | @Test
|
|
0 commit comments