-
Notifications
You must be signed in to change notification settings - Fork 70
/
ITStorageReadChannelTest.java
175 lines (153 loc) · 6.57 KB
/
ITStorageReadChannelTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage.it;
import static com.google.cloud.storage.PackagePrivateMethodWorkarounds.getBlobInfoFromReadChannelFunction;
import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.cloud.storage.TestUtils.gzipBytes;
import static com.google.cloud.storage.TestUtils.xxd;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.api.core.ApiFuture;
import com.google.cloud.ReadChannel;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.DataGenerator;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.TransportCompatibility.Transport;
import com.google.cloud.storage.it.runner.StorageITRunner;
import com.google.cloud.storage.it.runner.annotations.Backend;
import com.google.cloud.storage.it.runner.annotations.CrossRun;
import com.google.cloud.storage.it.runner.annotations.Inject;
import com.google.cloud.storage.it.runner.registry.Generator;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(StorageITRunner.class)
@CrossRun(
backends = Backend.PROD,
transports = {Transport.HTTP, Transport.GRPC})
public final class ITStorageReadChannelTest {
@Inject public Storage storage;
@Inject public BucketInfo bucket;
@Inject public Generator generator;
@Test
public void storageReadChannel_getObject_returns() throws Exception {
int _512KiB = 512 * 1024;
int _1MiB = 1024 * 1024;
final BlobInfo info;
ChecksummedTestContent content;
{
byte[] uncompressedBytes = DataGenerator.base64Characters().genBytes(_512KiB);
byte[] gzipBytes = gzipBytes(uncompressedBytes);
content = ChecksummedTestContent.of(gzipBytes);
BlobInfo tmp =
BlobInfo.newBuilder(bucket, generator.randomObjectName())
// define an object with explicit content type and encoding.
// JSON and gRPC have differing default behavior returning these values if they are
// either undefined, or match HTTP defaults.
.setContentType("text/plain")
.setContentEncoding("gzip")
.build();
Blob gen1 = storage.create(tmp, content.getBytes(), BlobTargetOption.doesNotExist());
info = gen1.asBlobInfo();
}
try (ReadChannel c =
storage.reader(info.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(true))) {
ApiFuture<BlobInfo> infoFuture = getBlobInfoFromReadChannelFunction(c);
ByteBuffer buf = ByteBuffer.allocate(_1MiB);
c.read(buf);
String actual = xxd(buf);
String expected = xxd(content.getBytes());
assertThat(actual).isEqualTo(expected);
BlobInfo blobInfo = infoFuture.get(3, TimeUnit.SECONDS);
assertAll(
() -> equalForField(blobInfo, info, BlobInfo::getName),
() -> equalForField(blobInfo, info, BlobInfo::getBucket),
() -> equalForField(blobInfo, info, BlobInfo::getGeneration),
() -> equalForField(blobInfo, info, BlobInfo::getMetageneration),
() -> equalForField(blobInfo, info, BlobInfo::getSize),
() -> equalForField(blobInfo, info, BlobInfo::getContentType),
() -> equalForField(blobInfo, info, BlobInfo::getContentEncoding));
}
}
@Test
// @CrossRun.Exclude(transports = Transport.GRPC)
public void storageReadChannel_shouldAllowDisablingBufferingBySettingChunkSize_lteq0()
throws IOException {
int _512KiB = 512 * 1024;
int _1MiB = 1024 * 1024;
final BlobInfo info;
byte[] uncompressedBytes = DataGenerator.base64Characters().genBytes(_512KiB);
{
BlobInfo tmp = BlobInfo.newBuilder(bucket, generator.randomObjectName()).build();
Blob gen1 = storage.create(tmp, uncompressedBytes, BlobTargetOption.doesNotExist());
info = gen1.asBlobInfo();
}
try (ReadChannel c = storage.reader(info.getBlobId())) {
c.setChunkSize(0);
ByteBuffer buf = ByteBuffer.allocate(_1MiB);
// Because this is unbuffered, the underlying channel will not necessarily fill up the buf
// in a single read call. Repeatedly read until full or EOF.
int read = fillFrom(buf, c);
assertThat(read).isEqualTo(_512KiB);
String actual = xxd(buf);
String expected = xxd(uncompressedBytes);
assertThat(actual).isEqualTo(expected);
}
}
@Test
public void storageReadChannel_getObject_404() {
BlobId id = BlobId.of(bucket.getName(), generator.randomObjectName());
try (ReadChannel c = storage.reader(id)) {
ApiFuture<BlobInfo> infoFuture = getBlobInfoFromReadChannelFunction(c);
IOException ioException =
assertThrows(IOException.class, () -> c.read(ByteBuffer.allocate(10)));
assertThat(ioException).hasCauseThat().isInstanceOf(StorageException.class);
ExecutionException ee =
assertThrows(ExecutionException.class, () -> infoFuture.get(3, TimeUnit.SECONDS));
assertThat(ee).hasCauseThat().isInstanceOf(StorageException.class);
StorageException cause = (StorageException) ee.getCause();
assertThat(cause.getCode()).isEqualTo(404);
}
}
private static <T, F> void equalForField(T actual, T expected, Function<T, F> f) {
F aF = f.apply(actual);
F eF = f.apply(expected);
assertThat(aF).isEqualTo(eF);
}
static int fillFrom(ByteBuffer buf, ReadableByteChannel c) throws IOException {
int total = 0;
while (buf.hasRemaining()) {
int read = c.read(buf);
if (read != -1) {
total += read;
} else {
break;
}
}
return total;
}
}