use of com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource in project java-storage by googleapis.
the class ITBlobWriteChannelTest method doJsonUnexpectedEOFTest.
private void doJsonUnexpectedEOFTest(int contentSize, int cappedByteCount) throws IOException {
String blobPath = String.format("%s/%s/blob", testName.getMethodName(), NOW_STRING);
BucketInfo bucketInfo = BucketInfo.of(dataGeneration.getBucketName());
BlobInfo blobInfoGen0 = BlobInfo.newBuilder(bucketInfo, blobPath, 0L).build();
RetryTestResource retryTestResource = RetryTestResource.newRetryTestResource(Method.newBuilder().setName("storage.objects.insert").build(), InstructionList.newBuilder().addInstructions(String.format("return-broken-stream-final-chunk-after-%dB", cappedByteCount)).build());
RetryTestResource retryTest = testBench.createRetryTest(retryTestResource);
StorageOptions baseOptions = StorageOptions.newBuilder().setCredentials(NoCredentials.getInstance()).setHost(testBench.getBaseUri()).setProjectId("project-id").build();
StorageRpc noHeader = (StorageRpc) baseOptions.getRpc();
StorageRpc yesHeader = (StorageRpc) baseOptions.toBuilder().setHeaderProvider(FixedHeaderProvider.create(ImmutableMap.of("x-retry-test-id", retryTest.id))).build().getRpc();
// noinspection UnstableApiUsage
StorageOptions storageOptions = baseOptions.toBuilder().setServiceRpcFactory(options -> Reflection.newProxy(StorageRpc.class, (proxy, method, args) -> {
try {
if ("writeWithResponse".equals(method.getName())) {
boolean lastChunk = (boolean) args[5];
LOGGER.info(String.format("writeWithResponse called. (lastChunk = %b)", lastChunk));
if (lastChunk) {
return method.invoke(yesHeader, args);
}
}
return method.invoke(noHeader, args);
} catch (Exception e) {
if (e.getCause() != null) {
throw e.getCause();
} else {
throw e;
}
}
})).build();
Storage testStorage = storageOptions.getService();
testStorage.create(bucketInfo);
ByteBuffer content = dataGeneration.randByteBuffer(contentSize);
// create a duplicate to preserve the initial offset and limit for assertion later
ByteBuffer expected = content.duplicate();
WriteChannel w = testStorage.writer(blobInfoGen0, BlobWriteOption.generationMatch());
w.write(content);
w.close();
RetryTestResource postRunState = testBench.getRetryTest(retryTest);
assertTrue(postRunState.completed);
Optional<StorageObject> optionalStorageObject = PackagePrivateMethodWorkarounds.maybeGetStorageObjectFunction().apply(w);
assertTrue(optionalStorageObject.isPresent());
StorageObject storageObject = optionalStorageObject.get();
assertThat(storageObject.getName()).isEqualTo(blobInfoGen0.getName());
// construct a new blob id, without a generation, so we get the latest when we perform a get
BlobId blobIdGen1 = BlobId.of(storageObject.getBucket(), storageObject.getName());
Blob blobGen2 = testStorage.get(blobIdGen1);
assertEquals(contentSize, (long) blobGen2.getSize());
assertNotEquals(blobInfoGen0.getGeneration(), blobGen2.getGeneration());
ByteArrayOutputStream actualData = new ByteArrayOutputStream();
blobGen2.downloadTo(actualData);
ByteBuffer actual = ByteBuffer.wrap(actualData.toByteArray());
assertEquals(expected, actual);
}
use of com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource in project java-storage by googleapis.
the class RetryTestFixture method apply.
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
boolean testSuccess = false;
boolean testSkipped = false;
try {
LOGGER.fine("Setting up retry_test resource...");
RetryTestResource retryTestResource = RetryTestResource.newRetryTestResource(testRetryConformance.getMethod(), testRetryConformance.getInstruction());
retryTest = testBench.createRetryTest(retryTestResource);
LOGGER.fine("Setting up retry_test resource complete");
base.evaluate();
testSuccess = true;
} catch (AssumptionViolatedException e) {
testSkipped = true;
throw e;
} finally {
LOGGER.fine("Verifying end state of retry_test resource...");
try {
if (retryTest != null) {
RetryTestResource postTestState = testBench.getRetryTest(retryTest);
if (testSuccess) {
assertTrue("expected completed to be true, but was false", postTestState.completed);
}
}
} finally {
LOGGER.fine("Verifying end state of retry_test resource complete");
if ((shouldCleanup(testSuccess, testSkipped)) && retryTest != null) {
testBench.deleteRetryTest(retryTest);
retryTest = null;
}
}
}
}
};
}
Aggregations