Search in sources :

Example 36 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class FileAsyncRequestBodyTest method changingFile_fileGetsDeleted_failsBecauseDeleted.

@Test
public void changingFile_fileGetsDeleted_failsBecauseDeleted() throws Exception {
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).build();
    ControllableSubscriber subscriber = new ControllableSubscriber();
    // Start reading file
    asyncRequestBody.subscribe(subscriber);
    subscriber.sub.request(1);
    assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS));
    // Delete the file
    Files.delete(testFile);
    // Finishing reading the file
    subscriber.sub.request(Long.MAX_VALUE);
    assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)).hasCauseInstanceOf(IOException.class);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Example 37 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class FileAsyncRequestBodyTest method changingFile_fileGetsLongerThanExistingLength_failsBecauseTooLong.

@Test
public void changingFile_fileGetsLongerThanExistingLength_failsBecauseTooLong() throws Exception {
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).build();
    ControllableSubscriber subscriber = new ControllableSubscriber();
    // Start reading file
    asyncRequestBody.subscribe(subscriber);
    subscriber.sub.request(1);
    assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS));
    // Change the file to be 1 byte longer than when we started
    int currentSize = Math.toIntExact(Files.size(testFile));
    byte[] slightlyLongerFileContent = new byte[currentSize + 1];
    ThreadLocalRandom.current().nextBytes(slightlyLongerFileContent);
    Files.write(testFile, slightlyLongerFileContent);
    // Finishing reading the file
    subscriber.sub.request(Long.MAX_VALUE);
    assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)).hasCauseInstanceOf(IOException.class);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Example 38 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class FileAsyncRequestBodyTest method changingFile_fileGetsTouched_failsBecauseUpdatedModificationTime.

@Test
public void changingFile_fileGetsTouched_failsBecauseUpdatedModificationTime() throws Exception {
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).build();
    ControllableSubscriber subscriber = new ControllableSubscriber();
    // Start reading file
    asyncRequestBody.subscribe(subscriber);
    subscriber.sub.request(1);
    assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS));
    // Change the file to be updated
    // Wait for 1 second so that we are definitely in a different second than when the file was created
    Thread.sleep(1_000);
    Files.setLastModifiedTime(testFile, FileTime.from(Instant.now()));
    // Finishing reading the file
    subscriber.sub.request(Long.MAX_VALUE);
    assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)).hasCauseInstanceOf(IOException.class);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Example 39 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ContentLengthMismatchTest method contentShorterThanContentLengthHeaderFails.

@Test
public void contentShorterThanContentLengthHeaderFails() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    S3AsyncClient s3Client = getAsyncClientBuilder().build();
    AsyncRequestBody requestBody = new AsyncRequestBody() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.empty();
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
            AsyncRequestBody.fromString("A").subscribe(subscriber);
        }
    };
    assertThatThrownBy(() -> s3Client.putObject(r -> r.bucket(bucket).key(key).contentLength(2L), requestBody).get(10, TimeUnit.SECONDS)).isInstanceOf(ExecutionException.class).hasMessageContaining("content-length");
}
Also used : Subscriber(org.reactivestreams.Subscriber) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.Test)

Example 40 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class AsyncChecksumValidationInterceptorTest method modifyAsyncHttpContent_putObjectRequest_checksumDisabled_shouldNotModify.

@Test
public void modifyAsyncHttpContent_putObjectRequest_checksumDisabled_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributesWithChecksumDisabled();
    Context.ModifyHttpRequest modifyHttpRequest = InterceptorTestUtils.modifyHttpRequestContext(GetObjectRequest.builder().build());
    Optional<AsyncRequestBody> asyncRequestBody = interceptor.modifyAsyncHttpContent(modifyHttpRequest, executionAttributes);
    assertThat(asyncRequestBody).isEqualTo(modifyHttpRequest.asyncRequestBody());
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) Context(software.amazon.awssdk.core.interceptor.Context) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) ChecksumCalculatingAsyncRequestBody(software.amazon.awssdk.services.s3.checksums.ChecksumCalculatingAsyncRequestBody) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)51 ByteBuffer (java.nio.ByteBuffer)25 Test (org.junit.jupiter.api.Test)24 List (java.util.List)17 Test (org.junit.Test)13 Subscriber (org.reactivestreams.Subscriber)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)11 Publisher (org.reactivestreams.Publisher)9 Flowable (io.reactivex.Flowable)8 Consumer (java.util.function.Consumer)8 AsyncAws4Signer (software.amazon.awssdk.auth.signer.AsyncAws4Signer)8 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)8 Signer (software.amazon.awssdk.core.signer.Signer)8 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Generated (software.amazon.awssdk.annotations.Generated)7 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)7