16
16
17
17
package com .google .cloud .storage .conformance .retry ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
19
20
import static java .util .Objects .requireNonNull ;
20
21
import static org .junit .Assert .assertNotNull ;
21
22
22
23
import com .google .auth .ServiceAccountSigner ;
23
24
import com .google .auth .oauth2 .ServiceAccountCredentials ;
24
25
import com .google .cloud .conformance .storage .v1 .InstructionList ;
25
26
import com .google .cloud .conformance .storage .v1 .Method ;
27
+ import com .google .cloud .storage .DataGenerator ;
26
28
import com .google .common .base .Joiner ;
27
29
import com .google .common .base .Suppliers ;
30
+ import com .google .common .io .ByteStreams ;
28
31
import com .google .errorprone .annotations .Immutable ;
32
+ import java .io .ByteArrayInputStream ;
33
+ import java .io .File ;
34
+ import java .io .FileOutputStream ;
29
35
import java .io .IOException ;
30
36
import java .io .InputStream ;
31
- import java .net .URISyntaxException ;
32
- import java .net .URL ;
33
37
import java .nio .charset .StandardCharsets ;
34
38
import java .nio .file .Path ;
35
- import java .nio .file .Paths ;
36
39
import java .time .Clock ;
37
40
import java .time .Instant ;
38
41
import java .time .ZoneId ;
39
42
import java .time .ZoneOffset ;
40
43
import java .time .format .DateTimeFormatter ;
41
44
import java .util .function .Supplier ;
42
45
import java .util .stream .Collectors ;
43
- import java .util .stream .IntStream ;
44
46
45
47
/**
46
48
* An individual resolved test case correlating config from {@link
@@ -72,9 +74,8 @@ final class TestRetryConformance {
72
74
private final String topicName ;
73
75
74
76
private final Supplier <byte []> lazyHelloWorldUtf8Bytes ;
75
- private final Path helloWorldFilePath = resolvePathForResource ();
76
- private final ServiceAccountCredentials serviceAccountCredentials =
77
- resolveServiceAccountCredentials ();
77
+ private final Supplier <Path > helloWorldFilePath ;
78
+ private final ServiceAccountCredentials serviceAccountCredentials ;
78
79
79
80
private final String host ;
80
81
@@ -137,33 +138,14 @@ final class TestRetryConformance {
137
138
String .format (
138
139
"%s_s%03d-%s-m%03d_top1" ,
139
140
BASE_ID , scenarioId , instructionsString .toLowerCase (), mappingId );
140
- lazyHelloWorldUtf8Bytes =
141
+ this . lazyHelloWorldUtf8Bytes =
141
142
Suppliers .memoize (
142
143
() -> {
143
144
// define a lazy supplier for bytes.
144
- // Not all tests need data for an object, though some tests - resumable upload - needs
145
- // more than 8MiB.
146
- // We want to avoid allocating 8.1MiB for each test unnecessarily, especially since we
147
- // instantiate all permuted test cases. ~1000 * 8.1MiB ~~ > 8GiB.
148
- String helloWorld = "Hello, World!" ;
149
- int baseDataSize ;
150
- switch (method .getName ()) {
151
- case "storage.objects.insert" :
152
- baseDataSize = _8MiB + 1 ;
153
- break ;
154
- case "storage.objects.get" :
155
- baseDataSize = _512KiB ;
156
- break ;
157
- default :
158
- baseDataSize = helloWorld .length ();
159
- break ;
160
- }
161
- int endInclusive = (baseDataSize / helloWorld .length ());
162
- return IntStream .rangeClosed (1 , endInclusive )
163
- .mapToObj (i -> helloWorld )
164
- .collect (Collectors .joining ())
165
- .getBytes (StandardCharsets .UTF_8 );
145
+ return genBytes (method );
166
146
});
147
+ this .helloWorldFilePath = resolvePathForResource (objectName , method );
148
+ this .serviceAccountCredentials = resolveServiceAccountCredentials ();
167
149
}
168
150
169
151
public String getProjectId () {
@@ -195,7 +177,7 @@ public byte[] getHelloWorldUtf8Bytes() {
195
177
}
196
178
197
179
public Path getHelloWorldFilePath () {
198
- return helloWorldFilePath ;
180
+ return helloWorldFilePath . get () ;
199
181
}
200
182
201
183
public int getScenarioId () {
@@ -238,15 +220,24 @@ public String toString() {
238
220
return getTestName ();
239
221
}
240
222
241
- private static Path resolvePathForResource () {
242
- ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
243
- URL url = cl .getResource ("com/google/cloud/storage/conformance/retry/hello-world.txt" );
244
- assertNotNull (url );
245
- try {
246
- return Paths .get (url .toURI ());
247
- } catch (URISyntaxException e ) {
248
- throw new RuntimeException (e );
249
- }
223
+ private static Supplier <Path > resolvePathForResource (String objectName , Method method ) {
224
+ return () -> {
225
+ try {
226
+ File tempFile = File .createTempFile (objectName , "" );
227
+ tempFile .deleteOnExit ();
228
+
229
+ byte [] bytes = genBytes (method );
230
+ try (ByteArrayInputStream in = new ByteArrayInputStream (bytes );
231
+ FileOutputStream out = new FileOutputStream (tempFile )) {
232
+ long copy = ByteStreams .copy (in , out );
233
+ assertThat (copy ).isEqualTo (bytes .length );
234
+ }
235
+
236
+ return tempFile .toPath ();
237
+ } catch (IOException e ) {
238
+ throw new RuntimeException (e );
239
+ }
240
+ };
250
241
}
251
242
252
243
private static ServiceAccountCredentials resolveServiceAccountCredentials () {
@@ -265,4 +256,19 @@ private static ServiceAccountCredentials resolveServiceAccountCredentials() {
265
256
public String getTopicName () {
266
257
return topicName ;
267
258
}
259
+
260
+ private static byte [] genBytes (Method method ) {
261
+ // Not all tests need data for an object, though some tests - resumable upload - needs
262
+ // more than 8MiB.
263
+ // We want to avoid allocating 8.1MiB for each test unnecessarily, especially since we
264
+ // instantiate all permuted test cases. ~1000 * 8.1MiB ~~ > 8GiB.
265
+ switch (method .getName ()) {
266
+ case "storage.objects.insert" :
267
+ return DataGenerator .base64Characters ().genBytes (_8MiB * 2 + _512KiB );
268
+ case "storage.objects.get" :
269
+ return DataGenerator .base64Characters ().genBytes (_512KiB );
270
+ default :
271
+ return "Hello, World!" .getBytes (StandardCharsets .UTF_8 );
272
+ }
273
+ }
268
274
}
0 commit comments