Search in sources :

Example 6 with SdkResponse

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

the class AsyncClientHandlerTransformerVerificationTest method marshallerThrowsException_shouldTriggerExceptionOccurred.

@Test
public void marshallerThrowsException_shouldTriggerExceptionOccurred() {
    SdkClientException exception = SdkClientException.create("Could not handle response");
    when(marshaller.marshall(any(SdkRequest.class))).thenThrow(exception);
    AtomicBoolean exceptionOccurred = new AtomicBoolean(false);
    executeAndWaitError(new TestTransformer<SdkResponse, Void>() {

        @Override
        public void exceptionOccurred(Throwable error) {
            exceptionOccurred.set(true);
            super.exceptionOccurred(error);
        }
    });
    assertThat(exceptionOccurred.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) SdkRequest(software.amazon.awssdk.core.SdkRequest) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) Test(org.junit.Test)

Example 7 with SdkResponse

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

the class AsyncClientHandlerTransformerVerificationTest method prepareCallsEqualToExecuteAttempts.

@Test
public void prepareCallsEqualToExecuteAttempts() {
    mockSuccessfulResponse();
    AtomicLong prepareCalls = new AtomicLong(0);
    executeAndWaitError(new TestTransformer<SdkResponse, Void>() {

        @Override
        public CompletableFuture<Void> prepare() {
            prepareCalls.incrementAndGet();
            return super.prepare();
        }

        @Override
        public void onStream(SdkPublisher<ByteBuffer> stream) {
            stream.subscribe(new DrainingSubscriber<ByteBuffer>() {

                @Override
                public void onComplete() {
                    transformFuture().completeExceptionally(RetryableException.builder().message("retry me please: " + prepareCalls.get()).build());
                }
            });
        }
    });
    assertThat(prepareCalls.get()).isEqualTo(1 + RETRY_POLICY.numRetries());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) DrainingSubscriber(software.amazon.awssdk.core.async.DrainingSubscriber) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with SdkResponse

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

the class AsyncClientHandlerTransformerVerificationTest method nonRetryableErrorDoesNotTriggerRetry.

@Test
public void nonRetryableErrorDoesNotTriggerRetry() {
    mockSuccessfulResponse();
    AtomicLong prepareCalls = new AtomicLong(0);
    executeAndWaitError(new TestTransformer<SdkResponse, Void>() {

        @Override
        public CompletableFuture<Void> prepare() {
            prepareCalls.incrementAndGet();
            return super.prepare();
        }

        @Override
        public void onStream(SdkPublisher<ByteBuffer> stream) {
            super.transformFuture().completeExceptionally(new RuntimeException("some error"));
        }
    });
    assertThat(prepareCalls.get()).isEqualTo(1L);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 9 with SdkResponse

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

the class BaseAsyncClientHandler method execute.

@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> CompletableFuture<ReturnT> execute(ClientExecutionParams<InputT, OutputT> executionParams, AsyncResponseTransformer<OutputT, ReturnT> asyncResponseTransformer) {
    return measureApiCallSuccess(executionParams, () -> {
        if (executionParams.getCombinedResponseHandler() != null) {
            // attempt to do this.
            throw new IllegalArgumentException("A streaming 'asyncResponseTransformer' may not be used when a " + "'combinedResponseHandler' has been specified in a " + "ClientExecutionParams object.");
        }
        ExecutionAttributes executionAttributes = executionParams.executionAttributes();
        executionAttributes.putAttribute(InternalCoreExecutionAttribute.EXECUTION_ATTEMPT, 1);
        AsyncStreamingResponseHandler<OutputT, ReturnT> asyncStreamingResponseHandler = new AsyncStreamingResponseHandler<>(asyncResponseTransformer);
        // For streaming requests, prepare() should be called as early as possible to avoid NPE in client
        // See https://github.com/aws/aws-sdk-java-v2/issues/1268. We do this with a wrapper that caches the prepare
        // result until the execution attempt number changes. This guarantees that prepare is only called once per
        // execution.
        TransformingAsyncResponseHandler<ReturnT> wrappedAsyncStreamingResponseHandler = IdempotentAsyncResponseHandler.create(asyncStreamingResponseHandler, () -> executionAttributes.getAttribute(InternalCoreExecutionAttribute.EXECUTION_ATTEMPT), Integer::equals);
        wrappedAsyncStreamingResponseHandler.prepare();
        // Running beforeExecution interceptors and modifyRequest interceptors.
        ExecutionContext context = invokeInterceptorsAndCreateExecutionContext(executionParams);
        HttpResponseHandler<OutputT> decoratedResponseHandlers = decorateResponseHandlers(executionParams.getResponseHandler(), context);
        asyncStreamingResponseHandler.responseHandler(decoratedResponseHandlers);
        TransformingAsyncResponseHandler<? extends SdkException> errorHandler = resolveErrorResponseHandler(executionParams.getErrorResponseHandler(), context, crc32Validator);
        TransformingAsyncResponseHandler<Response<ReturnT>> combinedResponseHandler = new CombinedResponseAsyncHttpResponseHandler<>(wrappedAsyncStreamingResponseHandler, errorHandler);
        return doExecute(executionParams, context, combinedResponseHandler);
    });
}
Also used : ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) Response(software.amazon.awssdk.core.Response) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) CombinedResponseAsyncHttpResponseHandler(software.amazon.awssdk.core.internal.http.async.CombinedResponseAsyncHttpResponseHandler) AsyncStreamingResponseHandler(software.amazon.awssdk.core.internal.http.async.AsyncStreamingResponseHandler)

Example 10 with SdkResponse

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

the class BaseAsyncClientHandler method execute.

@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse> CompletableFuture<OutputT> execute(ClientExecutionParams<InputT, OutputT> executionParams) {
    return measureApiCallSuccess(executionParams, () -> {
        // Running beforeExecution interceptors and modifyRequest interceptors.
        ExecutionContext executionContext = invokeInterceptorsAndCreateExecutionContext(executionParams);
        TransformingAsyncResponseHandler<Response<OutputT>> combinedResponseHandler = createCombinedResponseHandler(executionParams, executionContext);
        return doExecute(executionParams, executionContext, combinedResponseHandler);
    });
}
Also used : Response(software.amazon.awssdk.core.Response) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext)

Aggregations

SdkResponse (software.amazon.awssdk.core.SdkResponse)25 CompletableFuture (java.util.concurrent.CompletableFuture)12 Test (org.junit.Test)12 ByteBuffer (java.nio.ByteBuffer)11 VoidSdkResponse (software.amazon.awssdk.core.protocol.VoidSdkResponse)11 HashMap (java.util.HashMap)9 Consumer (java.util.function.Consumer)9 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 SdkPublisher (software.amazon.awssdk.core.async.SdkPublisher)8 SdkServiceException (software.amazon.awssdk.core.exception.SdkServiceException)8 Map (java.util.Map)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Flowable (io.reactivex.Flowable)6 List (java.util.List)6 CompletionException (java.util.concurrent.CompletionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Executors (java.util.concurrent.Executors)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6