Search in sources :

Example 1 with RetryTestResource

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);
}
Also used : DateTimeFormatter(org.threeten.bp.format.DateTimeFormatter) InstructionList(com.google.cloud.conformance.storage.v1.InstructionList) RetryTestResource(com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoCredentials(com.google.cloud.NoCredentials) BlobId(com.google.cloud.storage.BlobId) Random(java.util.Random) StorageOptions(com.google.cloud.storage.StorageOptions) ByteBuffer(java.nio.ByteBuffer) BlobWriteOption(com.google.cloud.storage.Storage.BlobWriteOption) Blob(com.google.cloud.storage.Blob) DataGeneration(com.google.cloud.storage.DataGeneration) TestName(org.junit.rules.TestName) ZoneOffset(org.threeten.bp.ZoneOffset) Instant(org.threeten.bp.Instant) Method(com.google.cloud.conformance.storage.v1.Method) ZoneId(org.threeten.bp.ZoneId) StorageRpc(com.google.cloud.storage.spi.v1.StorageRpc) ClassRule(org.junit.ClassRule) JsonParser(com.google.api.client.json.JsonParser) StorageObject(com.google.api.services.storage.model.StorageObject) BlobInfo(com.google.cloud.storage.BlobInfo) ImmutableMap(com.google.common.collect.ImmutableMap) BucketInfo(com.google.cloud.storage.BucketInfo) Assert.assertTrue(org.junit.Assert.assertTrue) Reflection(com.google.common.reflect.Reflection) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Logger(java.util.logging.Logger) TestBench(com.google.cloud.storage.conformance.retry.TestBench) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) FixedHeaderProvider(com.google.api.gax.rpc.FixedHeaderProvider) Rule(org.junit.Rule) Optional(java.util.Optional) WriteChannel(com.google.cloud.WriteChannel) Storage(com.google.cloud.storage.Storage) Assert.assertEquals(org.junit.Assert.assertEquals) PackagePrivateMethodWorkarounds(com.google.cloud.storage.PackagePrivateMethodWorkarounds) Clock(org.threeten.bp.Clock) Blob(com.google.cloud.storage.Blob) StorageObject(com.google.api.services.storage.model.StorageObject) BlobInfo(com.google.cloud.storage.BlobInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) StorageRpc(com.google.cloud.storage.spi.v1.StorageRpc) Storage(com.google.cloud.storage.Storage) StorageOptions(com.google.cloud.storage.StorageOptions) RetryTestResource(com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource) WriteChannel(com.google.cloud.WriteChannel) BucketInfo(com.google.cloud.storage.BucketInfo) BlobId(com.google.cloud.storage.BlobId)

Example 2 with RetryTestResource

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;
                    }
                }
            }
        }
    };
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) RetryTestResource(com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource)

Aggregations

RetryTestResource (com.google.cloud.storage.conformance.retry.TestBench.RetryTestResource)2 JsonParser (com.google.api.client.json.JsonParser)1 FixedHeaderProvider (com.google.api.gax.rpc.FixedHeaderProvider)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 NoCredentials (com.google.cloud.NoCredentials)1 WriteChannel (com.google.cloud.WriteChannel)1 InstructionList (com.google.cloud.conformance.storage.v1.InstructionList)1 Method (com.google.cloud.conformance.storage.v1.Method)1 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 BucketInfo (com.google.cloud.storage.BucketInfo)1 DataGeneration (com.google.cloud.storage.DataGeneration)1 PackagePrivateMethodWorkarounds (com.google.cloud.storage.PackagePrivateMethodWorkarounds)1 Storage (com.google.cloud.storage.Storage)1 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)1 StorageOptions (com.google.cloud.storage.StorageOptions)1 TestBench (com.google.cloud.storage.conformance.retry.TestBench)1 StorageRpc (com.google.cloud.storage.spi.v1.StorageRpc)1 ImmutableMap (com.google.common.collect.ImmutableMap)1